PYTHON
“A Versatile and powerful programming
language”
1
INTRODUCTION TO LOOPS
Loops are control structures in Python that
allow the execution of a block of code
repeatedly.
It helps in automating repetitive tasks and
iterating over a sequence of elements.
2
PURPOSE OF LOOPS
Efficiency in Code Execution
Handling Iterative Tasks
Reduction of Redundancy
Flexibility and Adaptability
Streamlining Control Flow
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.
for variable in sequence:
# Code block to be executed
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 ?
Iterating Over Elements in a Sequence:
It iterates over each element in a sequence (e.g., List, tuple,
string).
Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print("Current Fruit:", fruit)
7
FOR LOOP in PYTHON ?
Using range() for Numeric Iteration:
It is useful for iterating over numeric ranges.
Example:
for i in range(1, 6):
print("Number:", i)
8
FOR LOOP in PYTHON ?
Enumerating Indices and Values:
It is used to iterate over both the index and value of elements
in a sequence.
Example:
colors = ["red", "green", "blue"]
for index, color in enumerate(colors):
print("Index:", index, "| Color:", color)
9
FOR LOOP in PYTHON ?
Iterating Over Dictionary Items
It is used to iterate over key-value pairs in a dictionary.
Example:
person = {"name": "John", "age": 30, "city": "New York"}
for key, value in person.items():
print(key + ":", value)
10
FOR LOOP in PYTHON ?
Nested For Loops:
It is useful for iterating over combinations or permutations,
especially in scenarios like matrix traversal.
Example:
for i in range(3):
for j in range(3):
print(f"({i}, {j})")
11
FOR LOOP in PYTHON ?
Using else with For Loop:
The else block is executed after the loop has exhausted
its items, i.e., when the loop condition becomes false..
Example:
for i in range(5):
print(i)
else:
print("Loop Finished")
12
FOR LOOP in PYTHON ?
Iterating Over Strings:
A for loop can be used to iterate over each character
in a string.
Example:
word = "Python"
for char in word:
print("Character:", char)
13
FOR LOOP in PYTHON ?
Using zip() for Parallel Iteration:
The zip() function is used for parallel iteration over
multiple sequences.
Example:
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
for name, age in zip(names, ages):
print(f"{name} is {age} years old.")
14
FOR LOOP in PYTHON ?
Using break and continue in For Loop:
Break is used to exit the loop prematurely.
Continue is used to skip the rest of the code inside the loop
for the current iteration.
Example:
for i in range(10):
if i == 5:
break # exits the loop when i is 5
print(i)
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:
A while loop when the number of iterations is not known beforehand.
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 ?
User Input Validation :
A while loop to validate user input until it meets certain criteria .
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 :
while loops to continuously monitor and respond to events
Example:
user_response = ""
while user_response.lower() != "yes":
user_response = input("Do you want to continue? (yes/no): ")
print("Processing...")
print("Continuing with the next step.")
20
NESTED LOOP in PYTHON ?
Nested loops mean loops inside a loop
21
NESTED LOOP in PYTHON ?
Three-Dimensional Nested Loop
for i in range(2):
for j in range(3):
for k in range(4):
print(f"({i}, {j}, {k})")
22