Professional Documents
Culture Documents
Lecture 2
Lecture 2
Lecture 2
1 / 27
Contents
1 Modules
2 Functions
3 Strings
4 Exceptions
5 Lists, Tuples, Dictionaries and Set
6 Counters
7 Truthiness
8 List Comprehensions
9 Automated Testing and assert
10 Generators
11 Randomness
12 Regular Expressions
13 Zip and Argument Unpacking
14 args and kwargs
15 Type Annotations
2 / 27
Modules
3 / 27
Functions
4 / 27
Strings
5 / 27
Exceptions
6 / 27
Lists
7 / 27
Tuples
8 / 27
Dictionaries
9 / 27
Defaultdict
10 / 27
Set
11 / 27
Counters
12 / 27
Truthiness
13 / 27
List Comprehensions
14 / 27
Automated Testing and assert
15 / 27
Generators
Generators are created with functions and the yield operator. For
example:
def generate range(n):
i = 0
while i < n:
yield i
i += 1
The following loop will consume the yielded values one at a time
until none are left:
for i in generate range(10):
print(f"i:{i}")
for comprehensions wrapped in parentheses is also used to
generate generators.
evens below 20 = (i for i in generate range(20) if
i % 2 == 0)
An enumerate function turns values into pairs, i.e. (index,value).
16 / 27
Randomness
17 / 27
Randomness Cont...
18 / 27
Regular Expressions
19 / 27
Zip and Argument Unpacking
20 / 27
args and kwargs
21 / 27
args and kwargs cont...
22 / 27
Type Annotations
23 / 27
Type Annotations Cont...
24 / 27
How to Write Type Annotations
25 / 27
References
[1] Data Science from Scratch Joel Grus, Shroff/O’reilly, Second Edition
26 / 27
Thank You
Any Questions?
27 / 27