Here we are going to discuss, how to define a simple ‘for’ loop.

forloop1Look at the code snippet:

for num in range (10):
       print(num)

This is a simple ‘for’ loop which numbers from to 9. 

Here ‘num’ is variable, which is used in the ‘for’ loop.

‘in range’ is a key word which is used to define the number of iterations to be carried out in the ‘for’ loop.

when we compare with the ‘for’ loop statement in c++, we doesn’t need an Updation statement here.

Updation of the variable ‘num’ is carried out internally by our language.

And the statement inside the ‘for’ loop gets executed as long as the iteration of the ‘for’ loop continues.

And thus we obtain the following result.

forloop2

Advertisements