python32 Python Cheat Sheet

Just the basics

Created By: Arianne Colton and Sean Chen

General

  • Python is case sensitive
  • Python index starts from 0
  • Python uses whitespace (tabs or spaces) to indent code instead of using braces.

Help

Type Command
Help Home Page help()
Function Help help(str.replace)
Module Help help(re)

Module (aka Library)

Python module is simply a '.py' file

Type Command
List Module Contents dir(module1)
Load Module import module1 *
Call Function from Module module1.func1()

import statement creates a new namespace and executes all the statements in the associated .py file within that namespace. If you want to load the module's content into current namespace, use from module1 import *

String formatting in 3.6

Write an f prefix before our string, and inside the string you can use curly braces and access variables.

name = "Alex"
my_string = f"Hello {name}"
print(my_string) # Hello Alex
i = 10
print(f"{i} squared is {i*i}") # We can write expressions in the braces that are evaluated at runtime

Artikel on f-strings - Format Specification Mini-Language

Number Formatting

Number Format Output Description
3.1415926 {:.2f} 3.14 Format float 2 decimal places
3.1415926 {:+.2f} +3.14 Format float 2 decimal places with sign
-1 {:+.2f} -1.00 Format float 2 decimal places with sign
2.71828 {:.0f} 3 Format float with no decimal places
5 {:0>2d} 05 Pad number with zeros (left padding, width 2)
5 {:x<4d} 5xxx Pad number with x’s (right padding, width 4)
10 {:x<4d} 10xx Pad number with x’s (right padding, width 4)
1000000 {:,} 1,000,000 Number format with comma separator
0.25 {:.2%} 25.00% Format percentage
1000000000 {:.2e} 1.00e+09 Exponent notation
13 {:10d} 13 Right aligned (default, width 10)
13 {:<10d} 13 Left aligned (width 10)
13 {:^10d} 13 Center aligned (width 10)

Scalar Types

Check data type:

type(variable)

Six commonly used data types

  1. int/long* - Large int automatically converts to long
  2. float* - 64 bits, there is no 'double' type
  3. bool* - True or False
  4. str* - ASCII valued in Python 2.x and Unicode in Python 3

    • String can be in single/double/triple quotes
    • String is a sequence of characters, thus can be treated like other sequences
    • Special character can be done via \ or preface with r:

    python str1 = r'this\f?ff'

    • String formatting can be done in a number of ways

    python template = '%.2f %s haha $%d'; str1 = template % (4.88, 'hola', 2)

    str(), bool(), int() and float() are also explicit type cast functions.

  5. NoneType(None) - Python 'null' value (only one instance of None object exists)

    • None is not a reserved keyword but rather a unique instance of 'NoneType'
    • None is common default value for optional function arguments : python def func1(a, b, c = None)
    • Common usage of None: python if variable is None:
  6. datetime - built-in python 'datetime' module

    provides 'datetime', 'date', 'time' types.

    • 'datetime' combines information stored in 'date' and 'time'
Task Commands
Create datetime from String dt1 = datetime.strptime(' 20091031 ','%Y%m%d')
Format datetime to String dt1.strftime('%m/%d/%Y%H:%M')
Change Field Value dt2 = dt1.replace(minute =0 , second = 30 )
Get Difference diff = dt1 - dt
diff is a 'datetime.timedelta' object

Note: Most objects in Python are mutable except


Data structures

Note: All non-Get function call i.e. list1.sort() examples below are in-place (without creating a new object) operations unless noted otherwise.

Tuple

One dimensional, fixed-length, immutable sequence of Python objects of ANY type.

Task Command
Create Tuple tup1 = 4 , 5 , 6 or
tup1 = ( 6 , 7 , 8 )
Create Nested Tuple tup1 = ( 4 , 5 , 6 ), ( 7 , 8 )
Convert Sequence or Iterator to Tuple tuple([ 1 , 0 , 2 ])
Concatenate Tuples tup1 + tup
Unpack Tuple a, b, c = tup

Application of Tuple:

Swap variables:

b, a = a, b

List

One dimensional, variable length, mutable (i.e. contents can be modified) sequence of Python objects of any type.

