Python Flashcards

1
Q

Base Types

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Conversions

A

Can convert using the built in functions
int()
float()
bool()
str()
list()
dict()
set()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to use dicts, initialize, add, modify, delete, iterate, etc

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to use tuples, initialize, add, modify, delete, iterate, etc

A

Tuples are ordered and immuable but otherwise identical to lists
t = (1, 2, 3)
t = 1, 2, 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to use sets, initialize, add, modify, delete, iterate, etc

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to use lists, initialize, add, modify, delete, iterate, etc

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is python unpacking

A

* 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to work with strings? Concat, split, get character at, get length, search, etc

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to use modules

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Conditional Statements

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Define a class, have a public, protected, and private member

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Show how to use the 2 different kinds of loops

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are comprehensions and how to use them

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Use the range function

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Show how sequence indexing works

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Exception handling (catching and raising)

A
17
Q

How to define a function, distinguish between “regular” args and keyword args. what does *args, and **kwargs do

A
18
Q

How can you document a python function

A
19
Q

What is __name__ and how is it used in relation to the top level script

A
20
Q

Truthy/Falsy

A

By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object. Here are most of the built-in objects considered false:

  • constants defined to be false: None and False.
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • empty sequences and collections: ‘’, (), [], {}, set(), range(0)

Can use logical operators (and/or) which return the the value of an operand depending on shortcut evaluation

21
Q

Bit manipulation

A
22
Q

dunder methods

A
23
Q

== vs is

A

== compares value (__eq__) dunder method
is checks if they reference the same object