Robotics & Artificial Intelligence
Explain for loop with an example.
Python Control Flow
1 Like
Answer
The for loop is a fixed iterative looping construct in Python. In this looping system, the number of iterations is fixed. The loop starts with a given value of the control variable and executes the block of statements by updating the control variable unless the last limit of the loop is reached. This loop is preferred when the user is aware of the number of times the process is to be repeated.
Syntax:
for <variable> in range(<parameters>):
<statement or a set of statements>
Example:
for i in range(1, 6):
print(i, end=' ')
Output: 1 2 3 4 5
This code displays the numbers from 1 to 5 using a for loop.
Answered By
2 Likes