Practice Flashcards

1
Q

Remove an item from a list

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

Reverse list

A

.reverse() in place, only for list

reversed() returns iterator
(if the input list changes iterator sees it)

[::-1] reversed copy, slow

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

Sequence Functions

A

len()
min()
max()

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

Functions that return iterator

A

reversed(), enumerate(), zip(), map(), filter()

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

Generator Usage

A

infinite sequence

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

Map

A

map(function, iterables)
transformation function
lambda function (don’t use!)

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

Filter

A

map(function, iterable)
map(None, iterable) True elements
lambda function (don’t use!)

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

Generator expression without ()

A

list(square(x) for x in numbers)

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

Sum

A

sum(iterable, start)
start = 0
can’t sum strings

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

Zip

A

zip(*iterables, strict=False)

strict=True raises ValueError if iterables have different length

dict(zip(keys, values))

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

Enumerate

A

enumerate(iterable, start=0)

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

Dictionary update()

A

dict.update(iterable or kwargs)
iterable: dict, iterable of tuples etc.

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

Sort

A

sort(*, key=None, reverse=False)
list.sort()

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

Max, Min

A

max(iterable, *, key=None)
max(iterable, *, default, key=None)
max(arg1, arg2, *args, key=None)

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

All, Any

A

all(iterable)
True if iterable is empty

any(iterable)
False if iterable is empty

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

Is Instance

A

isinstance(object, classinfo)
classinfo - type, tuple of types

17
Q

Sorted

A

sorted(iterable, /, *, key=None, reverse=False)
returns a list

18
Q

Dictionary Creation

A
  • dict(dict1)
  • dict(iterable of (key, value))
  • merge: dict1 | dict2
  • merge into a literal: {…, **dict1, …}
19
Q

Is Subclass

A

issubclass(class, classinfo)
classinfo - class, tuple of classes

is class a subclass of classinfo

20
Q

Size

A
import sys

sys.getsizeof(obj)
21
Q

Closure Usage

A
  • function factories
  • callbacks
  • data encapsulation
  • decorators
  • memoization
22
Q

Raw String Literal

A

r'<string>'

treats backlslashes as literal characters

23
Q

Repr

A

repr(object)

returns a string containing a printable representation of an object

__repr__()

24
Q

Import

A

import module
import module as name
from module import var1, var2
from module import var1 as name1, var2 as name2