Welcome to CSE 160! Week 2
Section AA with Andre
Agenda
Goals:
Logistics
For loops
for i in [0, 1, 2, 3]:
print(i)
range
syntax: range(start [inclusive], stop [exclusive], step)
for i in range(5): # equivalent to range(0, 5, 1)
print(“hi!”)
[out]:
hi!
hi!
hi!
hi!
hi!
range
syntax: range(start [inclusive], stop [exclusive], step)
for i in range(5): # equivalent to range(0, 5, 1)
print(i)
[out]:
0
1
2
3
4
range
syntax: range(start [inclusive], stop [exclusive], step)
for i in range(3, 5): # equivalent to range(3, 5, 1)
print(i)
[out]:
3
4
range
syntax: range(start [inclusive], stop [exclusive], step)
for i in range(0, 10, 2):
print(i)
[out]:
0
2
4
6
8
range
syntax: range(start [inclusive], stop [exclusive], step)
for i in [‘apple’, ‘banana’, ‘orange’]:
print(i + ‘s’)
[out]:
apples
bananas
oranges
range
syntax: range(start [inclusive], stop [exclusive], step)
for letter in “five”:
print(letter)
[out]:
f
i
v
e
range
syntax: range(start [inclusive], stop [exclusive], step)
for letter in “five”:
print(letter * 5)
[out]:
fffff
iiiii
vvvvv
eeeee
Practice Problem #1
Write a for loop that will print the result of multiplying 3 by the numbers 8 through 12. The example solution is two lines long.
[out]:
24
27
30
33
36
Practice Problem #1
Write a for loop that will print the result of multiplying 3 by the numbers 8 through 12. The example solution is two lines long.
[out]:
24
27
30
33
36
Practice Problem #2
Write the output to the following program:
for i in [1, 2, 3]:
for j in [1, 2, 3]:
print(i + j)
Practice Problem #3
Write the output to the following program:
sum = 0
for i in [1, 2, 3]:
for j in [1, 2, 3]:
sum = sum + i
print(sum)
Mini-Project: Password Cracking