Task Command
Create List list1 = [ 1 , 'a', 3 ] or
list1 = list(tup1)
Concatenate Lists* list1 + list2 or
list1.extend(list2) 1.
Append to End of List list1.append('b')
Insert to Specific Position list1.insert(posIdx,'b') 2.
Inverse of Insert valueAtIdx = list1.pop(posIdx)
Remove First Value from List list1.remove('a')
Check Membership 3 in list1 => True 3.
Sort List list1.sort()
Sort with User-supplied Function list1.sort(key = len) # sort by length
  1. List concatenation using '+' is expensive since a new list must be created and objects copied over. Thus, extend() is preferable.

  2. Insert is computationally expensive compared with append.

  3. Checking that a list contains a value is lot slower than dicts and sets as Python makes a linear scan where others (based on hash tables) in constant time.

Built-in 'bisect module ‡

  • Implements binary search and insertion into a sorted list

  • 'bisect.bisect' finds the location, where 'bisect.insort' actually inserts into that location.

WARNING: bisect module functions do not check whether the list is sorted, doing so would be computationally expensive. Thus, using them in an unsorted list will succeed without error but may lead to incorrect results.


Dict (hash map)

Task Command
Create Dict dict1 = {'key1' : 'value1' , 2 : [ 3 , 2 ]}
Create Dict from Sequence dict(zip(keyList,valueList))
Get/Set/Insert Element dict1['key1'] 1.
dict1['key1'] = 'newValue'
Get with Default Value dict1.get('key1',defaultValue) 2.
Check if Key Exists 'key1' in dict
Delete Element del dict1['key1']
Get Key List dict1.keys() 3.
Get Value List dict1.values() 3.
Update Values dict1.update(dict2)
dict1 values are replaced by dict
Iterating over keys and values for key, val in dict1.items():
  1. 'KeyError' exception if the key does not exist.

  2. 'get()' by default (aka no 'defaultValue') will return 'None' if the key does not exist.

  3. Returns the lists of keys and values in the same order. However, the order is not any particular order, aka it is most likely not sorted.

Valid dict key types

  • Keys have to be immutable like scalar types (int, float, string) or tuples (all the objects in the tuple need to be immutable too)
  • The technical term here is 'hashability', check whether an object is hashable with python thehash('this is string'), hash([ 1 , 2 ]) This would fail.

Set

  • A set is an unordered collection of unique elements.

  • You can think of them like dicts but keys only.

| Task | Command | | Create Set | set([ 3 , 6 , 3 ]) or
{ 3 , 6 , 3 } | | Test | Subset set1.issubset (set2) | | Test | Superset set1.issuperset (set2) | | Test sets have same content | set1 == set |

  • Set operations:
Task Command
Union (aka 'or') set1 | set
Intersection (aka 'and') set1 & set
Difference set1 - set
Symmetric Difference (aka 'xor') set1 ^ set
Is superset of / is proper superset of set1 >= set / set1 > set
Is subset of / is proper subset of set1 <= set / set1 < set
Add/Remove/Discard/Pop Element set1.add(elem); set1.remove(elem) #raises error if not existent
set1.discard(elem); set1.pop()
Clear set set1.clear

Slicing for Sequence Types

Sequence types include 'str', 'array', 'tuple', 'list', etc.

Task Command
Notation list1[start:stop]
list1[start:stop:step]
(If step is used)

Notes:

  • start index is included, but 'stop' index is NOT.
  • start/stop can be omitted in which they default to the start/end.

Application of 'step':

Take every other element:

list1[::^2 ]

Reverse a string:

str1[::-1]

Packing and unpacking iterables with asterisk

Unpacking an iterable:

[x for x in range(100)] == [*range(100)] # <= far shorter and cooler!

Unpacking dictionary keys:

d = {'key1': 'A'}
list(d.keys()) == [*d] # <= creates a list of all keys!

Unpacking a whole dictionary


Functions

