Python3 Flashcards

1
Q

Give an example of type-hinting

A

def some_func(nums: List(int), val: int) -> int:

type hinting, afaik, is only for method params and returns, not variables

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

1-liner to iterate over a list (actually, any iterable like list, tuple, string), with both an index and value

A

for idx, val in enumerate(iterable):

Fun: for idx, val in enumerate(iterable, start = 2):
THIS WILL CHOOSE THE INDEX TO START AT, NOT THE ITEM TO START AT (e.g. if you choose 1, the result will be 1-indexed)

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

For debugging, use this trick to see everything in an iterator

A

print(list(iterator))

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

How would you use the python repl to see if a type is an iterator?

A

if we want to see if enumerate(nums) is an iterator, we need to see if its type has the ‘__iter__’ and ‘__next__’ method.

hasattr(enumerate(nums), ‘__iter__’) and hasattr(enumerate(nums), ‘__next__’) == True

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

How do you filter a list?

A

filtered_list = list(filter(lambda : somefunc(), your_list))

make sure to do list(filter()) lest you wind up with a “filter object” which seems to be a one time iterable (so if you iterate over it again, you’re gonna have a problem)

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

python3 get the max int

A

import sys

sys.maxsize

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

turn a string into a list

A

list(some_str)

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

type hinting for a list

A

from typing import List

def some_func(strs: List[str]) -> List[str]:

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

construct a list using a list comprehension

A

https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.

For example, assume we want to create a list of squares, like:

>>>
>>> squares = []
>>> for x in range(10):
...     squares.append(x**2)
...
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Note that this creates (or overwrites) a variable named x that still exists after the loop completes. We can calculate the list of squares without any side effects using:

squares = list(map(lambda x: x**2, range(10)))
or, equivalently:

squares = [x**2 for x in range(10)]
which is more concise and readable.

A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it. For example, this listcomp combines the elements of two lists if they are not equal:

> > > [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
and it’s equivalent to:

>>>
>>> combs = []
>>> for x in [1,2,3]:
...     for y in [3,1,4]:
...         if x != y:
...             combs.append((x, y))
...
>>> combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
Note how the order of the for and if statements is the same in both these snippets.

If the expression is a tuple (e.g. the (x, y) in the previous example), it must be parenthesized.

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

The best way to create the 4 directions in a matrix

A

TODO

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

the best way to create the 8 directions (including diagonals) in a matrix

A

TODO

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

TIP: BE VERY CAREFUL ABOUT _____ when nesting loops

A

indentation!!!
if you can , pull out functionality into helper functions to prevent issues in indentation, especially if you reset variables at the END of a loop`

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

How is a list implemented under the hood? What does this mean about its common operations?

A

todo

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

How can you get classic C-like array behavior from python (including allocating an array and constant time lookup once it’s allocated)

A

todo

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

What are the built in sort operations in python? (both in-place and make-a-copy)

A

todo

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

How is sort implemented in python under the hood?

A

todo

17
Q

How do you use a different kind of sort in python? Is it an override? A different import?

A

todo

18
Q

What is the performance of len(List)? Why?

A

todo

19
Q

What are some common immutable types?

A

string, int, etc.

NOTE: is it POSSIBLE to access the pass-by-reference of these? If so, how? If not, are there workarounds?

20
Q

What are some common mutable types?

A

list, etc.

21
Q

Review your type-hinting cheat sheet

A

https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html

22
Q

Does type hinting throw errors for wrong types?

A

NO. This is an explicit nongoal from the PEP standard. Type hinting can help with certain static analyzers, but I’m not sure which tools are available for this.

You must still validate!!

23
Q

Do python lists have constant time access like arrays do?

A

todo

24
Q
class Node:
  def \_\_init\_\_(self, val):
    self.val = val
    self.next = None
def some_func(anode):
  anode.val = 15

What does the following code output given the definitions above? Why?

> > headA = Node(10)
some_func(headA)
print(headA.val)
ANS

A

15 will be printed because objects are passed by reference in python3. This means that objects are mutable within methods.

25
Q

define “argument”

A

the instance of a variable that is passed into a function at runtime

26
Q

define “parameter”

A

the representation of what kind of arguments a function/method takes. It is seen in the definition of a function/method.

27
Q

sum two lists using lambda

A

first = [1, 2, 3, 4, 5]
second = [6, 7, 8, 9, 10]
three = list(map(lambda x,y: x+y,first,second))
print(three)

Output
[7, 9, 11, 13, 15]