-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpython101_workshop_script.py
More file actions
287 lines (210 loc) · 5.88 KB
/
python101_workshop_script.py
File metadata and controls
287 lines (210 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Python 101 — In-class Practice Script
# You do NOT need to type everything from scratch.
# Run selected lines and edit values where you see TODO / EXERCISE.
# Tip: You don't need to memorize syntax
# Focus on patterns and practice by editing values and re-running code.
# ============================================================
# Core basics: variables, comments, print(), type()
# ============================================================
# A '#' starts a comment. Python ignores comments.
text = "foo" # TODO: change this string
number = 42 # TODO: change this number
pi_value = 3.1415 # TODO: change this value
print("text =", text)
print("number =", number)
print("pi_value =", pi_value)
print("type(text) ->", type(text))
print("type(number) ->", type(number))
print("type(pi_value) ->", type(pi_value))
# Numbers vs strings
print(type(10))
print(type(10.0))
print(type("10.0"))
# Operators
print(6 * 7)
print(2 ** 4)
print(13 % 5)
print(3 > 4)
print(True and True)
print(True or False)
print(True == False)
# ============================================================
# EXERCISE 1: Operators
# ============================================================
# TODO:
# 1) Change x and y
# 2) Predict the output
# 3) Re-run this block
x = 13
y = 5
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)
print(x ** y)
# ============================================================
# Lists, indexing, slicing, append
# ============================================================
numbers = [1, 2, 3] # TODO: change list values
print(numbers)
print(numbers[0])
print(numbers[-1])
# Loop through a list
for n in numbers:
print(n)
# Slicing
a = [1, 2, 3, 4, 5]
print(a[1:3])
print(a[:2])
print(a[2:])
# Append (a list method)
numbers.append(4) # TODO: append something else
print(numbers)
# ============================================================
# EXERCISE 2: Lists
# ============================================================
# TODO:
# 1) Change my_list
# 2) Change slicing range
# 3) Append a STRING
my_list = [10, 20, 30, 40, 50]
print(my_list)
start = 1
end = 4
print(my_list[start:end])
my_list.append("NEW")
print(my_list)
# ============================================================
# Loops, tuples, dictionaries
# ============================================================
values = ["foo", "bar", "baz"]
for val in values:
print(val)
# enumerate gives index + value
for idx, val in enumerate(values):
print(idx, val)
print(str(idx)+"="+val)
# Tuples (immutable)
a_tuple = (1, 2, 3)
another_tuple = ("blue", "green", "red")
print(a_tuple[0])
print(another_tuple.index("green"))
# Dictionaries
translation = {"one": "first", "two": "second"}
print(translation['one'])
for key, value in translation.items():
print(key, value)
for key in translation.keys():
print(key, '->', translation[key])
# ============================================================
# EXERCISE 3: Object types
# ============================================================
# TODO:
# 1) Create a list OR tuple OR dictionary
# 2) Print the first element/value
# 3) Loop through it
ex_list = [1, 2, 3] # TODO: edit or replace
print(ex_list[0])
for item in ex_list:
print(item)
# ============================================================
# Functions and help()
# ============================================================
def add_function(a, b):
"""Return the sum of a and b."""
return a + b
print(add_function(5, 10))
# TODO (optional):
# Try changing the function body or inputs above.
# Try help(add_function)
# ============================================================
# Conditionals: if / elif / else
# ============================================================
a = 5
if a == 5:
print('A equals 5')
a = 6
if a == 5:
print('A equals 5')
else:
print('A is not equal to 5')
a = 'foo'
b = 'bar'
if a == 'foo':
if b == 'bar':
print('OK this is cool!')
else:
print('All foo no bar')
a = 7
if a > 5 and a < 10:
print('A is between 5 and 10')
a = 15
b = 5
c = 20
if a > b or a < c:
print(f'A is between {b} and {c}')
a = 5
b = 10
if not a > b:
print('A is not greater than B')
# ============================================================
# EXERCISE 4: Conditions
# ============================================================
# TODO:
# 1) Change a to 3
# 2) Change score to 60
# 3) Predict what will print, then re-run
a = 7
if a > 5 and a < 10:
print("A is between 5 and 10")
else:
print("A is NOT between 5 and 10")
score = 93
if score >= 90:
print("grade: A")
elif score >= 80:
print("grade: B")
else:
print("grade: C or below")
# ============================================================
# Shorthanded methods
# ============================================================
a = 5
if a != 10: print('A is not 10')
# TODO:
# Change the value of a to 10.
# What happens? Why?
a = 5
b = 10
print('A') if not a < b else print('B')
# TODO:
# Try changing a and b.
# Example:
# a = 20, b = 10
# a = 5, b = 3
# Before running, predict which letter will be printed.
# ============================================================
# Nested, breaking and shorthanding loops
# ============================================================
for i in range(4):
print('\n')
for j in range(3):
print(f'i = {i} j = {j}')
# TODO:
# Change range(4) or range(3) to a different number.
# How does the output change?
states = ['Colorado', 'Florida', 'Hawaii', 'California', 'Alaska']
for state in states:
if state == 'Colorado':
break
print(state)
# TODO:
# Move 'Colorado' to a different position in the list.
# What gets printed now?
# What happens if you remove the 'break' line?
mylist = [1, 'One', 'Two', 2, 'three', 3.14]
[print(x) for x in mylist]
# TODO:
# Replace print(x) with type(x).
# What do you notice about the different elements?