CSE 160 Section 2
Loops and Range, If statements!
CSE 160
CSE 160: Section 2
Logistics
CSE 160: Section 2
Hw 1 Tips
CSE 160: Section 2
DiffChecker Example
Problem 1 solution follows:
Root 1: ____
Root 2: ____
Problem 1 solution follows:
Root 1 : ____
Root 2 : ____
CSE 160: Section 2
DiffChecker Example
Problem 1 solution follows:
Root 1: ____
Root 2: ____
Problem 1 solution follows:
Root 1 : ____
Root 2 : ____
CSE 160: Section 2
If / Else Overview
CSE 160: Section 2
If/ Else Structure
is_raining = True
is_sprinkling = False
if is_raining:
print(“Bring an umbrella”)
elif is_sprinkling:
print(“Bring a raincoat”)
else:
print(“Bring sunglasses”)
CSE 160: Section 2
If/ Else Structure
is_raining = True
is_sprinkling = False
if is_raining:
print(“Bring an umbrella”)
elif is_sprinkling:
print(“Bring a raincoat”)
else:
print(“Bring sunglasses”)
Output:
Bring an umbrella
CSE 160: Section 2
Lecture Review: Loops
CSE 160: Section 2
Loops
for i in [0, 1, 2, 3]:
print(i)
CSE 160: Section 2
Loops
or number of times loop is repeated
CSE 160: Section 2
Nested Loops Overview
CSE 160: Section 2
Nested Loops
Example:
for i in [0, 1, 2, 3]:
for j in [0, 1, 2]:
print(i)
CSE 160: Section 2
Nested Loops
for i in [0, 1, 2, 3]:
for j in [0, 1, 2]:
print(i)
CSE 160: Section 2
Nested Loops
Example:
for i in [0, 1, 2, 3]:
for j in [0, 1, 2]:
print(i)
CSE 160: Section 2
Lecture Review: Range
CSE 160: Section 2
Range
Example: range(0, 6, 2) loops through [0, 2, 4]
Equivalent to: range(0, 5, 2)
CSE 160: Section 2
Kahoot!
CSE 160: Section 2
Section Handout Problems
CSE 160: Section 2
Lists, Ranges, and Loops
3. 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. Your output should read:
24
27
30
33
36
CSE 160: Section 2
Lists, Ranges, and Loops
3. Possible solutions appear below:
for value in [8, 9, 10, 11, 12]:
print(3 * value)
for value in range(8, 13):
print(3 * value)
for value in range(24, 37, 3):
print(value)
CSE 160: Section 2
Nested Loops
4. What is the output printed by the following program?
sum = 0
for i in [1, 2, 3]:
for j in [1, 2, 3]:
sum = sum + i
print(sum)
Answer: Python Tutor
CSE 160: Section 2
Additional Practice Problems
CSE 160: Section 2
Lists, Ranges, and Loops
6. What is the output printed by the following program?
for value in [1, 3, 5]:
print(value + value ** 2)
CSE 160: Section 2
Lists, Ranges, and Loops
6. What is the output printed by the following program?
for value in [1, 3, 5]:
print(value + value ** 2)
Output:
2
12
30
CSE 160: Section 2
Nested Loops
7. What is the output printed by the following program?
for i in [1, 2, 3]:
for j in [1, 2, 3]:
print(i + j)
Answer: Python Tutor
CSE 160: Section 2
Nested Loops
8. Write code to print the number of people over 20 years old in the list ages
ages = [20, 21, 20, 22, 19, 18, 14, 35]
CSE 160: Section 2
Nested Loops
ages = [20, 21, 20, 22, 19, 18, 14, 35]
count = 0
for age in ages:
if (age > 20):� count += 1
print(count)
CSE 160: Section 2
Written Check-In
Due Friday @ 11:59pm
CSE 160: Section 2