Nearby lessons
49 of 108Python - For Loop
What is a for Loop?
If we want to execute a group of statements for every element present in a sequence, we use the for loop.
A sequence can be a string, list, tuple, set, range, or dictionary.
Syntax of for Loop
main.py
No output captured.
Another Syntax
main.py
No output captured.
Flow of for Loop
- Take the first element from the sequence.
- Execute the loop body.
- Take the next element.
- Repeat until all elements are processed.
- When there are no more elements, the loop stops.
range() Function
The range() function is used to generate a sequence of numbers.
It returns a range object.
Three Forms of range()
| Form | Description |
|---|---|
range(n) |
Generates numbers from 0 to n-1. |
range(begin, end) |
Generates numbers from begin to end-1. |
range(begin, end, step) |
Generates numbers using the specified step value. |
Form 1 - range(n)
This form generates numbers from 0 to n-1.
Example 1 - Print Numbers from 0 to 9
main.py
0 1 2 3 4 5 6 7 8 9
Explanation
range(10) generates numbers from 0 to 9.
The value 10 is not included.
Example 2 - Print "Hello" 5 Times
main.py
Hello Hello Hello Hello Hello
Form 2 - range(begin, end)
This form generates numbers from begin to end-1.
Example 1 - Print Numbers from 10 to 20
main.py
10 11 12 13 14 15 16 17 18 19 20
Explanation
The starting value is included.
The ending value is excluded.
Therefore, range(10, 21) prints numbers from 10 to 20.
Example 2 - Print Numbers from 1 to 10
main.py
1 2 3 4 5 6 7 8 9 10
Example 3 - Print Numbers from 5 to 10
main.py
5 6 7 8 9 10
Form 3 - range(begin, end, step)
The third argument specifies the increment or decrement.
Example 1 - Print Even Numbers
main.py
0 2 4 6 8 10 12 14 16 18 20
Explanation
The value increases by 2 after every iteration.
Only even numbers are printed.
Example 2 - Print Odd Numbers
main.py
1 3 5 7 9 11 13 15 17 19
Example 3 - Print Numbers from 10 to 1
main.py
10 9 8 7 6 5 4 3 2 1
Explanation
A negative step decreases the value after every iteration.
This prints the numbers in reverse order.
Example 4 - Print Even Numbers in Reverse
main.py
20 18 16 14 12 10 8 6 4 2
Example 5 - Print Numbers from 10 to 0
main.py
10 9 8 7 6 5 4 3 2 1 0
Important Notes about range()
| Form | Description |
|---|---|
range(n) |
Generates numbers from 0 to n-1. |
range(begin, end) |
Generates numbers from begin to end-1. |
range(begin, end, step) |
Generates numbers using the specified step value. |
Summary of range()
| Function | Output |
|---|---|
range(5) |
0 1 2 3 4 |
range(1, 6) |
1 2 3 4 5 |
range(0, 11, 2) |
0 2 4 6 8 10 |
range(10, 0, -1) |
10 9 8 7 6 5 4 3 2 1 |
Important Points
- The
forloop is used to iterate over a sequence. - The
range()function returns a range object. range(n)generates numbers from0ton-1.range(begin, end)generates numbers frombegintoend-1.range(begin, end, step)allows custom increment or decrement.- A negative step generates numbers in reverse order.
Quick Summary
| Topic | Description |
|---|---|
for Loop |
Iterates over each element of a sequence. |
range(n) |
0 to n-1 |
range(begin, end) |
begin to end-1 |
range(begin, end, step) |
Uses a custom step value. |