-
Notifications
You must be signed in to change notification settings - Fork 18
/
Exercise2.py
610 lines (515 loc) · 16.8 KB
/
Exercise2.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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# - Exercises2.py *- coding: utf-8 -*-
"""
Python has lists. The empty list is []. The following is a list of one
item ["a"] and so is [3]. Here is a list with 3 items ["ball",3.14,-2]. Let's
define a list, I'll call it lis and we'll do things with it to illustrate
accessing items in a list. Execute the following cell with Ctrl-Enter.
"""
#%%
lis = ["a","b","c","d","e","f"]
#%%
"""
Exercise:
Some of the things that we can do with lists. Let's try them together.
lis[0] is the first element of the list. (index is 0)
lis[1] is the second element of the list and so on. (index is 1)
The length of the list is len(lis) and is the number of items in the list.
lis[-1] is the last item in the list.
lis[2:4] will list items 2 and 3 (but not 4)
lis[:4] will list items 0, 1, 2, 3 (but not 4); that is all items up to 4
lis[3:] will list all items starting with item 3.
lis.append("g") will append "g" onto the end of the list
"a" in lis # running this statement will return True
"r" in lis # running this statement will return False
Everything in Python is an object, whether it is a variable like x or a list
like lis. Objects have methods indicated by the dot. So .append() is a method
of the list object. We'll see more of this.
"""
"""
Here is an example function using a list. We pass in a list of items and
it checks for certain animals or flowers in the list.
We'll try it out on several lists such as ['bear'], ['daisy', lion'], etc.
"""
#%%
def who_is_there(lis):
if "bear" in lis:
print("There's a bear.")
if "lion" in lis:
print("There's a lion.")
if "daisy" in lis or "iris" in lis:
print("There are flowers.")
if "daisy" in lis and "iris" in lis:
print("There are at least two flowers.")
if "donkey" in lis:
print("There is a donkey.")
if "horse" not in lis:
print("There is no horse in the list.")
print("The list has",len(lis), "items")
#%%
"""
You should make up some lists and pass to 'who_is_there' to see how the if
statements handle various combinations. Some test lists for who_is_there:
"""
#%%
alion = ['lion']
ld = ['lion','daisy']
lbf = ['lion','bear','iris']
#%%
"""
The following function illustrates using lists in 'for' loops. Note that the
loop variable 'let' steps through the list, alist, taking the value of each of
its items in turn.
"""
#%%
lis = ["a","b","c","d","e","f"]
lis1 = ["a","b","a","r","c","a","a"]
#%%
def count_a(alist):
ct = 0
for let in alist:
if let == 'a':
ct = ct + 1
print("There are",ct,"letter a's in the list.")
#%%
"""
Note there is a basic design pattern to these lists. Some variable for
accumulating the results (above it is ct) is initiated before entering the
loop. This variable is updated within the loop. Afterwards that variable is
used (in this case ct is printed out).
"""
"""
Exercise:
Take the following list, nlis, and compute its average. That is, write
a function 'average(numlis)' that uses a 'for' loop to sum up the numbers
in numlis and divide by the length of numlis. Just to be sure that you
got all the numbers in numlis, print each one in your 'for' loop and
print the length of the the list. When using a loop, one always needs to
be careful that it loops as often as is expected. In this case also print out
the number of items in the list.
Caution: Do NOT use the variable nlis in your function. This function should
work on any list of numbers. Just to be sure make sure that your function
(without any changes) works on rlis as well as nlis.
"""
#%%
nlis = [2,4,8,105,210,-3,47,8,33,1] # average should by 41.5
rlis = [3.14, 7.26, -4.76, 0, 8.24, 9.1, -100.7, 4] # average is -9.215
#%%
# some tests for your function. Be sure your function works for these
average(nlis)
average(rlis)
#%%
"""
Solution:
"""
#%%
def average(numlis):
totalSum = 0;
length = len(numlis);
for num in numlis:
totalSum += num;
avg = totalSum / length;
print("The average is:",avg);
#%%
"""
End solution
"""
"""
Let me emphasize that you can make a 'for' loop with just a list.
One can simply step through a list to form the loop.
In this example case it is a list of states and we will simply be stepping
through the loop and printing out the states.
"""
#%%
newEngland = ["Maine","New Hampshire","Vermont", "Rhodes Island",
"Massachusetts","Connecticut"]
def for_state(slis):
for state in slis:
print(state)
#%%
"""
Keep in mind that a 'for' loop can step through various kinds of iterators.
"""
"""
Exercise:
Write a function 'print_list(lis)' that prints items of the list lis. Test it
by running the three tests that I give here. This requires writing a function
that includes a loop like the one above, but uses lis for the iterator. Inside
your function you should use lis to represent the list. If you do so, your
function should pass all three tests below.
"""
#%%
letter_list = ['a', 'b', 'c']
cap_list = ['A', 'B', 'C', 'D']
misc_list = ['ball', 3.14, -50, 'university', "course"]
#%%
"""
Solution:
"""
#%%
def print_list(lis):
for item in lis:
print(item)
#%%
"""
End solution
"""
"""
Lets' talk about data types. For starters Python has integers (e.g., 40), float
or real numbers (e.g., 40.0), string ("hello"), list ( ['a','b','c']), bool
(boolean -- that is, True or False). In Python they are called int, float,
str, list, bool. You can tell what type a variable x is by entering type(x).
Here is an example of several:
"""
#%%
x = 17 # integer
y = 3.14 # float
z = "The Walrus and the Carpenter" # string
z1 = "30" # string
z2 = '40' # string
vowels = ['a','e','i','o','u'] # list of strings
nums = ['1','2','3', '3.14'] # list of strings
phrases = ["’Twas brillig, and the slithy toves",
"Did gyre and gimble in the wabe:"] # list of strings (2 strings, in fact)
r = True # boolean
s = False # boolean
#%%
"""
Often you can convert one type to another: int(z1) makes and returns an
integer (30); float(z2) returns a float or real number (40.0); str(y) returns
the string "3.14"; etc.
This is important because z1+z2 is not 70 (it is '3040'); however,
int(z1)+int(z2) is 70. Here is a simple program showing when you might want to
use this technique.
"""
#%%
def multiply():
numstr1 = input("Enter a number: ")
numstr2 = input("Enter another number: ")
num1 = float(numstr1)
num2 = float(numstr2)
print("Their sum is ", num1 * num2)
# print("Won't work: ", numstr1 * numstr2)
#%%
"""
Compare list(range(2,20,3)) and range(2,20,3). The first one is a list and
the second one is what Python calls an iterator. The second one dishes out
the next element in the list each time it is called. This is one of the changes
from Python 2 to Python 3. In Python 2 it was a list and there was a function
xrange() for iterating without building the list. That is gone from Python 3.
Can you think of a reason that using range in Python 2 might not be a good idea
with huge lists?
"""
#%%
print(list(range(2,20,3)))
print(range(2,20,3))
#%%
"""
Caution: Notice that large numbers never include commas. Compare these two
examples. In the second, Python thinks that it is printing 3 numbers not 1.
"""
print(12345678)
print(12,345,678)
#%%
"""
Another caution. The following are Python keywords. They have special meaning
and shouldn't be used as variable names:
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try
You'll just get a syntax error:
"""
#%%
except = 5
#%%
"""
Note: for readability, if you feel that you need to use one of these as a
variable, you could use an underscore after it. For example, and_, class_, etc.
That makes it different from the Python keyword.
"""
#%%
newEngland = ["Maine","New Hampshire","Vermont", "Rhodes Island",
"Massachusetts","Connecticut"]
def for_state(state_list):
for state in state_list:
print(state)
#%%
"""
Let's print a small report. Here is a list of New England states and
their populations. We'll print this as a table or report. Essentially, this
is like the little function above, except that we need to handle the variables
in a more sophisticated way.
"""
#%%
newEngland = [["Massachusetts",6692824],["Connecticut",3596080],
["Maine",1328302],["New Hampshire",1323459],
["Rhode Island",1051511],["Vermont",626630]]
#%%
"""
Exercise:
Before writing the function, let's understand this list of lists better.
Try this out.
What is the first item of newEngland? (i.e., the one of index 0)
What is the second item?
What is the name of the state in the second element? How do we get that?
What is the population of the state in the second element?
"""
"""
Solution:
"""
#%%
newEngland[0];
newEngland[1];
newEngland[1][0];
newEngland[1][1];
#%%
"""
End solution
"""
#%%
newEngland = [["Massachusetts",6692824],["Connecticut",3596080],
["Maine",1328302],["New Hampshire",1323459],
["Rhode Island",1051511],["Vermont",626630]]
def report1(state_data):
""" prints population report """
print("Population State")
for state_item in state_data:
print(state_item[1], " ", state_item[0])
#%%
"""
Note: that because we pass the list into the function by way of the argument
state_data, the above works on the following mid-Atlantic list. Execute the
following cell to define midAtlantic in IPython and try it:
"""
#%%
midAtlantic = [["New York",19746227],["New Jersey",8938175],
["Pennsylvania",12787209]]
#%%
"""
Note that we don't use 19,746,227 as the population of New York. Why?
"""
"""
Another way to do it.
"""
#%%
newEngland = [["Massachusetts",6692824],["Connecticut",3596080],
["Maine",1328302],["New Hampshire",1323459],
["Rhode Island",1051511],["Vermont",626630]]
def report2(state_data):
""" prints population report """
print("Population State")
for i in range(0,len(state_data)):
print(state_data[i][1], " ", state_data[i][0])
#%%
"""
Find the sum of the populations of the New England states. Print
out how many there are. Use a basic loop design pattern.
"""
#%%
newEngland = [["Massachusetts",6692824],["Connecticut",3596080],
["Maine",1328302],["New Hampshire",1323459],
["Rhode Island",1051511],["Vermont",626630]]
def population(state_data):
""" Sums state populations """
sum_ = 0
num_states = len(state_data)
for i in range(0,num_states):
one_state = state_data[i]
pop = one_state[1]
sum_ = sum_ + pop
print("The total population of this list of states is",sum_)
print("There are",num_states,"states in this list of states.")
#%%
"""
Version using more syntactic sugar -- the variables have better and more
meaningful names. This may read better in a bigger program.
"""
#%%
def population(state_data):
""" Sums state populations """
population = 1
sum_ = 0
num_states = len(state_data)
for state in range(0,num_states):
sum_ = sum_ + state_data[state][population]
print("The total population of this list of states is",sum_)
print("There are",num_states,"states in this list of states.")
#%%
"""
Exercise:
Write a function 'average(nlis)' that uses a 'for' loop and 'range()' to sum up
the numbers in nlis and divide by the length of nlis. Just to be sure that you
have used all the numbers in nlis, print each one in your 'for' loop and print
the length of the list. Do not use the variable numlis in your function! If you
change to a different list will it work? For numlis, the output should look
like:
65 44 3 56 48 74 7 97 95 42
the average is 53.1
"""
#%%
numlis = [65, 44, 3, 56, 48, 74, 7, 97, 95, 42] # test on this list
numlis2 = [4,6,8,12,2,7,19] # test on a second list to be sure
#%%
"""
Solution Starter:
"""
#%%
def average(nlis):
pass # delete this and enter your code starting here
#%%
"""
End solution
"""
"""
Libraries. Python is a "small" language in the sense that many tools
that are available are not automatically included when you run it. Many of
these tools are in modules called libaries and can be loaded into your program
only when you need them, keeping your programs smaller when they aren't needed.
A typical way of doing that is
import random
which will load the library named random.
"""
#%%
import random
#%%
# Each run of the following gives a different random number between 0 and 1
print(random.random())
#%%
# Each run of the following gives a different random integer between 3 and 8
print(random.randint(3,8))
#%%
"""
The following example builds a sentence using various parts of speech.
It randomly chooses words from a list by using random.choice(), a function
or method imported from a library called random. We have used a method of the
string data type to capitalize the first letter of the sentence.
"""
#%%
import random
verbs=["goes","cooks","shoots","faints","chews","screams"]
nouns=["bear","lion","mother","baby","sister","car","bicycle","book"]
adverbs=["handily","sweetly","sourly","gingerly","forcefully","meekly"]
articles=["a","the","that","this"]
def sentence():
article = random.choice(articles)
noun = random.choice(nouns)
verb = random.choice(verbs)
adverb = random.choice(adverbs)
our_sentence = article + " " + noun + " " + verb + " " + adverb + "."
our_sentence = our_sentence.capitalize()
print(our_sentence)
#%%
"""
Exercise:
Adapt this function to write a four line poem. Call it simple_poem().
Essentially you have to write a loop around this so that you get 4 lines.
Remember that the inside or scope of the loop has to be indented 4 spaces.
Note: The Edit menu has a quick way to indent a series of lines. The function
is repeated here for your convenience in modifying it.
"""
"""
Solution (modify the copy below to be your simple_poem function):
"""
#%%
import random
verbs=["are","is","goes","cooks","shoots","faints","chews","screams"]
nouns=["bear","lion","mother","baby","sister","car","bicycle","book"]
adverbs=["handily","sweetly","sourly","gingerly","forcefully","meekly"]
articles=["a","the","that","this"]
def simple_poem():
for i in range(0,4,1):
article = random.choice(articles)
noun = random.choice(nouns)
verb = random.choice(verbs)
adverb = random.choice(adverbs)
our_sentence = article + " " + noun + " " + verb + " " + adverb + "."
our_sentence = our_sentence.capitalize()
print(our_sentence)
#%%
"""
End Solution:
"""
"""
Let's look at a couple of loop design patterns.
"""
"""
Example: Add numbers until you get a blank one. This initializes a variable
sum_ and adds to it each time through the loop. Afterwards sum_ is used in a
print statement.
"""
#%%
def add_up():
sum_ = 0
while True: # will loop forever
num = int(input("Enter a number, input 0 to quit: "))
if num == 0:
break # breaks out of while loop
sum_ = sum_ + num
print(sum_)
#%%
"""
Building lists - recall the .append() method
"""
#%%
baseball = []
baseball.append("ball")
baseball.append("bat")
baseball.append("mitt")
baseball
#%%
"""
Let's write a program to build a list of the numbers. Before we initialized
sum_ to 0. The equivalent for a list is to set it to the empty list. Adding to
the sum has its equivalent in appending to the list.
"""
#%%
def store_up():
num_lis = []
while True:
nextnum = int(input("Enter a number, 0 to quit: "))
if nextnum == 0:
break
num_lis.append(nextnum)
print(num_lis)
#%%
"""
Exercise:
Write a function diner_waitress() that asks for you order. First start an empty
list, call it order. Then use a while loop and an input() statement to gather
the order. Continue in the while loop until the customer says "that's all".
Onne way to end the loop is to use 'break' to break out of the loop when
"that's all" is entered.
Recall that you can add to a list by using the list's .append() method; suppose
that your list is called order. To create an empty list you can use
order = []. You are going to have to input one food at a time and append it
to the order list.
Then print out the order. Here is my run:
diner_waitress()
Hello, I'll be your waitress. What will you have?
menu item: eggs
menu item: bacon
menu item: toast
menu item: jelly
menu item: that's all
You've ordered:
['eggs', 'bacon', 'toast', 'jelly']
"""
#%%
"""
Solution:
"""
#%%
def diner_waitress():
food = []
print("Hello, I'll be your waitress. What will you have?")
while True:
nextFood = input("menu item: ")
if nextFood == "that's all":
break
food.append(nextFood)
print("You've ordered:")
print(food)
#%%