for number in [2, 3, 5]:
print(number)2
3
5
for loop does and when to use one.for loop.for loops repeat operations over a collectionPerforming the same calculation on every value in a list — or every file in a directory — by writing out each step individually is impractical. A for loop instructs Python to execute a block of statements once for each item in a collection, automatically advancing to the next item after each pass:
This is exactly equivalent to three separate print calls, but scales to thousands of items without any extra code.
for loopEvery for loop has three parts:
[2, 3, 5]).number). It is created automatically and can be named anything, but a descriptive name makes the code easier to read.The first line must end with a colon. Python uses indentation — not braces or keywords — to mark where the body begins and ends. Four spaces is the universal convention:
Cell In[2], line 2 print(number) # missing indentation ^ IndentationError: expected an indented block after 'for' statement on line 1
Unexpected indentation is also an error — every line in a block must line up consistently:
Cell In[3], line 2 lastName = "Smith" # unexpected extra indent ^ IndentationError: unexpected indent
The body can contain as many statements as needed, though loops that run to more than a few lines are often better refactored into a function:
The built-in range function produces a sequence of integers on demand — it does not build a list in memory, which matters when iterating over millions of values. range(N) yields 0, 1, …, N-1, and range(M, N) yields M, M+1, …, N-1:
A common and important programming pattern is to start with an initial value and update it incrementally inside a loop. This is called the accumulator pattern:
Read total = total + (number + 1) as: take the current value of total, add number + 1 to it, then store the result back into total. We add 1 because range(10) yields 0 through 9, not 1 through 10. The same pattern works for building up a string, a list, or any other type that can be combined incrementally.
Is an IndentationError a syntax error or a runtime error?
It is a syntax error. Python detects it while parsing the source code, before any code is executed. A runtime error only appears when the problematic line is actually reached during execution.
Create a table showing the line numbers and variable values as this program executes:
| Line | char |
total |
|---|---|---|
| 1 | — | 0 |
| 2 | 't' |
0 |
| 3 | 't' |
1 |
| 2 | 'i' |
1 |
| 3 | 'i' |
2 |
| 2 | 'n' |
2 |
| 3 | 'n' |
3 |
Fill in the blanks so that the program prints "nit" (the reverse of "tin"):
Fill in the blanks in each program to produce the indicated result.
Reorder and properly indent the lines below so that the program prints [1, 3, 5, 10]:
NameError caused by a missing quote, a misspelled variable, or a variable that was never defined?There are three errors: Number should be number (Python is case-sensitive), a should be "a" (missing quotes make it a variable reference rather than a string), and message is used before it is defined. The corrected version:
for loop executes its body once for each item in a collection.range(N) to iterate over the integers 0 through N-1.