Tutorialspoint

What is basic syntax of Python for Loops?

What is basic syntax of Python for Loops?                                           


1 Answer
Malhar Lathkar
, 0 Views

Python's for loop executes the body of loop for each object in a collection such as string, list, tuple or dictionary. Its usage is as follows:

for obj in seq:
    stmt1
    stmt2

Following code snippet iterates over each number element of the list and prints its square

L1=[1,2,3,4,5]
for x in L1:
    print (x*x)

The output will be

1
4
9
16
25
Advertisements