Beginner Level
Intermediate Level
Advanced Level
Introduction
One of the most powerful features of Python is its capability to use loops to iterate through data sets, and the for loop is one of the most basic and commonly used loops in Python. A for loop in Python allows you to perform a set of instructions repeatedly, for a specific number of times, or for each element in a sequence, such as a list or a dictionary. This tutorial will walk you through the basics of the for loop in Python programming language, providing you with the understanding and skills necessary to effectively use loops in your Python programs.
Table of Contents :
- Introduction to Python for loop
- Range function in Python
- Starting value of range function
- Increment value of the range function
Introduction to Python for loop
- While coding a logic in a programming language sometimes we need to execute a code-block multiple times over and over again.
- One of the technique of achieving this by using a for loop.
- The syntax of a for loop in Python is as follows -
for index in range(n):
For-code-block
- Here :
- index is called a loop counter.
- We can use the name of our choice for the loop counter. It need not be index always.
- range is a built in function used to generate sequence of integers for iteration.
- Here range will generate a sequence of n integers.
- In other words Python interpreter will execute the For-code-block n times in this case.
- In every iteration of the for loop one value from the generated sequence is assigned to the loop counter index.
Range function in Python
- range() is a built-in function in Python.
- range() function generates sequence of integers which can be iterated over.
- range(n) generates a sequence of n integers
- The sequence starts from zero and goes up-to integer n - 1 with an increment of 1.
- i.e. range(n) generates a sequence of numbers: 0,1, 2, …n-1.
- Code Sample :
for index in range(5):
sqr = index ** 2
print(f"Value of index = {index}")
print(f"Square of index = {sqr}")
print()
# Output
# Value of index = 0
# Square of index = 0
# Value of index = 1
# Square of index = 1
# Value of index = 2
# Square of index = 4
# Value of index = 3
# Square of index = 9
# Value of index = 4
# Square of index = 16
Starting value of range function :
- By default the sequence generated by the range() function starts from zero.
- We can change this behavior by specifying the start parameter in the range() function
- range(start_value, stop_value)
- Now the sequence generated starts from start_value and goes up-to stop_value - 1.
- The numbers are incremented by 1 in the sequence.
- Code Sample :
for index in range(2, 5):
sqr = index ** 2
print(f"Value of index = {index}")
print(f"Square of index = {sqr}")
print()
# Output
# Value of index = 2
# Square of index = 4
# Value of index = 3
# Square of index = 9
# Value of index = 4
# Square of index = 16
Increment value of the range function :
- By default the numbers in the sequence generated by the range function are incremented by one in each iteration.
- We can change this behavior by specifying the step parameter in the range() function
- range(start_value, stop_value, step_value)
- Now the numbers will be incremented by the step_value.
- Code Sample :
for index in range(2, 15, 3):
sqr = index ** 2
print(f"Value of index = {index}")
print(f"Square of index = {sqr}")
print()
# Output
# Value of index = 2
# Square of index = 4
# Value of index = 5
# Square of index = 25
# Value of index = 8
# Square of index = 64
# Value of index = 11
# Square of index = 121
# Value of index = 14
# Square of index = 196
Prev. Tutorial : Ternary Operator in Python
Next Tutorial : While loops