283x Filetype PDF File size 0.65 MB Source: www.cs.cmu.edu
Algorithmic Thinking:
Loops and Conditionals
Last Time
A control flow structure: for loop
range(n)
range(start, end)
range(start, end, step)
Assignments that modify variables:
x = x + y
Iteration with for loops
def test1(): What determines how many
for i in range(1,6): times “Woof” is printed is the
print("Woof") number of elements in the
range.
>>> test1()
Woof Any expression that gives 5
Woof elements in the range would
Woof give the same output.
Woof
Woof For example,
range(5), range(0,5), …
Iteration with for loops
def test2(): range(5) ?
for i in range(1,6): range(0, 5) ?
print(i, end='-') range(1, 6) ?
>>> test2() range(1, 10, 2) ?
1-2-3-4-5- range(2, 10, 2) ?
range(10, 1, -1) ?
range(10, 2, -4) ?
no reviews yet
Please Login to review.