Exam Reference
Operator precedence
Highest first. Operators on the same line are applied left to right.
( ) | grouping |
x[i] x[a:b] x.method() | indexing, slicing, method call |
** | exponent |
-x | negation |
* / // % | multiply, divide, floor-divide, remainder |
+ - | add, subtract |
== != < <= > >= in not in | comparisons |
not | boolean not |
and | boolean and |
or | boolean or |
Strings
Operators
s + t | a new string: s followed by t |
s * n | a new string: s repeated n times |
s == t | whether the two strings are identical |
s < t | whether s comes before t alphabetically, comparing one character at a time |
t in s | whether t appears inside s as consecutive characters |
len(s) | the number of characters |
s[i] | the character at index i (negative counts from the end) |
s[a:b] | a new string of the characters from index a up to, but not including, b |
Methods
None of these change s; each returns a new value.
s.upper() | a copy with all letters uppercase |
s.lower() | a copy with all letters lowercase |
s.strip() | a copy with whitespace removed from both ends |
s.count(t) | how many times t appears in s |
s.isupper() | whether every letter in s is uppercase |
s.islower() | whether every letter in s is lowercase |
s.isalpha() | whether s is non-empty and all letters |
s.isdigit() | whether s is non-empty and all digits |
s.isalnum() | whether s is non-empty and all letters or digits |
s.isspace() | whether s is non-empty and all whitespace |
Conversions
int(x) | x as an integer |
float(x) | x as a decimal number |
str(x) | x as a string |
input(prompt) | the text the user typed, as a string |
Lists
Operators
L + M | a new list: the elements of L followed by those of M |
L * n | a new list: L repeated n times |
L == M | whether the two lists have the same elements in the same order |
x in L | whether x is one of the elements |
len(L) | the number of elements |
L[i] | the element at index i (negative counts from the end) |
L[a:b] | a new list of the elements from index a up to, but not including, b |
L[i] = x | replaces the element at index i |
Methods
These change L itself.
L.append(x) | adds x at the end; returns None |
L.insert(i, x) | puts x at index i, shifting the rest right; returns None |
L.remove(x) | deletes the first element equal to x; returns None |
L.pop() | deletes the last element and returns it |
L.pop(i) | deletes the element at index i and returns it |
L.index(x) | the index of the first element equal to x |
L.count(x) | how many elements equal x |
L.sort() | puts the elements in increasing order; returns None |
L.reverse() | reverses the order; returns None |
Dictionaries
Operators
{k1: v1, k2: v2} | a new dictionary; {} is empty |
D[k] | the value for k (error if not a key) |
D[k] = v | sets the value for k, adding k if it is new |
k in D | whether k is one of the keys |
len(D) | the number of key-value pairs |
for k in D: | loops over the keys |
Methods
D.get(k) | the value for k, or None if k is not a key |
D.get(k, d) | the value for k, or d if k is not a key |
D.pop(k) | deletes k and returns its value |
D.keys() | all the keys |
D.values() | all the values |
D.items() | all the (key, value) pairs |
Functions
Defining and returning
def f(p1, p2):
statement block | defines f with parameters p1, p2; the indented statement block is the body of the function: one or more indented statements, run when f is called |
return x | immediately stop executing the function; the original function call evaluates to x |
No return executed means the value is None.
Calling a function
f(x, y) | arguments match parameters in order |
f(b=y, a=x) | arguments match parameters by name, in any order |
f(x, b=y) | positional arguments come before named ones |
r = f(x, y) | stores the call's value in r |
Default arguments
def power(base, exp=2):
return base ** exp
power(3) | uses exp = 2: 9 |
power(3, 3) | replaces the default exp: 27 |
Parameters with defaults must come after those without: def f(a, b=5) is legal, def f(a=5, b) is an error.