The For Loop:
Python implements an iterator-based ‘for loop’.It is a type of ‘for loop’ that iterates over a list of items through an explicit or implicit iterator.
The Loop is introduced by the keyword ‘for’ which is followed by a random variable name which contain the values supplied by the object.
This is the syntax of Python ‘for Loop’
for variable in list:
1.Display natural from 1 to 10 using “For loop”
i=1 n=int(input("Enter the number up to which you want to print the natural numbers?")) for i in range(0,10): print(i,end = ' ') OUTPUT: 0 1 2 3 4 5 6 7 8 9
Using range function with for loop:
The range() function can be combined with the ‘for loop’ to supply the numbers required by the loop.In the following example,the range(1,x+1) provided the numbers 1 to 50 needed by the ‘for loop’ to add the sum of 1 until 50:
x=50 total=0 total=total+number print("Sum of 1 until %d:%d"%(x,total)) OUTPUT: Sum of 1 until 50:1275