Python Flashcards
Base Types
Everything is a class/object
Numeric Types — int, float, complex
integers have no limit
int 1234
int 0
int -192
int 0xF3
int 0b010
float 0.0
float 9.2
float -1.7e-6
Text Sequence type (str)
Boolean type (True/False)
Built in functions
Collections (tuple/list/set/dict)
Conversions
Can convert using the built in functions
int()
float()
bool()
str()
list()
dict()
set()
How to use dicts, initialize, add, modify, delete, iterate, etc
d = { 'x': 1, 'y': 2, ... } dict(x=1, y=2) d['x'] # 1 d['z'] # key error d.get('z', -1) # -1 d.items() # [('x', 1), ('y', 2)] d.keys() d.values() d['x'] = 99 # set value del d['x'] # delete entry in dict d.pop(<key>[, <default>]) # removes item and returns value or default d.popitem() # removes last added item and returns as a tuple d.update({'z': 3}). #merges with other dictionary (updating d) d.update([('z', 3)]). #adds the key value pairs (updating d)
As of 3.7, dictionaries are ordered
Keys must be immutable
How to use tuples, initialize, add, modify, delete, iterate, etc
Tuples are ordered and immuable but otherwise identical to lists
t = (1, 2, 3)
t = 1, 2, 3
How to use sets, initialize, add, modify, delete, iterate, etc
Set operations
Sets are unordered, elements are unique, mutable (but elements are immutable)
~~~
x = set(1, 2, 3)
x = {1, 2, 3}
x = set() # only way to make an empty set
1 in x #True
4 in x #False
x1 = {‘foo’, ‘bar’, ‘baz’}
x2 = {‘baz’, ‘qux’, ‘quux’}
x1 | x2 # Union: {‘baz’, ‘quux’, ‘qux’, ‘bar’, ‘foo’}
x1.union(x2) #same thing, though you could pass in any iterable here
x1 & x2 #Intersection: {‘baz’}
x1.intersection(x2) # same
x1 - x2 # Difference, {‘foo’, ‘bar’}
x1.difference(x2) # Same
x1 ^ x2 # Symmetric difference: {‘foo’, ‘qux’, ‘quux’, ‘bar’}
x1.symmetric_differrence(x2) # same
x1 |= x2 # x1 is now {‘baz’, ‘quux’, ‘qux’, ‘bar’, ‘foo’}
x1.update(x2) # same
…
x.add(<elem>) # Add a single immutable object
x.remove(<elem>) # remove or raise exception
x.discard(<elem>) # Same as remove but don't raise exception
x.clear() # Removes all elements</elem></elem></elem>
x = frozenset([‘foo’, ‘bar’, ‘baz’])
~~~
How to use lists, initialize, add, modify, delete, iterate, etc
Lists are ordered and mutable. Accessed by index (see sequence indexing)
~~~
a = [‘foo’, ‘bar’, ‘baz’, ‘qux’]
a = a + [‘blah’]. # concatenation
b = [1, 2] * 3 # [1, 2, 1, 2, 1, 2]
len(a)
min(a)
max(a)
a.append(3)
a.append([3, 4])]
a.extend(<iterable>) # Appends using an iterable
a.insert(<index>, <obj>) # inserts object into list pushing other values to the right
a.remove(<obj>) #Removes <obj> from the list
a.pop() #removes and returns the last value
a.pop(1) # removes and returns the value at index 1
~~~</obj></obj></obj></index></iterable>
What is python unpacking
* operator is an unpacking operator which works on any iterable
# Can be used to unpack into a function call def num_sum(a, b, c, d): ... something ... num_sum(1, 2, 3, 4) num_sum(*[1, 2, 3, 4]) # Or to initialize lists x1 = [1, 2] x2 = [3, 4] x = [*x1, *x2] # Or to assign multiple values from a list x = [1, 2, 3, 4] first, *middle, last = x first # 1 middle # [2, 3] last # 4 # Or to define functions with varying number of args def num_sum(*nums) num_sum(1, 2, 3, 4) # ** operator unpacks dictionaries num_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3} print(*num_dict) # a b c # Can merge dictionaries x = {**dict1, **dict2}
How to work with strings? Concat, split, get character at, get length, search, etc
How to use modules
Conditional Statements
Define a class, have a public, protected, and private member
Show how to use the 2 different kinds of loops
What are comprehensions and how to use them
Use the range function
Show how sequence indexing works
Slicing x[<start>:<end>:<step></step></end></start>
For any iterable (lists, tuples, strings, bytes)
~~~
x = [10, 20, 30, 40]
x[0] #10
x[1] #20
x[4] #List index out of range
x[-1] #40
x[1] = 100 #to modify
del x[1] to remove
len(x) # length of list
x[:-1] # [10, 20, 30]
x[1:-1] # [20, 30]
x[::2] # [10, 30]
x[::-1] # Will return a reversed list
x[:] # Shallow copy
x[1, 3] = [200, 300] #Replace part of list with something else
del x[1, 3] #delete part of a list