@@ -18,13 +18,30 @@ Key concepts introduced
18
18
19
19
Dictionaries are data structures which allow random access by a key (string, number, whatever). They are extremely common and powerful in Python.
20
20
21
- info = dict() # {} info[ 'age'] = 42 info[ 'loc'] = 'Italy' info = dict(age=42, loc='Italy') info = {'age': 42, 'loc': 'Italy'} location = info[ 'loc'] if 'age' in info: # use info[ 'age']
21
+ info = dict() # {}
22
+ info['age'] = 42
23
+ info['loc'] = 'Italy'
24
+
25
+ info = dict(age=42, loc='Italy')
26
+ info = {'age': 42, 'loc': 'Italy'}
27
+
28
+ location = info['loc']
29
+
30
+ if 'age' in info:
31
+ # use info['age']
22
32
23
33
** Lambdas**
24
34
25
35
Lambdas are small inline methods.
26
36
27
- def find_sig_nums(nums, predicate): for n in nums: if predicate(n): yield n numbers = [ 1, 1, 2, 3, 5, 8, 13, 21, 34] sig = find_sig_nums(numbers, lambda x: x % 2 == 1) # sig -> [ 1, 1, 3, 5, 13, 21]
37
+ def find_sig_nums(nums, predicate):
38
+ for n in nums:
39
+ if predicate(n):
40
+ yield n
41
+
42
+ numbers = [1, 1, 2, 3, 5, 8, 13, 21, 34]
43
+ sig = find_sig_nums(numbers, lambda x: x % 2 == 1)
44
+ # sig -> [1, 1, 3, 5, 13, 21]
28
45
29
46
** CSV File Parsing**
30
47
@@ -40,18 +57,40 @@ Lambdas are small inline methods.
40
57
41
58
** py2 vs py3**
42
59
43
- try: import statistics # only Python 3.4.3+ except: # statitics_2_stand_in defines a mean method import statitics_2_stand_in as statistics
60
+ try:
61
+ import statistics # only Python 3.4.3+
62
+ except:
63
+ # statitics_2_stand_in defines a mean method
64
+ import statitics_2_stand_in as statistics
44
65
45
66
# Can use statistics.mean as needed
46
67
47
- numbers = [ 1, 6, 99, ..., 5] the_ave = statistics.mean(numbers)
68
+ numbers = [1, 6, 99, ..., 5]
69
+ the_ave = statistics.mean(numbers)
48
70
49
71
** List comprehensions**
50
72
51
- paying_usernames = [ u.name for u in get_active_customers() if u.last_purchase == today ]
52
- # paying_usernames is a list
73
+ paying_usernames = [
74
+ u.name
75
+ for u in get_active_customers()
76
+ if u.last_purchase == today
77
+ ]
78
+ # paying_usernames is a list
53
79
54
80
** Generator expressions**
55
81
56
- paying_usernames = ( u.name for u in get_active_customers() if u.last_purchase == today ) # paying_usernames is a generator
82
+ paying_usernames = (
83
+ u.name
84
+ for u in get_active_customers()
85
+ if u.last_purchase == today
86
+ )
87
+ # paying_usernames is a generator
88
+
89
+
90
+ ------------------
91
+
92
+ Note: To see finished code that outputs the expected
93
+ numbers, you can use this alternate branch:
57
94
95
+ App 9's [ program.py] ( https://github.com/mikeckennedy/python-jumpstart-course-demos/blob/app_9_matching_output/apps/09_real_estate_analyzer/final/program.py )
96
+ for this "you try" section
0 commit comments