Lesson 03: Iteration and control flow (part I)

This week we focus on iteration and control flow (i.e., the order in which a program or script executes). You will learn how to iterate or loop over a sequence (list, range, str, tuple) using a for loop.

You will also learn how to write conditional statements in order to determine which computations your code must perform. Conditional statements can be placed inside the body of a for loop in order to act as data filters or terminate a looping operation if a particular condition has been satisfied.

Conditional statements employ a variety of operators. As noted previously, Python operators are organized into groups. We’ve touched on arithmetic operators and assignment operators. Starting this week you will begin using other operators when writing conditional statements, especially comparison, logical, identity, and membership operators.

The teaching team will also introduce you to VS Code’s debugger, a tool used to test and debug programs. When running the debugger you can step through your code line by line while the program is running, setting “break points” that pause the execution of your program at a given line (or lines) in order to examine its evolving state as objects in memory are created, mutated (if permitted), reassigned, deleted, etc.

Readings

Below are a set of readings that you should consider reviewing. Note that the Real Python articles referenced below may require signing up for a free account.

1.0 Required

Eric Matthes, Python Crash Course, 3rd Edition (No Starch Press, 2023).

Read Chapter 4, Working with Lists.

Eric covers looping over a list, the range() type, list comprehensions, tuples, and slicing. You can skip the sections on list comprehensions and styling. We cover those topics later in the semester.

Read chapter 5, If statements.

Eric discusses conditional tests, if statements, and using if statements with lists.

Lisa Tagliaferri, Understanding Boolean Logic in Python 3.

Lisa discusses booleans, comparison operators, logical operators, truth values, and using expressions that return True or False for control flow. Functions are often designed to perform a computation that returns either True or False given a certain input.

Chad Hansen, Understanding the Python Traceback (RealPython, 2019).

Chad discusses the “traceback”, an error report issued by the Python Interpreter whenever the code you write triggers an exception. Read the following sections:

  • What is a Python Traceback?
  • How do you Read a Python Trackback?
  • What are some Common Tracebacks in Python?

Then review the following exceptions:

  • AttributeError
  • IndentionError
  • IndexError
  • NameError
  • SyntaxError
  • TypeError
  • ValueError

Geir Arne Hjelle, Python range(): Represent Numerical Ranges (Real Python, Jan 2024)

Thorough discussion of the range type and its uses.

John Sturtz, Conditional Statements in Python (Real Python, Sept 2018).

Solid introduction to conditional statements. Includes useful flow charts.

Lisa Tagliaferri, How to Construct For Loops in Python 3 (Digital Ocean, Sept 2016).

Lisa’s articles provides the right amount of detail in an otherwise compact presentation format.

Lisa Tagliaferri, How to Write Conditional Statements in Python 3 (Digital Ocean, June 2016).

Lisa provides a short introduction on how to craft if-elif-else conditional statements, including nested if statements.

3.0 Reference

Visual Studio Code, Debugging (Microsoft, nd).