-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpythonintro.py
More file actions
417 lines (256 loc) · 9.02 KB
/
pythonintro.py
File metadata and controls
417 lines (256 loc) · 9.02 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# Welcome! This is a Python program file for pcDuino Lubuntu
# 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 pcDuino")
#Go!
# 2. This is a variable
message = "Go to Level Two"
print(message)
# 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 = 456
c = a + b
print("a=%s" % (a))
print("b=%s" % (b))
print("b=%s" % (c))
# 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=%s" % (a)) # think - should this be 123 or 100?
c = 50
print("a=%s" % (c)) # think - should this be 50 or 777?
d = 10 + a - c
print("a=%s" % (d)) # think - what should this be now?
# GO!
# 7. You can also use '+' to add together two strings
greeting = 'Hi '
name = 'pcDuino' # 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 = 18 # 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 = 19 # 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
pcDuino_is_fun = True
pcDuino_expensive = False
# We can also compare two variables using ==
pcDuino_age = 15
pcDuinoer_age = 20 # fill in your age
print(pcDuino_age == pcDuinoer_age) # this prints either True or False
# GO!
# 11. We can use less than and greater than too - these are < and >
pcDuino_is_older = pcDuino_age > pcDuinoer_age
print(pcDuino_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'
pcDuino = 49
pcDuinoes = 3 * pcDuino
total_cost = total_cost + pcDuino
if total_cost <= money:
message = "You have enough money for 3 pcDuinoes as well"
else:
message = "You can't afford 3 pcDuinoes"
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)
print 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
print main_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
print all_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)
print colour_set
# 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 = ['sky', 'young', 'Jim', 'darren', 'pillar', 'own', 'judy', 'alvin']
# 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 = 'Young sky own jim darren'
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]
print first_letter
print last_letter
print first_three
print last_three
print middle
# 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 young"
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
parts_long = my_long_sentence.split(',')
print parts_long
# 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 = ('Own', 26)
print(person[0] + ' is ' + str(person[1]) + ' years old')
# GO!
# (name, age)
students = [
('Sky', 19),
('Young', 18),
('Jim', 14),
('Darren', 10),
('Own', 7)
]
# Now write a loop to print each of the students' names and age
for name,age in students:
print name,age
print "-------------------------"
# 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
number = 11
for name,age in students:
if age > number:
print name,age
print "**********************"
# GO!
# 22. Another useful data structure is a dictionary
# Dictionaries contain key-value pairs like an address book maps name
# to number
addresses = {
'Darren': '0161 5673 890',
'Jim': '0115 8901 165',
'Cherish': '0114 2290 542',
'Jarek': '999'
}
# You access dictionary elements by looking them up with the key:
print(addresses['Jim'])
# You can check if a key or value exists in a given dictionary:
print('Jim' in addresses) # [False]
print('Cherish' 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 Cherish's phone number to a new number
addresses['Cherish'] = '0115 236 359'
print(addresses['Cherish'])
# GO!
# Delete Jarek from the dictinary
print('Jarek' in addresses) # [True]
del addresses['Jarek']
print('Jarek' 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. A final challenge using the skills you've learned:
# 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
#sum(),range()
print sum(range(1001))
#reduce(),lambda(),range()
print reduce(lambda x,y:x+y,range(1,1001))