Sep 27: Two Kinds of for Loops
Learning Objectives
After today's class, you should be able to:
- Explain the syntax and the purpose of a
for
statement. - Predict how
range()
works given 1, 2, or 3 arguments. - Explain the difference of iterating values versus indexes.
Reminders¶
- Read Ch 6 – ideally:
- 1st half before Friday
- 2nd half before Monday
- Submit HW 6 – ideally:
- 1 and 2 before Friday
- 3 and 4 before Monday
POGIL Activity¶
- For Loops
- If you are absent today, complete this activity at home
- Bring your completed activity to class or office hours
Example Code¶
Model 1
print("hello")
for x in [2, 7, 1]:
print("the number is", x)
print("goodbye")
Question 8
for c in "Hi!":
print(c)
Model 2
range(5)
list(range(5))
x = range(3)
print(x)
print(list(x))
list(range(5, 10))
list(range(-3, 4))
list(range(4, 10, 2))
for i in range(5):
print(i)
Model 3
names = ["emma", "liam", "aisha", "mateo", "sofia", "ravi"]
for i in range(len(names)):
name = names[i]
# replace element at index i
names[i] = name.capitalize()
Question 24
prices = [19.99, 6.34, 1.00, 12.79, 2.50]