Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
BOOK 000 · CHAPTER 4 · For Loops

For Loops

On this pageBasic ExampleWorking with StringsUsing range(stop) FunctionUsing range(start,stop,step) FunctionExample with User InputMini Project — Multiplication Table Generator

A for loop is used to repeat a block of code a specific number of times or to go through each item in a sequence, such as a list, tuple, or string.

A for loop requests one item at a time from an iterable, something that supplies successive items, such as a list or string. It stops when no items remain; it does not need to know the number of iterations in advance.

Basic Example

Let’s begin with a simple example that prints each fruit in a list:

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
    print("I like", fruit)
Listing 120 — Basic Example

Output

I like apple
I like banana
I like cherry

Here, Python goes through the list fruits one element at a time, temporarily storing each value in the variable fruit.

The loop ends automatically after the last item.

This type of loop is very common in data processing, where you need to perform the same action for each element in a dataset.

Working with Strings

A for loop can also be used to go through each character in a string:

for letter in "Python":
    print(letter)
Listing 121 — Working with Strings

Output

P
y
t
h
o
n

This is often used when you want to check or modify characters individually - for example, counting vowels or formatting text.

Using range(stop) Function

When you need to repeat an action a specific number of times, the built-in range(stop) function is ideal.

It creates a sequence of numbers for the loop to iterate over, stopping just before the value defined by stop.

for i in range(5):
    print("Iteration", i)
Listing 122 — Using range(stop) Function

Output

Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4

The function range(5) creates numbers from 0 to 4, stopping before 5.

Using range(start,stop,step) Function

You can also start at a specific number or set a custom step value using range(start, stop, step), which lets you control where the sequence begins, where it ends, and how much it increases with each loop iteration.

for i in range(2, 10, 2):
    print(i)
Listing 123 — Using range(start,stop,step) Function

Output

2
4
6
8

This prints only the even numbers from 2 up to, but not including, 10.

Example with User Input

Now, let’s make the loop interactive.

This program asks the user for a number and prints all numbers from 1 up to that number.

limit = int(input("Enter a number: "))

for i in range(1, limit + 1):
    print(i)
Listing 124 — Example with User Input

Example Run:

Enter a number: 5
1
2
3
4
5
Listing 125 — Example with User Input

Here, the program uses the value provided by the user to control how many times the loop runs.

It demonstrates how for and range() can be used together to create flexible repetition.

Mini Project — Multiplication Table Generator

Write a program that displays the multiplication table for a number entered by the user. The program should print the results from 1 × number up to 10 × number.

Example Run:

Enter a number: 4

Multiplication table for 4:
4 × 1 = 4
4 × 2 = 8
4 × 3 = 12
4 × 4 = 16
4 × 5 = 20
4 × 6 = 24
4 × 7 = 28
4 × 8 = 32
4 × 9 = 36
4 × 10 = 40
Listing 126 — Mini Project — Multiplication Table Generator

This small project demonstrates how for loops can automate repetitive calculations, a key skill for tasks such as data processing, analysis, and report generation.