1 of 29

CSE 160 Section 2

Loops and Range, If statements!

CSE 160

CSE 160: Section 2

2 of 29

Logistics

  • How was HW0? Everything has been set up?

  • HW1 (due Thurs Jan 22 @ 11:59pm)
    • Submitting file on Gradescope - don’t change the file name!
    • Wait for autograder!

  • Programming practice 2 (due Wed Jan 21 @ 11:59pm)

  • Written Check-in 2 (due Friday Jan 16 @ 11:59pm)

CSE 160: Section 2

3 of 29

Hw 1 Tips

  • Reminders:
    • To run the file: python FILE_NAME.py
    • Check for formatting errors - flake8: flake8 FILE_NAME.py
    • Don’t change the filenames!
  • Your output should exactly match the expected output.
    • blanks should be replaced with your answers

Diffchecker.com

CSE 160: Section 2

4 of 29

DiffChecker Example

Problem 1 solution follows:

Root 1: ____

Root 2: ____

Problem 1 solution follows:

Root 1 : ____

Root 2 : ____

CSE 160: Section 2

5 of 29

DiffChecker Example

Problem 1 solution follows:

Root 1: ____

Root 2: ____

Problem 1 solution follows:

Root 1 : ____

Root 2 : ____

CSE 160: Section 2

6 of 29

If / Else Overview

CSE 160: Section 2

7 of 29

If/ Else Structure

  • Checks that condition is True/False, and executing code based on that condition

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

8 of 29

If/ Else Structure

  • Checks that condition is True/False, and executing code based on that condition

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

9 of 29

Lecture Review: Loops

CSE 160: Section 2

10 of 29

Loops

  • Any variable name can be used in place of i (for good style, pick a variable name that fits the context)
  • Indentation is a requirement to indicate loop body

for i in [0, 1, 2, 3]:

print(i)

PythonTutor

CSE 160: Section 2

11 of 29

Loops

  • Avoid redundancy
  • More readable
  • Avoid bugs
  • Can easily make changes in code body,

or number of times loop is repeated

CSE 160: Section 2

12 of 29

Nested Loops Overview

CSE 160: Section 2

13 of 29

Nested Loops

  • A for loop within the body of another for loop

Example:

for i in [0, 1, 2, 3]:

for j in [0, 1, 2]:

print(i)

CSE 160: Section 2

14 of 29

Nested Loops

  • Nested loop structure
    • Outer loop
    • Inner loop

for i in [0, 1, 2, 3]:

for j in [0, 1, 2]:

print(i)

  • Variable names i and j should be different for clarity

CSE 160: Section 2

15 of 29

Nested Loops

  • Lets see what this outputs!

Example:

for i in [0, 1, 2, 3]:

for j in [0, 1, 2]:

print(i)

PythonTutor

CSE 160: Section 2

16 of 29

Lecture Review: Range

CSE 160: Section 2

17 of 29

Range

  • range(start, stop, step)
  • range(start, stop)
    • (inclusive, exclusive)
    • Step is assumed to be 1
  • range(stop)
    • (exclusive)
    • Step is assumed to be 1; Start is assumed to be 0

Example: range(0, 6, 2) loops through [0, 2, 4]

Equivalent to: range(0, 5, 2)

Python Tutor

CSE 160: Section 2

18 of 29

Kahoot!

Link

CSE 160: Section 2

19 of 29

Section Handout Problems

CSE 160: Section 2

20 of 29

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

21 of 29

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

22 of 29

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

23 of 29

Additional Practice Problems

CSE 160: Section 2

24 of 29

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

25 of 29

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

26 of 29

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

27 of 29

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

28 of 29

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

29 of 29

Written Check-In

Due Friday @ 11:59pm

CSE 160: Section 2