For Loop in Python3 - Coders PlayGround

Learn to code and change the future!

Tuesday, 29 August 2017

For Loop in Python3


Hello friends, lets learn about for loop in python.
The for loop in python is bit different than C. 

Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.


Check out this program:

list1 = [1,5,4,6,9,8,7,1,5,2,0,1,3]

for eachno in list1:
  print (eachno)

# highlight and tab to indent
# indentation is important in Python


print ("Using range function...")
# xrange in Python 2.7
# range built in function

for x in range(1,11):
  print (x)


# prints from 1 --> 10

 
Output
PropellerAds