/
intro.py
377 lines (232 loc) · 8.35 KB
/
intro.py
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# Welcome! This is a Python program file
# The lines that start with a hash (#) are comments
# They are for you to read and are ignored by Python
# When you see 'GO!', save and run the file to see the output
# When you see a line starting with # follow the instructions
# Some lines are python code with a # in front
# This means they're commented out - remove the # to uncomment
# Do one challenge at a time, save and run after each one!
# 1. This is the print statement
print("Hello world")
# GO!
# 2. This is a variable
message = "Level Two"
# Add a line below to print this variable
# GO!
# 3. The variable above is called a string
# You can use single or double quotes (but must close them)
# You can ask Python what type a variable is. Try uncommenting the next line:
# print(type(message))
# GO!
# 4. Another type of variable is an integer (a whole number)
a = 123
b = 654
c = a + b
# Try printing the value of c below to see the answer
# GO!
# 5. You can use other operators like subtract (-) and multiply (*)
# Try some below by replacing the word with the correct operator
# a times b
# b minus a
# 12 times 4
# 103 add 999
# GO!
# 6. Variables keep their value until you change it
a = 100
# print(a) # think - should this be 123 or 100?
c = 50
# print(c) # think - should this be 50 or 777?
d = 10 + a - c
# print(d) # think - what should this be now?
# GO!
# 7. You can also use '+' to add together two strings
greeting = 'Hi '
name = '' # enter your name in this string
message = greeting + name
# print(message)
# GO!
# 8. Try adding a number and a string together and you get an error:
# age = # enter your age here (as a number)
# print(name + ' is ' + age + ' years old')
# GO!
# See the error? You can't mix types like that.
# But see how it tells you which line was the error?
# Now comment out that line so there is no error
# 9. We can convert numbers to strings like this:
# print(name + ' is ' + str(age) + ' years old')
# GO!
# No error this time, I hope?
# Or we could just make sure we enter it as a string:
# age = # enter your age here, as a string
# print(name + ' is ' + age + ' years old')
# GO!
# No error this time, I hope?
# 10. Another variable type is called a boolean
# This means either True or False
raspberry_pi_is_fun = True
raspberry_pi_is_expensive = False
# We can also compare two variables using ==
bobs_age = 15
# your_age = # fill in your age
# print(your_age == bobs_age) # this prints either True or False
# GO!
# 11. We can use less than and greater than too - these are < and >
# bob_is_older = bobs_age > your_age
# print(bob_is_older) # do you expect True or False?
# GO!
# 12. We can ask questions before printing with an if statement
money = 500
phone_cost = 240
tablet_cost = 200
total_cost = phone_cost + tablet_cost
can_afford_both = money > total_cost
if can_afford_both:
message = "You have enough money for both"
else:
message = "You can't afford both devices"
# print(message) # what do you expect to see here?
# GO!
# Now change the value of tablet_cost to 260 and run it again
# What should the message be this time?
# GO!
# Is this right? You might need to change the comparison operator to >=
# This means 'greater than or equal to'
raspberry_pi = 25
pies = 3 * raspberry_pi
total_cost = total_cost + pies
if total_cost <= money:
message = "You have enough money for 3 raspberry pies as well"
else:
message = "You can't afford 3 raspberry pies"
# print(message) # what do you expect to see here?
# GO!
# 13. You can keep many items in a type of variable called a list
colours = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet']
# You can check whether a colour is in the list
# print('Black' in colours) # Prints True or False
# GO!
# You can add to the list with append
colours.append('Black')
colours.append('White')
# print('Black' in colours) # Should this be different now?
# GO!
# You can add a list to a list with extend
more_colours = ['Gray', 'Navy', 'Pink']
colours.extend(more_colours)
# Try printing the list to see what's in it
# GO!
# 14. You can add two lists together in to a new list using +
primary_colours = ['Red', 'Blue', 'Yellow']
secondary_colours = ['Purple', 'Orange', 'Green']
main_colours = primary_colours + secondary_colours
# Try printing main_colours
# 15. You can find how many there are by using len(your_list). Try it below
# How many colours are there in main_colours?
# GO!
all_colours = colours + main_colours
# How many colours are there in all_colours?
# Do it here. Try to think what you expect before you run it
# GO!
# Did you get what you expected? If not, why not?
# 16. You can make sure you don't have duplicates by adding to a set
even_numbers = [2, 4, 6, 8, 10, 12]
multiples_of_three = [3, 6, 9, 12]
numbers = even_numbers + multiples_of_three
# print(numbers, len(numbers))
numbers_set = set(numbers)
# print(numbers_set, len(numbers_set))
# GO!
colour_set = set(all_colours)
# How many colours do you expect to be in this time?
# Do you expect the same or not? Think about it first
# 17. You can use a loop to look over all the items in a list
my_class = ['Sarah', 'Bob', 'Jim', 'Tom', 'Lucy', 'Sophie', 'Liz', 'Ed']
# Below is a multi-line comment
# Delete the ''' from before and after to uncomment the block
'''
for student in my_class:
print(student)
'''
# Add all the names of people in your group to this list
# Remember the difference between append and extend. You can use either.
# Now write a loop to print a number (starting from 1) before each name
# 18. You can split up a string by index
full_name = 'Dominic Adrian Smith'
first_letter = full_name[0]
last_letter = full_name[19]
first_three = full_name[:3] # [0:3 also works]
last_three = full_name[-3:] # [17:] and [17:20] also work
middle = full_name[8:14]
# Try printing these, and try to make a word out of the individual letters
# 19. You can also split the string on a specific character
my_sentence = "Hello, my name is Fred"
parts = my_sentence.split(',')
# print(parts)
# print(type(parts)) # What type is this variable? What can you do with it?
# GO!
my_long_sentence = "This is a very very very very very very long sentence"
# Now split the sentence and use this to print out the number of words
# GO! (Clues below if you're stuck)
# Clue: Which character do you split on to separate words?
# Clue: What type is the split variable?
# Clue: What can you do to count these?
# 20. You can group data together in a tuple
person = ('Bobby', 26)
# print(person[0] + ' is ' + str(person[1]) + ' years old')
# GO!
# (name, age)
students = [
('Dave', 12),
('Sophia', 13),
('Sam', 12),
('Kate', 11),
('Daniel', 10)
]
# Now write a loop to print each of the students' names and age
# GO!
# 21. Tuples can be any length. The above examples are 2-tuples.
# Try making a list of students with (name, age, favourite subject and sport)
# Now loop over them printing each one out
# Now pick a number (in the students' age range)
# Make the loop only print the students older than that number
# GO!
# 22. Another useful data structure is a dictionary
# Dictionaries contain key-value pairs like an address book maps name
# to number
addresses = {
'Lauren': '0161 5673 890',
'Amy': '0115 8901 165',
'Daniel': '0114 2290 542',
'Emergency': '999'
}
# You access dictionary elements by looking them up with the key:
# print(addresses['Amy'])
# You can check if a key or value exists in a given dictionary:
# print('David' in addresses) # [False]
# print('Daniel' in addresses) # [True]
# print('999' in addresses) # [False]
# print('999' in addresses.values()) # [True]
# print(999 in addresses.values()) # [False]
# GO!
# Note that 999 was entered in to the dictionary as a string, not an integer
# Think: what would happen if phone numbers were stored as integers?
# Try changing Amy's phone number to a new number
# addresses['Amy'] = '0115 236 359'
# print(addresses['Amy'])
# GO!
# Delete Daniel from the dictinary
# print('Daniel' in addresses) # [True]
# del addresses['Daniel']
# print('Daniel' in addresses) # [False]
# GO!
# You can also loop over a dictionary and access its contents:
'''
for name in addresses:
print(name, addresses[name])
'''
# GO!
# 23. What is the sum of all the digits in all the numbers from 1 to 1000?
# GO!
# Clue: range(10) => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Clue: str(87) => '87'
# Clue: int('9') => 9