CSE 160 Section 2
Loops and Range!
TA1 & TA2
Logistics
Hw 1 Tips
Lecture Review: Loops
Loops
for i in [0, 1, 2, 3]:
print(i)
Loops
or number of times loop is repeated
Lecture Review: Range
Range
Example: range(0, 6, 2) loops through [0, 2, 4]
Equivalent to: range(0, 5, 2)
Kahoot!
Section Handout Problems
Lists, Ranges, and Loops
3. What is the output printed by the following program?
for value in [1, 3, 5]:
print(value + value ** 2)
Lists, Ranges, and Loops
3. What is the output printed by the following program?
for value in [1, 3, 5]:
print(value + value ** 2)
Output:
2
12
30
Lists, Ranges, and Loops
4. 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
Lists, Ranges, and Loops
4. 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)
Nested Loops
5. 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
Nested Loops
6. 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
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]
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]
count = 0;
for age in ages:
if (age > 20):� count += 1;
print(count)
Mod Operator Overview
Mod
Modulo operator, denoted as %, is a mathematical operation that calculates the remainder when one number is divided by another.
Mod
Mod
What is the output of the following code?
for i in range(5):
print(i % 3)
Mod
What is the output of the following code?
for i in range(5):
print(i % 3)
Answer:
0
1
2
0
1
If / Else Overview
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”)
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
Coding Practice 1
# ~~~ Begin Problem 1 ~~~
'''
Given a list of integers, prints the sum of the even ints in the list.
Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.
Assumptions:
nums: a variable containing list of integers.
Prints: a integer representing the sum of the even numbers in the list
Examples:
nums = [5, 2, 6, 3, 4] should print 12
nums = [3, 6, 11, 2, 5] should print 8
nums = [] should print 0
nums = [7] should print 0
nums = [5, 8, 5] should print 8
nums = [72, 2, 6, 1] should print 80
nums = [6] should print 6
nums = [4, 4, 4] should print 12
nums = [1, 1, 1, 2] should print 2
Try copying these values into the nums variable to see if your code works.
'''
nums = []
sum = 0
# Write your code for Problem 1 here!
print(sum)
# ~~~ End Problem 1 ~~~
Section Code
** Note that section codes are only given in-person and are not given through email, Ed, or other mediums.