Nearby lessons

43 of 159

Python - For Loop

📌 What You Will Learn
  • Define the for loop and the sequences it can iterate over
  • Understand the three forms of the range() function
  • Use range(n) to generate numbers from 0 to n-1
  • Use range(begin, end) to print a range of numbers
  • Use range(begin, end, step) to print even, odd and reverse-order numbers

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

🐍Code Cell
1for variable in sequence:
2 statement
Output
No output captured.

Another Syntax

🐍Code Cell
1for variable in sequence:
2 statement1
3 statement2
4 statement3
Output
No output captured.

Flow of for Loop

  1. Take the first element from the sequence.
  2. Execute the loop body.
  3. Take the next element.
  4. Repeat until all elements are processed.
  5. 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

🐍Code Cell
1for x in range(10):
2 print(x)
Output
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

🐍Code Cell
1for x in range(5):
2 print("Hello")
Output
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

🐍Code Cell
1for x in range(10, 21):
2 print(x)
Output
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

🐍Code Cell
1for x in range(1, 11):
2 print(x)
Output
1
2
3
4
5
6
7
8
9
10

Example 3 - Print Numbers from 5 to 10

🐍Code Cell
1for x in range(5, 11):
2 print(x)
Output
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

🐍Code Cell
1for x in range(0, 21, 2):
2 print(x)
Output
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

🐍Code Cell
1for x in range(1, 21, 2):
2 print(x)
Output
1
3
5
7
9
11
13
15
17
19

Example 3 - Print Numbers from 10 to 1

🐍Code Cell
1for x in range(10, 0, -1):
2 print(x)
Output
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

🐍Code Cell
1for x in range(20, 0, -2):
2 print(x)
Output
20
18
16
14
12
10
8
6
4
2

Example 5 - Print Numbers from 10 to 0

🐍Code Cell
1for x in range(10, -1, -1):
2 print(x)
Output
10
9
8
7
6
5
4
3
2
1
0
📝 Key Takeaways
  • The for loop executes a group of statements for every element in a sequence
  • A sequence can be a string, list, tuple, set, range or dictionary
  • range() generates a sequence of numbers and returns a range object
  • range(n) generates numbers from 0 to n-1
  • A negative step value prints numbers in reverse order

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3