What is the syntax for a For loop?

#to loop through something
for <variable counter> in <sequence>:
     <code to be executed, potentially referencing variable counter>

#to loop through a range
for <variable counter> in range(<start>, <end>, <increment>):
     <code to be executed, potentially referencing variable counter>
#examples
for letter in "Hello":
 print(letter)

nums=[1,2,3,4,5]
for n in nums:
 print("current number squared: ", n*n)

for n in range(1,11,2):
    print(n)

#output
H
e
l
l
o
current number squared: 1
current number squared: 4
current number squared: 9
current number squared: 16
current number squared: 25
1
3
5
7
9

 

 

 

Advertisements