Show Menu
Cheatography

nus cs1010s python Cheat Sheet (DRAFT) by

CS1010S Mid-terms cheatsheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Types of Errors

IndexError
Raised when the index of a sequence is out of range
NameError
Raised when a variable is not found in the local and global scope
Syntax­Error
Raised by the parser when a syntax error is encoun­tered
TypeError
Raised when a functi­on/­ope­ration is applied to an object of an incorrect type
Unboun­dLo­cal­Error
Raised when a reference is made to a local variable in a functi­on/­method, but no value has been bound to that variable
ZeroDi­vis­ion­Error
Raised when the second operand of a divisi­on/­module operation is zero
ValueError
Raised when a function gets an argument of a correct type but improper value
Memory­Error (Recur­sio­nError)
Raised when an operation runs out of memory
Runtim­eError
Raised when an error does not fall under any other category

Alphab­etical Order (ASCII Table, ord & chr)

48: 0 49: 1 50: 2 51: 3 52: 4 53: 5 54: 6 55: 7 56: 8 57: 9 58: : 59: ; 60: < 61: = 62: > 63: ? 64: @

65: A 66: B 67: C 68: D 69: E 70: F 71: G 72: H 73: I 74: J 75: K 76: L 77: M 78: N 79: O 80: P 81: Q 82: R 83: S 84: T 85: U 86: V 87: W 88: X 89: Y 90: Z

97: a 98: b 99: c 100: d 101: e 102: f 103: g 104: h 105:i 106: j 107: k 108: l 109: m 110: n 111: o 112: p 113: q 114: r 115: s 116: t 117: u 118: v 119: w 120: x 121: y 122:z

ord('A') = 65, chr(66) = 'B'
0 < 9 < 'A' < 'Z' < 'a' < 'z'
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Loop Statements

break
Terminates the whole loop
continue
Stops the current iteration of the loop, and goes on to the next iteration of the loop
pass
Does nothing and continues the rest of the code inside the current iteration of the loop

Boolean Values

False evaluates to 0; int(False) == 0, while True evaluates to 1; int(True) = 1
On the other hand, any empty str, tuple, list ('', (), []), the value 0 and None evaluates to False; bool(0­/No­ne/­"­"/()) = False, and any other expression will evaluate to True; bool(1­/-9­5/"C­S1010S is fun"­/("C­", "­S", "­S", "­U", "­C", "­K", "­S")) = True

String Slicing Mechanism

s = 'abcdef '
0 1 2 3 4 5
-6-5-4­-3-2-1

s[star­t(i­ncl­usi­ve)­:st­op(­exc­lus­ive­):step]
e.g.
s[1:] = 'bcdef'
s[3::-1] = 'dcba'
s[6:] = ''
s[2:-6:-1] = 'cb'

Tuple and string functions

len()
Returns the length of the string­/number of items in the tuple
max()
Returns the largest item in the tuple
min()
Returns the smallest item in the tuple
sum()
Returns the sum of all elements in the tuple
tuple()
Converts an iterable into a tuple
tuple.c­ou­nt(ele)
Counts the number of occurr­ences of an element in a tuple
str.in­dex­(ele)
Searches the string for a specified ele from the left and returns the position of where it was found

Checking data type

type(v­alue) == Type
isinst­anc­e(v­alue, Type)
 

Orders of Growth (OOG)

O(1) < O(log n) < O(n) < O(n log n) < O(n2) < O(2n) < O(n!) < O(nn)
O(1): Indexing, replacing variable name
O(log n): Constantly halvin­g/d­oubling a number (depending on direction)
O(n): Going through the whole tuple/­string (for loop/r­ecu­rsion)
O(n2): Going through the whole tuple once for each element (Usually nested for loop)
O(2n): The tree splits into 2/x number of branches for each level (Usually for recursion tree)

Sample Answer:
Time: O(n), there is a total of n recursive calls.
Space: O(n), there is a total of n recursive calls, and each call will take up space on the stack.

Time: O(n), the loop will iterate n times.
Space: O(1), no extra memory is needed because the variables are overwr­itten with the new values.

Big O Notation

Time Comple­xity: Sum of time taken at each level of the recursion tree (number of recursive calls, intensive operat­ions)
Time Comple­xity: Count the loops, and the intensive operations (eg string concat­ena­tion)
Space Comple­xity: Height of the recursion tree (Also check for strings, tuples, etc)
Space Comple­xity: Count the variables stored (need to store individual chars for strings)

String Concat­enation

String Concat­enation (2)

String Slicing

String Slicing (2)

Extra OOG

 

Copy of Tree

Flatten Tuples

Counting Leaves

Counting Change Problem

Towers of Hanoi