Computer >> Computer tutorials >  >> Programming >> Python

How to use for loop in Python?


The for loop in Python is used to iterate over a number of elements or some specific integer range. The elements might be of an array, string or any other iterative object in Python.

The for loop is the most frequently used looping statement. Most of the programming questions we encounter make use of the for loop in its solution.

For in range loop

Python has for an range loop. It takes the two integer values which specify the range within which the variable must iterate. If only one integer parameter is specified, then the specified integer is taken as the end of the range and the starting is 0 by default.

Syntax

for variable in range( starting value, end value)
for variable in range( end value)

Note: The for loop in Python iterates one less than the end value. This means that if the starting and end values are 1 and 5 respectively, then the loop will iterate for 1,2,3,4. The starting value is inclusive but the end value is not inclusive.

Example

for i in range(1,5):
   print(i,end=" ")
print()
for i in range(5):
   print(i,end=" ")

Output

1 2 3 4
0 1 2 3 4

Reverse for loop

The for in range loop can take a third parameter -1, which specifies that the for loop will iterate in reverse order. The loop starts from starting value and iterates in decreasing order to the end value(not inclusive).

Example

for i in range(5,0,-1):
   print(i,end=" ")

Output

5 4 3 2 1

Specify third parameter in the for loop

The third parameter in the for loop can be modified to use for loop in different ways. The third parameter, in actual, specifies the steps the variable should jump. If not specified, it is 1 by default.

If the third parameter is negative, it specifies that the loop will iterate in reverse order.

The number of steps (jumps) backwards will be specified by this parameter.

If the parameter is positive, the loop will iterate in forward order. The number of steps forward will be specified by this parameter.

Let’s understand with the help of the below example.

Example

for i in range(0,11,2):
   print(i,end=" ")
print()
for i in range(10,-1,-2):
   print(i,end=" ")

Output

0 2 4 6 8 10
10 8 6 4 2 0

For in loop

This loop is used to iterate over a iterable object such as string or array. It cannot be used to iterate over some specific integer range.

Syntax

for variable in iterable object

This loop iterates over all the elements of the iterable object one by one without taking into account their index. If indexes are needed, use for in range loop.

Example

st="Tutorials"
for i in st:
   print(i,end=" ")
print()
array=[2,4,6,8,10]
for i in array:
   print(i,end=" ")
print()
for i in range(len(array)):
   print(array[i],end=" ")

Output

T u t o r i a l s
2 4 6 8 10
2 4 6 8 10

Note: As clear from the above example, the variable in for in loop holds the elements of the iterable object one by one, whereas the variable in for in range loop holds the indexes of the elements.