CSE 160 Section 3
Loops and Range
CSE 160
CSE 160: Section 3
Logistics
CSE 160: Section 3
Lecture Review: Loops
CSE 160: Section 3
Loops
for i in [0, 1, 2, 3]:
print(i)
CSE 160: Section 3
Loops
or number of times loop is repeated
CSE 160: Section 3
Lecture Review: Range
CSE 160: Section 3
Range
Example: range(0, 6, 2) loops through [0, 2, 4]
Equivalent to: range(0, 5, 2)
CSE 160: Section 3
Section Handout Problems
CSE 160: Section 3
Warm-Up Problem
What is the difference between the behavior of the two for loops provided?
for i in range(4): for i in [0, 1, 2, 3]: print(i) print(i)
| |
CSE 160: Section 3
Warm-Up Problem
What is the difference between the behavior of the two for loops provided?
for i in range(4): for i in [0, 1, 2, 3]: print(i) print(i)
| |
There is no difference! Both print
0
1
2
3
range(4) generates 0, 1, 2, 3 automatically (the stop value 4 is not included); the list writes them out by hand.
CSE 160: Section 3
Problem 1: Range()
Fill in each blank with the correct range() call or sequence of numbers.
Sequence | range() Call |
[0, 1, 2, 3] | |
[-4, -3, -2, -1, 0] | |
| range(0, 10, 2) |
| range(2, 11, 3) |
[25, 20, 15, 10, 5, 0] | |
| range(1000, -100, -100) |
CSE 160: Section 3
Problem 1: Range()
Sequence | range() Call |
[0, 1, 2, 3] | range(4) or range(0, 4) or range(0, 4, 1) |
[-4, -3, -2, -1, 0] | range(-4, 1) or range(-4, 1, 1) |
[0, 2, 4, 6, 8] | range(0, 10, 2) |
[2, 5, 8] | range(2, 11, 3) |
[25, 20, 15, 10, 5, 0] | range(25, -1, -5) |
[1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 0] | range(1000, -100, -100) |
CSE 160: Section 3
Problem 2: Four or For
What is the output of the following program?
sum = 1
for i in [1, 2, 3, 4]:
sum *= i
print(sum)
CSE 160: Section 3
Problem 2: Four or For
What is the output of the following program?
sum = 1
for i in [1, 2, 3, 4]:
sum *= i
print(sum)
24
CSE 160: Section 3
Problem 3: Skipping Numbers
Write a for loop that will print the result of multiplying 3 by the numbers 8 through 12. Your output should read:
24
27
30
33
36
CSE 160: Section 3
Problem 3: Skipping Numbers
Write a for loop that will print the result of multiplying 3 by the numbers 8 through 12. Your output should read:
24
27
30
33
36
for i in range(8, 13):
print (3 * i)
CSE 160: Section 3
Exit Ticket
Write a program to print the number of people over 30 years old in the list ages.
ages = [20, 21, 20, 22, 19, 18, 14, 35]
CSE 160: Section 3
Exit Ticket
Write a program to print the number of people over 30 years old in the list ages.
ages = [20, 21, 20, 22, 19, 18, 14, 35]
# "Over 30" means strictly older than 30 (age > 30). Otherwise (age >= 30), the answer should be 2
count = 0
for age in ages:
if age > 30:
count += 1
print(count)
CSE 160: Section 3
Problem 5: Putting It All Together
Given a sequence of numbers called ages, write a program that calculates and prints out the number of people 30 years old and above, the number of people between 20 and 29 years old (inclusive), and the number of people under 20 years old in the given sequence.
For example, if the sequence is : ages = [20, 21, 20, 22, 19, 18, 14, 35]
Then the output of the program should be:
1 people are 30 years old or older
4 people are between 20 and 29 years old
3 people are younger than 20 years old
If there are no ages in the sequence that fall into an age category, the number printed for that category should be 0.
CSE 160: Section 3
Problem 5: Putting It All Together
ages = [20, 21, 20, 22, 19, 18, 14, 35]
count_30_plus = 0
count_20_to_29 = 0
count_under_20 = 0
for age in ages:
if age >= 30:
count_30_plus += 1
elif age >= 20: # age < 30 here
count_20_to_29 += 1
else: # age < 20
count_under_20 += 1
print(count_30_plus, "people are 30 years old or older")
print(count_20_to_29, "people are between 20 and 29 years old")
print(count_under_20, "people are younger than 20 years old")
CSE 160: Section 3
Problem 4: What is the output?
What is the output of the following program?
for value in range(3):
print(value + value ** 2)
CSE 160: Section 3
Problem 4: What is the output?
What is the output of the following program?
for value in range(3):
print(value + value ** 2)
0
2
6
CSE 160: Section 3
Thanks for Coming!!
CSE 160: Section 3