Fundamentals Flashcards

(54 cards)

1
Q

List common functions of the random module.

A

> import random

  • random.random()
  • random.randrange(upper_bound)
  • random.randrange(lower_bound, upper_bound)
  • random.shuffle(xs)
  • random.choice(xs)
  • random.sample(xs, n)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Name all boolean operators

A

and, or, not, is, ==

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

Name all values that evaluate to false in predicates.

A
  • False
  • None
  • 0 and 0.0
  • empty list
  • empty set
  • empty dict
  • empty str
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to test all boolean values in a collection xs?

A

any(xs) or all(xs)

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

How to sort a collection xs?

A

sorted(x)

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

Parameters for sorted.

A

sorted(xs, key=function, reverse=True)

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

How to find the extrema in a collection xs?

A

max(xs) and min(xs)

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

How to add indexes to elements of a collection xs?

A

enumerate(xs)

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

Demonstrate list comprehensions.

A

[x * y for x in xs

for y in ys

if x % y == 0]

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

Demonstrate a dict comprehension.

A

{k: v for (k, v) in zip(xs, ys)}

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

Demonstrate a set comprehension.

A

{x for x in xs}

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

List the 5 most common built-in collections

A
  • tuple
  • list
  • set
  • dict
  • (str)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to test if an object x is contained in a collection xs?

A

x in xs

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

List 4 commonly used non-standard collections.

A
  • defaultdict(factory_function)
  • dequeue
  • Counter
  • OrderedDict
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to get the number of elements in a collection xs?

A

len(xs)

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

Get the 3rd up to and including the 9th element of a list xs.

A

xs[3:10]

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

Unpack a list (or tuple) literal with 3 elements to:

  1. three variables a, b and c
  2. two variables a and b
A
  1. > a, b, c = [1, 2, 3]
  2. > a, *b = [1, 2, 3]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Methods to add and sort list objects.

A
  • extend(another_list)
  • add(an_element)
  • sort()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Methods to add and remove elements from the set xs.

A
  • xs.add(element)
  • xs.discard(element)
  • xs.remove(element)

remove raises KeyError if the value not present

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

Methods to access the contents of a dict xs.

A
  • xs.get(key, default_value)
  • xs.keys()
  • xs.values()
  • xs.items()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

In which module can you find the defaultdict and Counter classes?

A

In the collections module.

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

How to get the sum of all elements in a collection xs?

23
Q

How can you catch exceptions?

A

try:

x * 3

except NameError as e:

print(‘error: ‘ + str(e))

24
Q

Demonstrate “if then else”.

A

if predicate_a:

<some></some>

elif predicate_b:

<some></some>

else:

<some></some>

<variable> = <some> <strong>if</strong> <em>predicate</em> <strong>else</strong> <some></some></some></variable>

25
Demonstrate looping constructs and all relevant keywords.
**for** x **in** xs: *pass* **while** *predicate*: * pass* keywords: **break**, **continue**
26
Name 4 common higher-order functions.
* **map** * **filter** * functools.**reduce** * functools.**partial**
27
Demonstrate the var arg syntax.
**def** f(***\*args***): **for** arg **in** args: **pass**
28
How to declare keyword arguments?
**def** my\_function(***\*\*kwargs***): **for** (k, args) **in** args.items(): **pass**
29
Demonstrate a *generator expression*.
**(**x **for** x **in** xs**)**
30
How to (auto-) reload modules in IPyhton?
1. %load\_ext autoreload 2. %autoreload 2
31
Magic command to enable timing in IPython?
%timeit
32
IPython magic command to reset the shell?
%reset
33
IPython magic command to load an extension? (e.g. autoreload)
%load\_ext autoreload
34
Create a new Python3 project.
1. \> pip3 install --user pipenv 2. \> pipenv install 3. \> pipenv shell 4. *... start writing code*
35
How to reload a module in the REPL shell?
1. \> import importlib 2. \> importlib.reload(module)
36
List 3 (4) different string literals.
* *Raw:* r'hello, hello' * *Multiline:* '''a lot of lines...''' * *Normal:* 'a string' or "a string"
37
List all currently imported modules.
1. \> import sys 2. \> sys.modules
38
The Zen of Python
1. Beautiful is better than ugly. 2. Explicit is better than implicit. 3. Simple is better than complex. 4. Complex is better than complicated. 5. Flat is better than nested.
39
List 4 common mutable types.
* **list** * **set** * **dict** * **bytearray**
40
Name 10 common immutable types.
1. **bool** 2. **int** 3. **float** 4. **decimal** 5. **complex** 6. **range** 7. **tuple** 8. **bytes** 9. **str** 10. **frozenset**
41
String methods to change cases?
* .title() * .capitalize() * .upper() * .lower() * .swapcase()
42
3 String methods that change alignment?
* .center() * .ljust() * .rjust()
43
Method to convert a string into bytes?
.**encode**(encoding='utf-8')
44
Common ***set*** methods?
* **&**.intersection() * **|**.union() * **-**.difference() * **^**.symmetric\_difference() * **\<=**.issubset() / **\<** * **\>=**.issuperset / **\>**
45
What is a decorator?
A *function* that takes a single *function as a parameter* and *returns another function*.
46
What is the special *syntax* for a *decorator*?
@my\_decorator def f(x): pass
47
Difference between ***is*** and **==**?
* ***is*** tests if two variables point to the same object * **==** test if two variables have the same values
48
How are new exceptions defined?
**class** ZonkError(***Exception***): **pass**
49
How can you *change* a **global variable** in a local context?
def f(): global my\_var my\_var = 'new value'
50
Which *statements* can be followed by an **else** statement?
* if / elif * for (i.e. no break) * while (i.e. no break) * except
51
When are the *expressions* given for **default arguments** *evaluated*?
During ***function definition.*** NOT when the function is called!
52
What is a **module**?
A file of Python code.
53
What is a **package**?
A **hierarchy** of *files* and *directories* containing a *\_\_init\_\_.py* file.
54
Name 3 function from the ***itertools*** module.
* chain(xs, ys...) * .cycle(xs) * .accumulate(xs, f)