0% found this document useful (0 votes)
20 views5 pages

For Loop

The for loop iterates over elements in a sequence like a string, list, or tuple. It checks if there is an element in the sequence, assigns the element or next element to the variable, runs the statements, and exits or runs the else block before continuing the rest of the code. Nested for loops run an inner loop for each iteration of the outer loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

For Loop

The for loop iterates over elements in a sequence like a string, list, or tuple. It checks if there is an element in the sequence, assigns the element or next element to the variable, runs the statements, and exits or runs the else block before continuing the rest of the code. Nested for loops run an inner loop for each iteration of the outer loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

for Loop

The for loop is useful to iterate over the elements of sequence such as string,
list, tuple etc.
Syntax:
for var in sequence:
Statements
Rest of the Code
If NO Element
is Element in
Sequence ?

If YES then var = Element or Next Element

Statements Exit

Rest of the Code


for Loop with Range
a = range(5)
for i in a :
print(i)

for i in range(5) :
print(i)
for Loop with else
The for loop is useful to iterate over the elements of sequence such as string,
list, tuple etc. The else suite will be always executed irrespective of the
statements in the loop are executed or not.
Syntax:
for var in sequence:
Statements
else:
Statements
Rest of the Code
Nested for loop
For loop inside another for loop is known as nested for loop.
for i in range(n):
for j in range(y):
Statements
Statements
Rest of the Code

You might also like