cs1010s Cheatsheet Midterms 2
cs1010s Cheatsheet Midterms 2
Types Examples Types Elements Variables can be assigned to any datatype Index - str/tup[i] Slice - str/tup[start:stop:step]
Integers 1 2 3 Calculation ** * / // + - % e.g. Start Index to start
Floats 1.0 2.1 3.001 Comparison > >= < <= == != is x=5 x = 0.5 string1 = ‘hello!’ tuple2 = (1,2,3) Index to stop
returns i-th Stop
Strings ‘ ‘ / “ “ ‘Cs1010s!’ ‘’ / “” Truth Table (non-inclusive)
index of str/tup
Multiple Variable Assignment (Single Line) Number of Indices to skip
Booleans True (1) False (0) AND TRUE FALSE OR TRUE FALSE NOT Step
Use this to replace values at the same time If negative, slice in reverse
Tuples (1,2,3) (1, True, ‘a’) () TRUE TRUE FALSE TRUE TRUE TRUE Negates If [start or stop ≥ len(str/tup)]
e.g. If i ≥ len(str/tup)
* When compared, Bool become 1/0 FALSE FALSE FALSE FALSE TRUE FALSE bool/is/in or [stop < start and step > 0]
x, y = 1, 2 Index Error
x, y = y, x x and y values are swapped No error, slice returned even if empty
Arithmetic Operations
x = 2, y =1
Types Integer Float String Bool(1/0) Tuple Forward
0 1 2 3 4 5 6 7 8 9
Integer ** * / // + - % ** * / // + - % * ** * / // + - % * Assignment Operator Index
Float ** * / // + - % ** * / // + - % * ** * / // + - % * Operator Example, variable is x Equivalent to str/tup a b c d e f g h i j
String * * + * = x=5 x=5 Reverse
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
+= x += 5 x=x+5 Index
Bool(1/0) ** * / // + - % ** * / // + - % * ** * / // + - % *
Tuple * * * + -= x -= 5 x=x-5
How to slice with negative indices? Same as positive.
*= x *= 5 x=x*5 Use reverse index (or convert to forward index) and read from Start Index to
Comparison Operations
/= x /= 5 x=x/5 End Index.
Types Integer Float String Bool(1/0) Tuple * Stop is non-inclusive for negative indices as well!
%= x %= 5 x=x%5
Integer > >= < <= == != > >= < <= == != == != > >= < <= == != == !=
//= x //= 5 x = x // 5
Float > >= < <= == != > >= < <= == != == != > >= < <= == != == != Types of Functions
**= x **= 5 x = x ** 5
String == != == != > >= < <= == != == != == != Functions with a name Anonymous (lambda) functions
Can be used for a variable assigned to any datatype, as
Bool(1/0) > >= < <= == != > >= < <= == != == != > >= < <= == != == != def <fn> (params): <assign> = lambda params: desired result
long as the Arithmetic Operation is valid for that type
Tuple == != == != == != == != > >= < <= == != <code if necessary> e.g.
Variable Scoping return desired result square = lambda x: x**2
is/is not can be used where ==/!= can be used but is/is not compares identity while ==/!= compares equivalence
Numbers (int, flt, and bool) Letters tup and str
Variables modified within a function do not alter the e.g. square(2) = 2**2 = 4
Compared numerically Uppercase letters < Lowercase letters Compare the first non-equal values global variable
def square(x):
** Value of str(number) < str(letter) Value increases down the alphabet Else, compare by size >>> x = 5 #global variable
return x**2 * If an assignment can stand on its own
>>> def square(x):
Type Conversions square(2) = 2**2 = 4 without (), it is most likely a lambda function
return x*x
Integer Float String Bool Tuple Value inputted will replace the params found in desired result
>>> square(x)
Only for int strings 1 if True Value inputted can be a function as well.
int() No change Floor of float Not possible 25
Turns strings to integers 0 if False Functions are always evaluated leftmost, innermost.
>>> x
Only for float strings 1.0 is True
float() float(5) = 5.0 No change Not possible 5 #remains unchanged Conditional Statements
Turns strings to floats 0.0 if False
Structure: One block
‘True’ if True Membership Tesing
str() str(5) = ‘5’ str(5.0) = ‘5.0’ No change str((1, 2)) = ‘(1, 2)’ if <condition>: Always the first statement. Will evaluate if true.
‘False’ if False in check if a str in a string or an int/str in a tuple <result> “if” statements will start a new block.
True if not empty True if not empty ‘a’ in ‘apple’ True ‘h’ not in ‘hi’ False Can have multiple elif statements.
bool() True True No change elif <condition>:
False if empty False if empty ‘a’ not in (‘a’,’b’) False 1 in (1,2) True Will evaluate if true. Not necessary.
tuple() Not possible Not possible tuple(‘A1’) = (‘A’, ’1’) Not possible No change **1 in ‘cs1010s’ Error **‘1’ in ‘cs1010s’ True <result>
Final Statement. Not necessary. Will evaluate if rest are
else:
Tuples Box and Pointer Notation false. If missing, function will just pass.
<result>
Immutable sequences. They are iterable. Slicing can be used, but a new tuple will be created Elements in tuples exist outside of the tuple.
Examples of Tuples Tuple Operations Tuple
(1, 2, 3, 4) Tuple with 4 elements Tuples can be added Tuples put in other tuples.
((1, 2), 3, 4) Tuple with 3 elements bar = (1, 2) bat = (3, 4) bar = (1, 2) bat = (3, 4)
(1,) Tuple with 1 element foo = bar + bat foo = (bar, bat)
(1) Integer, not a tuple foo (1, 2, 3, 4) foo ((1, 2), (3, 4)) Each box points to another tuple, or a single element
Python Loops Order of Growth Data Abstraction
Iteration *Always look at greatest value ** DO NOT BREAK ABSTRACTION
Recursion *Worse Case Scenario As much as possible, use
For loop While loop
O(1) Constant functions that have been defined
Use it for problems that can build up on the previous value stored in a variable. Similar to
recursion, and it’s better to use this, unless there is a clear recursive formula. O(log n) Logarithmic
** DO NOT CROSS ABSTRACTION
Use it for problems that have some kind of pattern involving a Use it if you need to iterate through a set (str/tup/range()). O(n) Linear
BARRIER AND ASSUME
recursive formula. O(n log n) Linearithmic IMPLEMENTATION
Need to be able to find a base case/null value. Iterating through range() using numbers can be used as a counter or to access the numbers. O(n2) Quadratic/Binary
Similarly, iterating through str/tup can be used as a counter for len(str/tup) or used to access the For example,
O(n3) Cubic/Tertiary
e.g. fib(n) = fib(n-1) + fib(n-1) Base case: f(1), f(2) = 0,1 values. If we “don’t know” x is a tuple,
fact(n) = n * fac(n-1) Null value: f(0) = 1 O(2n) Exponential
you cannot add x to a tuple
break Terminates the current loop and resumes execution at the next statement O(nn) Exponential assuming that it is
continue Rejects all the remaining statements in the loop and returns to the top of the loop O(n!) -