What is basic syntax of Python for Loops?



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 −

Example

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)

Output

The output will be

1
4
9
16
25

Advertisements