Python Loops
Python Loops
1
INTRODUCTION TO LOOPS
2
PURPOSE OF LOOPS
Reduction of Redundancy
3
TYPES OF LOOPS
4
FOR LOOP
The for loop iterates over each item in the
specified sequence, executing the code block for
each iteration.
SYNTAX
5
FLOWCHART OF FOR LOOP
For Each Item In Sequence
True
Expression
False
Statement(s)
Exit from the
FOR loop
6
FOR LOOP in PYTHON ?
7
FOR LOOP in PYTHON ?
8
FOR LOOP in PYTHON ?
9
FOR LOOP in PYTHON ?
10
FOR LOOP in PYTHON ?
11
FOR LOOP in PYTHON ?
12
FOR LOOP in PYTHON ?
13
FOR LOOP in PYTHON ?
14
FOR LOOP in PYTHON ?
15
WHILE LOOP
A while loop is a control flow structure in Python
that allows a block of code to be executed
repeatedly as long as a specified condition is true.
while condition:
# Code block to be executed
SYNTAX
16
FLOWCHART OF WHILE LOOP
Enter the while loop
False
Expression
True
Statement(s)
Exit from the
While loop
17
Use cases of WHILE LOOP in PYTHON ?
Indefinite Iteration:
Example:
user_input = ""
while user_input != "exit":
user_input = input("Enter a command (type 'exit' to end): ")
print("Processing:", user_input)
18
Use cases of WHILE LOOP in PYTHON ?
Example:
age = -1
while age < 0 or age > 120:
age = int(input("Enter your age (between 0 and 120): "))
print("Valid age entered:", age)
19
Use cases of WHILE LOOP in PYTHON ?
Handling events :
20
NESTED LOOP in PYTHON ?
21
NESTED LOOP in PYTHON ?
for i in range(2):
for j in range(3):
for k in range(4):
print(f"({i}, {j}, {k})")
22