Python (general) (overall)

 Python

open source (free).
Python is still the leader: 65.6% in 2018.

Advantages:
1) Interpreted language. Hence, the coded program does not need any compilation.
2) Dynamically defines variable types.
3) Unique in its way with less coding which makes it more acceptable for the users.
4) Strongly typed which needs manual typecasting.
5) Portable, extendable, and scalable.

    Web Development Frameworks: Django, Pyramid,
    Web Development Micro-Frameworks: Bottle, Flask,
    Web Development: Tornado, web2py
    GUI Development: tkInter, PyGObject, PyQt, PySide, Kivy, wxPython
    Scientific and Numeric:
      1) SciPy is a collection of packages for mathematics, science, and engineering.
      2) Pandas is a data analysis and modeling library.
      3) IPython is interactive shell that features easy editing/recording of a work
        session, and supports visualizations and parallel computing.
    Software Development: Buildbot, Trac, Roundup
    System Administration: Ansible, Salt, OpenStack

Good at Natural Language Processing (NLP) such as classic NTLK, topic modeling with GenSim, or
the blazing-fast and accurate spaCy. Good at neural networking, with Theano (4.9% in 2018) and
Tensorflow (29.9% in 2018); scikit-learn (24.4% in 2018) for machine learning,
and NumPy and Pandas for data analysis.

Juypter/iPython are Web-based notebook server that allows you to mix code, plots,
and anything in a shareable logbook format.

One of Python's killer concept is Read-Evaluate-Print-Loop (REPL), spread across almost
all languages including both Scala and R.

As opposed to R, Python is a traditional object-oriented language.

Standard library supports many Internet protocols:
 1) HTML and XML
 2) JSON
 3) E-mail processing.
 4) Support for FTP, IMAP, and other Internet protocols.
 5) Easy-to-use socket interface.

Package Index has yet more libraries:
 1) Requests, a powerful HTTP client library.
 2) BeautifulSoup, an HTML parser that can handle all sorts of oddball HTML.
 3) Feedparser for parsing RSS/Atom feeds.
 4) Paramiko, implementing the SSH2 protocol.
 5) Twisted Python, a framework for asynchronous network programming.

Advantages:
 1) Python s focus on readability, coherence, and software quality.
 2) Python code is typically one-third to one-fifth the size of equivalent C++ or Java code.  
 3) Most Python programs run unchanged on all major computer platforms.  
 4) Python to be used as a product customization and extension tool.
 
Disads:
  1) Requires correct white-spacing in your code.
  2) Must wait for features unlike Scala or Java.
  3) Slow execution speed

Dynamic typing, Automatic memory management, Programming-in-the-large support,
Built-in object types

--------------------------------------------------------------        
Python 2.X/3.X story

While 3.X was largely the same language, it ran almost no code written for prior re-
leases. It:
  Imposed a Unicode model with broad consequences for strings, files, and libraries
  Elevated iterators and generators to a more pervasive role, as part of fuller functional paradigm
  Mandated new-style classes, which merge with types, but grow more powerful and complex
  Changed many fundamental tools and libraries, and replaced or removed others entirely

The mutation of print from statement to function alone, aesthetically sound as it may
be, broke nearly every Python program ever written.

When to choose 3.X: new features, evolution
When to choose 2.X: existing code, stability
When to choose both: version-neutral code

Reading-order 3.X impact examples:
  Printing, sorts, the string format method, and some dict calls rely on function keyword arguments.
  Dictionary key lists and tests, and the list calls used around many tools, imply iteration concepts.
  Using exec to run code now assumes knowledge of file objects and interfaces.
  Coding new exceptions requires classes and OOP fundamentals.
  And so on even basic inheritance broaches advanced topics such as metaclasses and descriptors.

--------------------------------------------------------------

Stackless: Python for concurrency

Reimplementation of CPython for concurrency. Because it does not save state on
the C language call stack, Stackless can make Python easier to port to small
stack architectures, provides efficient multiprocessing options, and fosters novel programming
structures such as coroutines.

