Scripting >> Python >> Examples >> Loops >> How to use For loop

 Example 1

forloop.py

myfriends = ["Tom","Dick","Harry"]
print("\nMy friends are:")
for myfriend in myfriends:
   print(myfriend)
 
print("\nMy friends are:")
for index in range(0,3):
   print( "{} : {}".format(index+1,myfriends[index]) )

 Sample run

python forloop.py

My friends are:
Tom
Dick
Harry

My friends are:
1 : Tom
2 : Dick
3 : Harry