for loop in Python
A for loop is used to iterate over a sequence.
We use a for loop when we already know how many times the loop should run.
Sequence means an ordered collection of elements. That is, values are arranged in order (1st, 2nd, 3rd, …). Values can be accessed using an index. Examples: list, range, etc.
Iterate means accessing each element of a sequence one by one and processing it. Iteration is usually done using loops like
Before learning about the for loop, let us first understand
Sequence means an ordered collection of elements. That is, values are arranged in order (1st, 2nd, 3rd, …). Values can be accessed using an index. Examples: list, range, etc.
Iterate means accessing each element of a sequence one by one and processing it. Iteration is usually done using loops like
for loop and while loop. Before learning about the for loop, let us first understand
range().
range() in Python
In Python, the
range() function is used to create a sequence of numbers.
It is mostly used inside loops (especially for loops).
Syntax:
range(start, stop, step)
Here:
1. start → Starting value (default = 0)
2. stop → Ending value (stop value is not included)
3. stop → The stop value is mandatory in range()
4. step → Increment value for each iteration (default = 1)
Example:
range(5)
Explanation:
1. It creates a sequence of numbers.
2. Default values: start = 0, stop = 5, step = 1
3. Numbers generated: 0 to 4 (5 is not included)
for loop in Python
A for loop is an entry-control loop.
It is used when the number of times a code block should execute is known in advance.
Syntax:
for variable in sequence:
statements
Here:
1. variable → Holds one value from the sequence in each iteration
2. sequence → A collection like list, tuple, string, or range
3. statements → Code that runs for each value in the sequence
Example: Finding the sum of first 100 natural numbers using for loop
# Finding sum of first 100 Natural Numbers
sum = 0
for i in range(1, 101):
sum += i
print("Sum:", sum)
Output:
Sum: 5050
Sum: 5050
-
sum = 0
Initializes the variablesumwith 0 to store the total. -
for i in range(1, 101):
range(1, 101)generates numbers from 1 to 100 (101 is not included). -
sum += i
Adds each value ofitosumon every iteration. -
print("Sum:", sum)
Prints the final sum.
Example using range(stop)
# Printing values from 0 to 4
for i in range(5):
print(i)
Output:
0
1
2
3
4
0
1
2
3
4
start = 0 (default)
stop = 5
step = 1 (default)
stop = 5
step = 1 (default)
Example using range(start, stop)
# Printing values from 1 to 5
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
1
2
3
4
5
Example using range(start, stop, step)
# Printing odd numbers up to 10
for i in range(1, 10, 2):
print(i)
Output:
1
3
5
7
9
1
3
5
7
9
Example using range() with negative step
# Printing even numbers in descending order
for i in range(10, 1, -2):
print(i)
Output:
10
8
6
4
2
10
8
6
4
2
When to use for loop?
- Counting loops (printing numbers, calculating sum or factorial)
- Fixed number of iterations (repeat a task exactly N times)
What we learned so far
- What a for loop is
- How to write programs using a for loop
- break and continue statements will be covered in the next topic