Features:
 1) Microthreads: tasklets wrap functions allowing them to be launched as microthreads.
 2) Channels: channels can be used for bidirectional communication between tasklets.
 3) Scheduling: a "round robin" scheduler is built in that can schedule either cooperatively or preemptively.
 4) Serialisation: tasklets can be serialised to disk through pickling for later resumption of execution.

--------------------------------------------------------------

PyPy: Python for speed

Reimplementation of CPython for speed with JIT compiler.  7.6 times faster than CPython 2.X.
See http://speed.pypy.org/ .  Support for Stackless and greenlets are now integrated in the normal PyPy.
The main difference that is not going to be fixed is that PyPy does not support refcounting semantics.
PyPy is the successor to Psyco, and incorporates the JIT concepts that Psyco pioneered.

--------------------------------------------------------------

Composition:
1. Programs are composed of modules.
2. Modules contain statements.
3. Statements contain expressions.
4. Expressions create and process objects.

Why Use Built-in Types?
1. Built-in objects make programs easy to write.
2. Built-in objects are components of extensions.
3. Built-in objects are often more efficient than custom data structures.
4. Built-in objects are a standard part of the language.

Object type and its Example literals/creation:
Numbers                         1234, 3.1415, 3+4j, 0b111, Decimal(), Fraction()
Strings                                   'spam', "Bob's", b'a\x01c', u'sp\xc4m'
Lists                                    [1, [2, 'three'], 4.5], list(range(10))
Dictionaries                    {'food': 'spam', 'taste': 'yum'}, dict(hours=10)
Tuples                            (1, 'spam', 4, 'U'), tuple('spam'), namedtuple
Files                                open('eggs.txt'), open(r'C:\ham.bin', 'wb')
Sets                                                 set('abc'), {'a', 'b', 'c'}
Other core types                                           Booleans, types, None
Program unit types                                   Functions, modules, classes
Implementation-related types                     Compiled code, stack tracebacks

--------------------------------------------------------------

STRINGS

S = 'Spam'
Can index from front as zero based, so S[0] is 'S'
Can index in reverse which is one based, so S[-1] is 'm'
Slicing with S[1:3] is 'pa'
Slicing with S[:-1] is 'Spa'
Slicing so nothing happens with S[:] is 'Spam'
Concatenation with S + 'xyz' is 'Spamxyz'
Repetition with S * 4 is 'SpamSpamSpamSpam'
Expand to list by L = list(S) is ['S', 'p', 'a', 'm']

s.find('pa') is 1
S.replace('pa', 'XYZ') is 'SXYZm' after which S is back to being 'Spam'

line = 'aaa,bbb,ccccc,dd'
line.split(',') results in ['aaa', 'bbb', 'ccccc', 'dd']

line2 = 'aaa,bbb,ccccc,dd/n'
line2.rstrip().split(',')   results in ['aaa', 'bbb', 'ccccc', 'dd'] since
  does functions from left to right.

print(help(S.replace))  shows the help

Python s strings also come with full Unicode support required for processing text in
internationalized character sets.

In Python 3.X, the normal str string handles Unicode text (including ASCII, which is
just a simple kind of Unicode); a distinct bytes string type represents raw byte values
(including media and encoded text)

'sp\xc4m' results in 'sp m'
b'a\x01c' is bytes strings are byte-based data results in b'a\x01c'
u'sp\u00c4m' is Unicode literal results in 'sp m'

--------------------------------------------------------------

IMMUTABLE/NOT

Every string operation is defined to produce a new string as its result, because
strings are immutable in Python they cannot be changed in place after they are
created.

Numbers, strings, and tuples are immutable;
Lists, dictionaries, and sets are not.

--------------------------------------------------------------

LISTS/DICTIONARIES

