INTRO O
PROBLEM SOLVING AND
PROGRAMMING IN PYTHON
(use the Space key to navigate through all slides)

CIS 1051 - FOR LOOP

Understanding the
statement
REPEATING Actions in Code
for
Every day life, we often repeat tasks:
- Checking emails (one by one).
- Taking steps while walking.
- Counting down before a rocket launch.



Wouldn’t it be GREAT
to make Python
repeat actions for us?
That’s exactly what
a for loop does!
Syntax:
write Python programs that
repeat the same code execution
for variable in sequence:
# Code to repeat
passit allows us to execute a block of code multiple times, iterating over items in a sequence.
- The variable takes one value from the sequence at a time.
- The loop body runs once per item in the sequence.
Example: Printing Names in a List
names = ["Alice", "Bob", "Charlie"]
for name in names:
print(name)-
The loop picks the first name
("Alice") and prints it. -
Then, it picks
"Bob"... -
Then, ...
Each iteration automatically
moves to the next item!
Example: Looping Over a String
word = "Python"
for letter in word:
print(letter)What Happens?
The loop prints each letter
one at a time!
(a string is also a sequence of characters)
Using the range() Function
# Starts at 1, stops before 6
for num in range(1, 6):
print(num)How It Works
-
range(1, 6)generates
a List[1, 2, 3, 4, 5]
We can use range(n) to repeat an action n times.
# `_` is a placeholder (we
# don't need the variable)
for _ in range(3):
print("Hello!")This was crafted with
A Framework created by Hakim El Hattab and contributors
to make stunning HTML presentations
for-loop 2026
By Andrea Gallegati
for-loop 2026
- 1