Returns the index of substring(x) inside a string. If not found it raises ValueError.
y and z are start(inclusive) and stop(noninclusive) indexes respectively.
Back
divmod(x,y)
Front
returns the tuple (x//y, x%y)
Back
list addition (+)
Front
List concatenation
Back
random.seed(x)
Front
if x is not provided then it seeds based on the time
Can provide an integer to assign a specific randomness
returns the sorted iterable. Can pass a function into "key = " or make your own using the lambda function. reverse = True will go from high to low or Z to A.
Back
del list[a:b]
Front
deletes elements in the list from index a (inclusive) to index b (exclusive)
Back
file.readline()
Front
returns one line from the file.
Back
Modulus with negative in numerator
Front
-25% 3 = 2
Returns a positive number. Multiply the denominator by a negative number to go below the numerator then the answer is the amount you add up by to get the remainder
Back
hex(x)
Front
returns hexadecimal representation of an integer
hex(12648430)
'0xc0ffee'
Back
math.e
Front
the value of e
Back
TypeError
Front
-Raised when an operation or function is applied to an object of inappropriate type.
Back
math.radians(x)
Front
converts to radians from degrees input
Back
dict.values()
Front
Returns list of dictionary dict's values. Same as keys in that if you update after creating a values list, the list will update automatically.
Back
Modulus with negative in denominator
Front
5%-2 = -1
Result will be negative. Multiply by a negative number to go above the numerator then subtract to get the remainder
Back
Valid Identifiers
Front
Variables cannot start with numbers
Variables cannot contain symbols
Back
string.upper()
Front
returns the uppercase version of the string
Back
math.sin(x)
Front
sine of x (x in radians)
Back
dict.update(dict2)
Front
adds dict2 key value pairs to the first dict. can be a symbol name for dict or a {} notation inside of the update()
Back
A ValueError exception is raised
Front
If you call the index method to locate an item in a list and the item is not found, this happens
Back
math.pi
Front
the value of pi
Back
string.isalpha()
Front
Returns true (a nonzero number) if the character is a letter. If it is not then it is a zero.
Back
Lambda
Front
anonymous function. general format is:
lambda x: x+1
x is the input and after the colon is the output
Back
random.random()
Front
takes no arguments, returns floats between 0 and 1
Back
file.readlines()
Front
returns a list of the lines.
Back
ZeroDivisionError
Front
Dividing by an expression that evaluates to zero (divide by zero)
Back
Pythonic Assignment
Front
a,b,c = 5/4, 1 , 2
out:
a= 5/4
b = 1
c = 2
Back
random.randint(x,y)
Front
returns a random integer between x and y (including both x and y)
Back
SyntaxError
Front
Errors with syntax
Back
string.join(iterable)
Front
takes the iterable and concatenates each element in a string with the string before join as the separator
Back
dictionary
Front
can be made with {} or dict()
dict(x=4,y=8) are keyword arguments that can be passed through to create key-value pairs on initialization.
Back
string.capitalize()
Front
returns the string with only the first letter in first word capitalized. Lowercase everything else
Back
string.lower()
Front
returns the lowercase version of the string
Back
XOR (^)
Front
Takes the binary of each number then does an xor for each bit. If one is smaller than the other then it just does the smallest ones length
Back
dict.items()
Front
Returns a list of dict's (key, value) tuple pairs. Updates even after it is used
Back
dict.keys()
Front
Returns a list of keys. If a key-value pair is added after the .keys() is used, the list will still update that key into the list.
Back
bin(x)
Front
converts number to binary. returns string
bin(2796202)
'0b1010101010101010101010'
Back
list.pop([i])
Front
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
Back
list.append(value)
Front
appends an element to list. Modifies the original list, doesn't return anything
Back
list.extend(list2)
Front
Takes a iterable and appends each element to the list. Modifies the original list, doesn't return anything
Back
list.insert(i, x)
Front
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
Back
string.find(x,y,z)
Front
Returns the position of the first occurrence of item x in the string, else returns -1. optional start(y) and stop(z) indexes
Back
random.gauss(mu,sigma)
Front
returns a random value on based on the mean (mu) and standard deviation (sigma)
Back
string.title()
Front
Returns the string with each word starting with a capital
Back
string.strip("string")
Front
strips "string" from original string in front and back as many times as possible. If no argument is given then it removes white space characters that are trailing or leading
Back
with (file i/o)
Front
Opens a file, manipulates based on what is indented, then closes the file automatically.
with open('output.txt', 'w') as f:
f.write('Hi there!')
Back
pow(x,y,z)
Front
Return x to the power y; if z is present, return x to the power y, modulo z (computed more efficiently than pow(x, y) % z).
Back
int(x,y)
Front
Can be a string for x, converts to base 10. y specifies the base that x is in. If y is not provided it assumes it is base 10
Back
Section 2
(11 cards)
class header
Front
class ClassName(object):
'code'
Back
__init__
Front
A constructor method that initializes a class instance.
def __init__(self, x=0,y, z):
self.x = x
self.y = y
self.z = z
Back
Set Comprehension
Front
identical to list comprehension but you use curly brackets instead of square
num = {x*3 for x in range(10) if x*3 % 4 == 0}
Back
list comprehension
Front
Python rules for creating lists intelligently. Can have as many for loops and ifs as you want
s = [x for x in range(1:51) if x%2 == 0]
[2, 4, 6, 8, 10, 12, 14, 16, etc]
Back
__sub__
Front
overwrites the class operator for subtraction
def __sub__(self, other):
x = self.x - other.x
y = self.y - other.y
return Point(x, y)
Back
time.perf_counter()
Front
Returns a time to be used in comparison.
The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.
Back
__str__
Front
returns a string that is used when a object is printed
def __str__(self):
return '(%g, %g)' % (self.x, self.y)
Back
AssertationError
Front
Returns an Assertation Error if the conditions are false.
syntax:
assert boolean statement
Back
static method
Front
Static methods are defined in an object but do not require instance data. Therefore, they can be called directly with the class name, such as Math.random().
Use the decorator before the definition to explicitly declare it.
@staticmethod
def ...
Back
__add__
Front
overloads the addition operator when dealing with a class
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return Point(x, y)
Back
Dictionary Comprehension
Front
fruits = ['apple', 'mango', 'banana','cherry']
# dict comprehension to create dict with fruit name as keys
d = {f:len(f) for f in fruits}
{'cherry': 6, 'mango': 5, 'apple': 5, 'banana': 6}