Practice Flashcards

(34 cards)

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

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

filter(function, iterable)
filter(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

25
Add
**x = x + 1** – .__add__() **x += 1** – .__iadd__()
26
NamedTuple
namedtuple(typename, field_names, *, rename=False, defaults=None, module=None) returns a new tuple subclass named *typename* filednames – `['x, 'y']` or `'x y'` or `'x, y'` ``` from collections import namedtuple Point = namedtuple('Point', ['x', 'y']) p = Point(11, y=22) print(p.x, p[1]) ``` ._fields ._make(iterable) ._asdict() ._replace(**kwargs)
27
Callable
callable(object) True if object appears callable, False if not
28
Iterator Chain
one element at a time chain = iter2(iter1(...))
29
str() vs repr()
str() for user repr() for developer
30
Walrus Operator
NAME := expr assignment expression Python 3.8 PEP 572
31
Send
generator.send(value) resumes generator and sends value to yield expression returns next yielded value, or raises StopIteration if done two-way communication next() with value coroutines, event-driven flows, stateful processes
32
Throw
generator.throw(value) raises exception inside generator
33
Close
generator.close() raises GeneratorExit inside generator at last yield returns value or None ends generator gracefully
34
yield from
generator delegations delegates part of operations to another generator subgenerator PEP 380