Lecture 11: Iteration and For-Loops
(Sections 4.2 and 10.3) CS 1110 Introduction to Computing Using Python
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
Lecture 11: Iteration and For-Loops (Sections 4.2 and 10.3) CS - - PowerPoint PPT Presentation
http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 11: Iteration and For-Loops (Sections 4.2 and 10.3) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
2
3
4
5
6
7
8
9
grades has more elements put next element in x
True False
print(x)
10
11
12
13
14
15
16
17
18
19
print(line+", blah blah something that rhymes with "+str(num))
20
print(line+", blah blah something that rhymes with "+str(num))
21
# at our 1 year anniversary my partner gave me a rose # and promised to give me 1 more rose each year thereafter # how many roses will that be?! met_year = 2003 n_years = 75 total_roses = 0 for n_years in list(range(1, n_years+1)): print(str(met_year+n_years)+”: "+str(n_years)+" roses") total_roses = total_roses + n_years print("After "+str(n_years)+" years: "+str(total_roses)+" roses!")
22
23
24
25
26
>>> len_list = list(map(len, ['a', 'bc', 'defg’])) >>> len_list [1, 2, 4]
27
28
29
30
31
32
33
1 2 id4 5 4 7 grades id4 add_one the_list 1 id4 1 2
34
1 2 1 2 id4 5 4 7 grades id4
add_one the_list 2 id4
x 5
35
1 2
add_one the_list 1 id4 x 6 1 2 id4 5 4 7 grades id4
Loop back to line 1
36
1 2
add_one the_list 2 id4
x 4 1 2 id4 5 4 7 grades id4
37
1 2 add_one the_list 1 id4
x 5 1 2 id4 5 4 7 grades id4
Loop back to line 1
38
1 2
add_one the_list 2 id4
x 7 1 2 id4 5 4 7 grades id4
39
1 2 add_one the_list 1 id4
x 8 1 2 id4 5 4 7 grades id4
Loop back to line 1
40
1 2
add_one the_list id4
x 8 RETURN NONE 1 2 id4 5 4 7 grades id4
41
1 2
1 2 id4 5 4 7 grades id4