Google News
logo
Python Program to Using the range() function in a for loop
In the following example of Python program that demonstrates how to use the `range()` function in a `for` loop :
Program :
# Using the range() function in a for loop
for i in range(5):
    print(i)

print("Done.")
Output :
0
1
2
3
4
Done.
In the above program, we use a `for` loop to iterate over a range of numbers generated by the `range()` function. The `range(5)` function generates a sequence of integers from `0` up to, but not including, `5`.

Inside the loop, we use the `print()` function to print each number in the range.

When the loop exits, the program continues to execute the `print()` statement outside the loop, which prints the message "Done."

You can use the `range()` function to generate a sequence of numbers to iterate over in a loop. The `range()` function can take one, two, or three arguments, specifying the start, end, and step size of the sequence.