Python is pass by reference , function arguments are passed by reference.

  • Basic Form :

    def func1(posArg1, keywordArg1 =1, ..):

    Note:

    • Keyword arguments MUST follow positional arguments.
    • Python by default is NOT "lazy evaluation", expressions are evaluated immediately.
  • Function Call Mechanism:

    1. All functions are local to the module level scope. See 'Module' section.
    2. Internally, arguments are packed into a tuple and dict, function receives a tuple 'args' and dict 'kwargs' and internally unpack.
  • Common usage of 'Functions are objects':

    python def func1(ops = [str.strip, user_define_func, ..], ..): for function in ops: value = function(value)

Return Values

  • None is returned if end of function is reached without encountering a return statement.

  • Multiple values can be returned via one tuple object:

return (value1, value2)
value1, value2 = func1(..)

Args, Kwargs

Use the args/kwargs arguments to keep backward code compatibility and less updating of old code!

def version3(a, b , *args, **kwargs):
    print(a)
    print(b)
    # all no-name parameters
    if args:
        for c in args:
            print(c)
    # all named (aka key-)parameters
    if kwargs:
        for key, value in kwargs.items():
            print(key,':', value)

Anonymous (aka lambda) functions python32

An anonymous function is a simple function consisting of a single statement.

lambda x : x * 2
# def func1(x) : return x * 2

Deriving new functions from existing ones by partial argument application:

ma60 = lambda x : pd.rolling_mean(x,60 )

Useful Functions (for Data Structures)

Enumerate returns a sequence (i, value) tuples where i is the index of current item.

Create a dict mapping of value of a sequence (assumed to be unique) to their locations in the sequence:

for i, value in enumerate(collection):

Sorted returns a new sorted list from any sequence

sorted([ 2 , 1 , 3 ]) => [ 1 , 2 , 3 ]

Application

# returns sorted unique characters
sorted(set('abc bcd')) => [' ','a', 'b', 'c', 'd']

Zip pairs up elements of a number of lists, tuples or other sequences to create a list of tuples :

zip(seq1, seq2)

Result:

[('seq1_1', 'seq2_1'), (..), ..]
  • Zip can take arbitrary number of sequences. However, the number of elements it produces is determined by the 'shortest' sequence.

  • Application: Simultaneously iterating over multiple sequences :

for i, (a, b) in enumerate(zip(seq1, seq2)):
    print (i, a, b)
  • Unzip - another way to think about this is converting a list of rows to a list of columns.
seq1, seq2 = zip(*zipOutput)

Reversed iterates over the elements of a sequence in reverse order.

list(reversed(range( 10 ))) *

reversed() returns the iterator, list() makes it a list.


Control and Flow

Operators

for conditions in 'if else'

Task Command
Check if two variables are same object var1 is var2
... are different object var1 is not var
Check if two variables have same value var1 == var

Warning: Use 'and', 'or', 'not' operators for compound conditions, not &&, ||, !.

If-Oneliner

temp = 10 if False else 20

Common usage of 'for' operator

Task Command
Iterating over a collection (i.e. list or tuple) or an iterator for element in iterator:
... If elements are sequences, can be 'unpack' for a, b, c in iterator:

pass

no-op statement. Used in blocks where no action is to be taken.

Ternary Expression

less verbose version of 'if else'

  • Basic Form :
value = true-expr if condition else false-expr
# example
x = 1100 if y==true else 320

No switch/case statement! Use if/elif instead.


Object-Oriented Programming

1) 'object' is the root of all Python types

2) Everything (number, string, function, class, module, etc.) is an object, each object has a 'type'. Object variable is a pointer to its location in memory.

3) All objects are reference-counted.

sys.getrefcount( 5 ) => x
# This creates a 'reference' 
# to the object on the right side of =, 
# thus both a and b point to 5
a = 5 , b = a
sys.getrefcount( 5 ) => x + 2
del(a); sys.getrefcount( 5 ) => x + 1

4) Class Basic Form:

class MyObject(object):
# 'self' is equivalent of 'this' in Java/C++

    def __init__(self, name):
    self.name = name

    def memberFunc1(self, arg1):
    pass

    @staticmethod
    def classFunc2(arg1):
    pass

