COMP 204
Operations on containers: enumerate, zip, comprehension Mathieu Blanchette based on material from Yue Li, Carlos Oliver Gonzalez and Christopher Cameron
1 / 21
COMP 204 Operations on containers: enumerate, zip, comprehension - - PowerPoint PPT Presentation
COMP 204 Operations on containers: enumerate, zip, comprehension Mathieu Blanchette based on material from Yue Li, Carlos Oliver Gonzalez and Christopher Cameron 1 / 21 Quiz password 2 / 21 Side-track: a convenient way to format print
1 / 21
2 / 21
1 p i = 3.1415927 2 3 # standard
4
5 6 # p r i n t i n g
7
8
9 10 grades = { ' Sjoerd ' :
11 f o r
12
13
14 15 #output : 16 # p i
17 # p i
18 # p i
19 # Sjoerd
20 # Jack
21 # Annie
3 / 21
4 / 21
1 L = s o m e l i s t 2 f o r
3
4
1 L = s o m e l i s t 2 f o r
3
5 / 21
1 names = [ ” H i l l a r y ” , ”Yang” , ” Bernard ” , ” Drina ” ] 2 3 # Goal :
4 5 # u s in g
6 f o r
7
8
9 10 # u s in g
11 f o r
12
6 / 21
1 names = [ ” H i l l a r y ” , ”Yang” , ” Bernard ” , ” Drina ” ] 2 ages = [42 , 15 ,23 ,17] # the
3 4 # Goal :
5 6 # u s in g
7 f o r
8
9
10
11 12 # u s in g
13 f o r
14
15
7 / 21
1 A = s o m e l i s t 2 B = s o m e o t h e r l i s t 3 f o r
4
5
6
1 A = s o m e l i s t 2 B = s o m e o t h e r l i s t 3 f o r
4
8 / 21
1 2 names = [ ' John ' , ' Daenery ' , ' Jamie ' , ' Tyrion ' , ' Robert ' ] 3 surnames = [ 'Snow ' , ' Targaryen ' , ' L a n n i s t e r ' , ' L a n n i s t e r ' ,\ 4
5 6 # without
7 # i s
8 f u l l n a m e s = [ ] 9 f o r
10
11
12 13 # or 14 f u l l n a m e s = [ ] 15 f o r
16
17
18 19 # This
20 f u l l n a m e s = [ ] 21 f o r
22
23
9 / 21
1 names = [ ' John ' , ' Daenery ' , ' Jamie ' , ' Tyrion ' , ' Robert ' ] 2 surnames = [ 'Snow ' , ' Targaryen ' , ' L a n n i s t e r ' , ' L a n n i s t e r ' ,\ 3
4 deaths = [ 5 ,
5 6 f o r
7
10 / 21
1 L = s o m e l i s t 2
3 4 f o r
5
6
1 L = s o m e l i s t 2 3
11 / 21
1 # Given
2 # Produce
3 l e n g t h o f g e n e s =[160 ,393 ,3012 ,192 ,27] 4 5 # with
6
7 f o r
8
9 10 # u s in g
11
12 / 21
1 # Produce
2 3 # with
4 s q u a r e s =[] 5 f o r
6
7 8 # with
9 s q u a r e s =[ n∗n
13 / 21
1 # Given
2 # Produce
3 # Assume
4 # the
5 s=”ATGCAGCATGAAGATGAA” 6 7 # with
8
9 f o r
10
11 12 # with
13
14 15 # Note :
16
14 / 21
1 L = s o m e l i s t 2
3 4 f o r
5
6
7
1 L = s o m e l i s t 2 3
15 / 21
1 # Goal :
2 3 # with
4 squares odd =[] 5 f o r
6
7
8 9 # with
10 s q u a r e s o f o d d = [ i ∗ i
16 / 21
1 # Goal :
2 #
3 4 names = [ ' John ' , ' Daenery ' , ' Jamie ' , ' Tyrion ' , ' Robert ' ] 5 6 # with
7 names with N =[] 8 f o r
9
10
11 12 # with
13 names with N = [ name
14
17 / 21
1 # Goal :
2 #
3 4 names = [ ' John ' , ' Daenery ' , ' Jamie ' , ' Tyrion ' , ' Robert ' ] 5 surnames = [ 'Snow ' , ' Targaryen ' , ' L a n n i s t e r ' , ' L a n n i s t e r ' ,\ 6
7 8 # with
9
10 f o r
11
12
13 14 # with
15
16
18 / 21
1 # Goal :
2 #
3 #
4 5 names = [ ' John ' , ' Daenery ' , ' Jamie ' , ' Tyrion ' , ' Robert ' ] 6 surnames = [ 'Snow ' , ' Targaryen ' , ' L a n n i s t e r ' , ' L a n n i s t e r ' ,\ 7
8 deaths = [ 5 ,
9 10 # with
11
12 f o r
13
14
15 16 # with
17
18
19 / 21
1 D = s o m e d i c t i o n n a r y 2
3 4 f o r
5
6
7
8
1 D = s o m e d i c t i o n n a r y 2
3
20 / 21
1 # Goal :
2 # Produce :
3 #
4 5 def BMI(h ,w) : 6
7 8
9
10
11 12 # with
13 high BMI={} 14 f o r
15
16
17
18
19 20 # with
21 high BMI = {name : BMI(h ,w)
22
23
24
21 / 21