1 of 26

CSE 160 Section 2

Loops and Range!

TA1 & TA2

CSE 160

CSE 160: Section 2

2 of 26

Logistics

  • Homework: Practical (due Friday 4/10 @ 11:59pm)
    • Submitting file on Gradescope - don’t change the file name!
    • Wait for autograder!
  • Section Assignment 2 (due Friday 4/10 @ 11:59pm)

  • Programming Practice 2 (due Tuesday 4/14 @ 11:59pm)
  • Homework: DNA Analysis (due Friday 4/17 @ 11:59pm)
  • Quest 1 in lecture (on Friday 4/17 @ 1:30pm)

CSE 160: Section 2

3 of 26

Homework: Practical Tips

  • Reminders:
    • To run the file: %run 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 26

DiffChecker Example

Problem 1 solution follows:

Root 1: ____

Root 2: ____

Problem 1 solution follows:

Root 1 : ____

Root 2 : ____

CSE 160: Section 2

5 of 26

DiffChecker Example

Problem 1 solution follows:

Root 1: ____

Root 2: ____

Problem 1 solution follows:

Root 1 : ____

Root 2 : ____

CSE 160: Section 2

6 of 26

Lecture Review: Range

CSE 160: Section 2

7 of 26

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

8 of 26

Lecture Review: Loops

CSE 160: Section 2

9 of 26

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 range(1, 4):

print(i)

PythonTutor

CSE 160: Section 2

10 of 26

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

11 of 26

Nested Loops Overview

CSE 160: Section 2

12 of 26

Nested Loops

  • A for loop within the body of another for loop

Example:

for i in range(0, 3):

for j in range(0, 2):

print(i)

CSE 160: Section 2

13 of 26

Nested Loops

  • Nested loop structure
    • Outer loop
    • Inner loop

for i in range(0, 3):

for j in range(0, 2):

print(i)

  • Variable names i and j should be different for clarity

CSE 160: Section 2

14 of 26

Nested Loops

  • Lets see what this outputs!

Example:

for i in range(0, 3):

for j in range(0, 2):

print(i)

PythonTutor

CSE 160: Section 2

15 of 26

Boolean Zen

  • Minimize the use of if statements where possible!
    • More readable
    • Easier to debug

num = 0

if num == 2:

is_two = True

else:

is_two = False

num = 0

is_two = num == 2

Without boolean zen:

With boolean zen:

CSE 160: Section 2

16 of 26

Section Handout Problems

CSE 160: Section 2

17 of 26

Ranges and Loops

2. 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

18 of 26

Ranges and Loops

2. Possible solutions appear below:

for value in range(8, 13):

print(3 * value)

for value in range(24, 37, 3):

print(value)

*You’ll learn about lists soon!

for value in [8, 9, 10, 11, 12]:

print(3 * value)

CSE 160: Section 2

19 of 26

Nested Loops

3. What is the output printed by the following program?

sum = 0

for i in range(1, 4):

for j in range(1, 4):

sum = sum + i

print(sum)

Answer: Python Tutor

CSE 160: Section 2

20 of 26

Ranges and Loops

4. What is the output printed by the following program?

for value in range(1, 6, 2):

print(value + value ** 2)

CSE 160: Section 2

21 of 26

Ranges and Loops

4. What is the output printed by the following program?

for value in range(1, 6, 2):

print(value + value ** 2)

Output:

2

12

30

CSE 160: Section 2

22 of 26

Additional Practice Problems

CSE 160: Section 2

23 of 26

Nested Loops

5. What is the output printed by the following program?

for i in range(1, 4):

for j in range(1, 4):

print(i + j)

Answer: Python Tutor

CSE 160: Section 2

24 of 26

Ranges and Loops

6. What is the output printed by the following program?

for value in range(1, 4):

print(value + value ** 2)

CSE 160: Section 2

25 of 26

Section Code:

CSE 160: Section 2

26 of 26

Looking ahead: Functions, Lists, and Nested Lists

Be sure to remember the Section Code!

CSE 160: Section 2