Lists can hold different types of objects and have no fixed size:
L = [123, 'spam', 1.23]
L * 2 results in [123, 'spam', 1.23, 123, 'spam', 1.23]
print(L) results in [123, 'spam', 1.23] since no change of original

Core data types support arbitrary nesting:
M = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
M[1] results in  [4, 5, 6] because it is 2nd row (zero-based)
M[1][2] results in  6 because 2nd row and 3rd item

List comprehension expression:
col2 = [row[1] for row in M]
col2 results in [2, 5, 8]

Python dictionaries are mappings are also collections of other objects, but they
store objects by key instead of by relative position.

D = {'food': 'Spam', 'quantity': 4, 'color': 'pink'}
D['food'] results in {'food': 'Spam', 'quantity': 5, 'color': 'pink'}
 
rec = {'name': {'first': 'Bob', 'last': 'Smith'},
                'jobs': ['dev', 'mgr'],
                'age': 40.5}
rec['jobs'][-1] results in 'mgr'

--------------------------------------------------------------

GARBAGE COLLECTION

In standard Python (a.k.a. CPython), the space is reclaimed immediately, as soon
as the last reference to an object is removed.

--------------------------------------------------------------

IF STATEMENTS

Python knows when the if statement ends by indentation:
D = {'a': 1, 'b': 2, 'c': 3}

if not 'f' in D:
    print('missing')
    print('no, really...')

D = {'a': 1, 'b': 2, 'c': 3}
Ks.sort()
print(Ks)  results in ['a', 'b', 'c']

--------------------------------------------------------------

FOR LOOPS

for c in 'spam':
    print(c.upper())

x = 4
while x > 0:
    print('spam!' * x)
    x -= 1

results in  spam!spam!spam!spam!
            spam!spam!spam!
            spam!spam!
            spam!

FOR loop can be compressed to 1 line:
squares = [x ** 2 for x in [1, 2, 3, 4, 5]]

equals this:
squares = []
for x in [1, 2, 3, 4, 5]: # This is what a list comprehension does
   squares.append(x ** 2) # Both run the iteration protocol internally
    
--------------------------------------------------------------

TUPLES

The tuple is an immutable list. Example would be components about specific
calendar date. Use parentheses to mark tuples.

T = (1, 2, 3, 4) # A 4-item tuple
T + (5, 6)       # results in (1, 2, 3, 4, 5, 6)
T[0] = 2         # results in ERROR since tuples are immutable

--------------------------------------------------------------

FILES (ASCII TEXT)

Last major core type: the file.  

f = open('data.txt', 'w') # Make a new file in output mode ('w' is write)
f.write('Hello\n')        # Write strings of characters to it
f.close()                 # Close to flush output buffers to disk

f = open('data.txt')      # 'r' (read) is the default processing mode
text = f.read()           # Read entire file into a string
text.split()              # File content is always a string

for line in open('data.txt'): print(line)  # has iterator

--------------------------------------------------------------

BINARY FILES

Python s struct module can both create and unpack packed binary data.

import struct
packed = struct.pack('>i4sh', 7, b'spam', 8) # Create packed binary data
packed                                       # returns b'\x00\x00\x00\x07spam\x00\x08'  (10 bytes, not objects or text)

file = open('data.bin', 'wb')                # Open binary output file
file.write(packed)                           # returns 10.  writes packed binary data.
file.close()

data = open('data.bin', 'rb').read()         # Open/read binary data file
data[4:8]                                    # returns b'spam'  .  (slices bytes in the middle).
list(data)                                   # returns [0, 0, 0, 7, 115, 112, 97, 109, 0, 8] . (A sequence of 8-bit bytes)
struct.unpack('>i4sh', data)                 # Unpack into objects again


--------------------------------------------------------------

UNICODE TEXT FILES

Python text files automatically encode on writes and decode on reads per the encoding
scheme name you provide.


Comments

Popular posts from this blog

Upgrading to .NET8 from desktop versions 4.8.X

GHL Chat Bots for Webpage

GHL > Set website so shorter URL address