obj1 = MyObject('name1')
obj1.memberFunc1('a')
MyObject.classFunc2('b')

5) Useful interactive tool:

# list all methods available on the object
dir(variable1)

List, Set and Dict comprehensions

List comprehensions

Concisely form a new list by filtering the elements of a collection and transforming the elements passing the filter in one concise expression.

Basic form:

[expr for val in collection if condition]

A shortcut for:

result = []
for val in collection:
    if condition:
        result.append(expr)

The filter condition can be omitted, leaving only the expression.

Dict comprehensions

Basic form :

{ key-expr : value-expr for value in collection if condition }

Set Comprehension

{ expr for val in collection if condition }

Nested list Comprehensions

Basic form:

[expr for val in collection for
    innerVal in val if condition]

Common String operations

Concatenate List/Tuple with Separator:

', '.join([ 'v1', 'v2', 'v3']) => 'v1, v2, v3'

Format String:

string1 = 'My name is { 0 }{name}'
newString1 = string1.format('Sean', name ='Chen')

print("This is Pi: {:2.3f}".format(math.pi))
Number Format Output Description
3.1415926 {:.2f} 3.14 Format float 2 decimal places
3.1415926 {:+.2f} +3.14 Format float 2 decimal places with sign
-1 {:+.2f} -1.00 Format float 2 decimal places with sign
3.200 {:g} 3.2 Format float to remove insignificant trailing zeros
2.71828 {:.0f} 3 Format float with no decimal places
5 {:0>2d} 05 Pad number with zeros (left padding, width 2)
5 {:x<4d} 5xxx Pad number with x's (right padding, width 4)
10 {:x<4d} 10xx Pad number with x's (right padding, width 4)
1000000 {:,} 1,000,000 Number format with comma separator
0.25 {:.2%} 25.00% Format percentage
1000000000 {:.2e} 1.00e+09 Exponent notation
13 {:10d} 13 Right aligned (default, width 10)
13 {:<10d} 13 Left aligned (width 10)
13 {:^10d} 13 Center aligned (width 10)

Split String:

sep = '-';
stringList1 = string1.split(sep)

Get Substring:

start = 1
print( string1[start:8] )

String Padding with Zeros:

month = '5';
month.zfill( 2 ) 

# Result: '05'

month = '12';
month.zfill( 2 )

# Result: '12'

Exception Handling

1. Basic Form :

try:
    ..
except ValueError as e:
    print e
except (TypeError, AnotherError):
    ..
except:
    ..
finally:
    # clean up, e.g. close db
    ..

2. Raise Exception Manually

raise AssertionError # assertion failed
raise SystemExit # request program exit
raise RuntimeError('Error message : ..')

Properties python32

The getter and setter system of python. This is done with the property command.

class Celsius:
    def get_temperature(self):
        return self._temperature

    def set_temperature(self, value):
        if value < -273:
            raise ValueError("Temperature below -273 is not possible")
        self._temperature = value
    # --- property( getter_function=None, setter_function=None, fdel=None, doc=None)
    temperature = property(get_temperature,set_temperature)

This is a short version of this programiz tutorial.

Generators

Generator functions look like regular functions, but act more like lists which isn't stored in memory and instead created when needed. This is signaled by using yield instead of return.

Example:

from pathlib import Path

def archive_files():
    archive_path = Path(r"H:\Projects\002_Companies\Vertify\ImportExport")
    for f in archive_path.iterdir():
        if f.is_dir() and "Male" in f.stem or "Female" in f.stem:
            yield f

# generator function used as a parameter! Yay!
def print_files(gen):
    for x in gen():
        print(x.stem)

print_files(archive_files) 

Coroutines

From stackabuse: Coroutines are functions that deliberately yield control over to the caller.

def bare_bones():
    print("My first Coroutine!")
    try:
        while True:
            value = (yield)
            print(value)
    except GeneratorExit:
        print("Exiting coroutine...")

coroutine = bare_bones()
next(coroutine) # initiate
coroutine.send("First Value") # send value
coroutine.send("Second Value") # send another value
coroutine.close()

Coroutines are the other side of the generator medal: Generators produce data, Coroutines consume data.