October 13, 2019

Python tutorial 8 | for loop statement

This is the next of Python Tutorials for Beginners. Please view all the Python tutorials in this blog here. This Python beginner tutorial explains the for statement in Python with multiple examples. The Python for statement loops over items of any sequence like an integer series or a list. Please view the Python video tutorial 8 or read on...

In Python programming, it is simple to create the sequence of integers that you need by using the range function. You can use the continue statement or break statement in the for loop. As in the while loop, the continue statement skips the current iteration of the for loop. The break statement ends the for loop immediately.

The for statements in Python programming have the Python for loop syntax below. The variable is assigned the first value in the sequence and the code block1 (for block) is run. Then the variable is assigned the second value in the sequence and the code block1 is run again. This goes on until the sequence is complete. Then the code block2 (else block) is run once and the entire for loop statement ends. Note that the else part of the for statement is optional.

for variable(s) in sequence:
    code block1
else:
    code block2

Now, let us see Python examples of Python for loop programs.

# Python code example
# print integers from range(6) i.e. 0 through 5
# i is the Python for loop index
for i in range (6):
    print (i)
else:
    print ('End of Python for statement')

# Python code example
# print integers from the range(6) but exclude 3 i.e. 0, 1, 2, 4, 5
for i in range (6):
    if i == 3:
        continue # skip current iteration of the loop
    print (i)
else:
    print ('End of for statement')

# Python code example
# print integers from the range(6) but up to 2 i.e. 0, 1, 2
for i in range (6):
    if i == 3:
        # terminate the for loop immediately
        # even the else block (code block2) is not run
        break
    print (i)
else:
    print ('End of for statement')

# Python code sample
# print integers from range(2, 6) i.e. 2, 3, 4, 5
# i is the Python for loop index 
for i in range (2,6):
    print (i)
else:
    print ('End of Python for loop')

# Python code sample
# print integers from range starting from 1, ending up to but excluding 10
# the Python for loop step is 3
# in other words, print 1, 4 and 7
for i in range (1,10,3):
    print (i)
else:
    print ('End of Python for loop')

# Python code sample
# print integers from range starting from 10, ending up to but excluding 0
# the Python for loop step is -2
# in other words, print 10, 8, 6, 4 and 2
for i in range (10,0,-2):
    print (i)
else:
    print ('End of Python for loop')

# Python code sample
# print strings from a Python list
words = ['alpha', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot']
for x in words:
    print (x)
# the else block is optional

Want to learn how these Python samples work? Or learn about nested loops (for loop inside another for loop) in Python programming? View my Python video tutorial 8. Thank you.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.