Yogesh Tyagi
@ytyagi782
Python Loops
Explained !
While , Fo r, an d
hi ng In Be tw een
Everyt
Yogesh Tyagi
@ytyagi782
Loops in Python: Mastering
While Loops & For Loops
Learn how to efficiently repeat tasks and
iterate through data using Python's powerful
looping constructs: while and for loops.
Yogesh Tyagi
@ytyagi782
What are Loops in Python?
Loops allow you to execute a block of code
multiple times, either for a fixed number of
iterations or until a specific condition is met.
Why use loops?
Automates repetitive tasks.
Simplifies working with data structures like
lists and dictionaries.
Helps iterate over a range of values or
sequences efficiently.
Yogesh Tyagi
@ytyagi782
Types of Loops in Python
Python provides two primary types of
loops:
Repeats a block of code as
long as a condition is True.
While
Loop Example: Keep asking for user
input until it's valid.
Iterates over a sequence
(like a list, tuple, or string) or a
For range of numbers.
Loop Example: Process each item in
a list.
Yogesh Tyagi
@ytyagi782
While Loop – Introduction
The while loop repeatedly executes a block of code
as long as the given condition is True.
Syntax
The condition is checked before
Key each iteration.
Features If the condition becomes False, the
loop stops.
Yogesh Tyagi
@ytyagi782
While Loop – Example
Example
Basic While Loop
The loop starts with count = 1.
The condition count <= 5 is
Explanation: checked before each iteration.
The loop stops when count
exceeds 5.
Yogesh Tyagi
@ytyagi782
While Loop – Infinite Loops
A while loop can accidentally run forever if the
condition never becomes False.
Example
Fix: Ensure the loop has a mechanism to update
variables and eventually end.
Yogesh Tyagi
@ytyagi782
For Loop – Introduction
The for loop iterates over a sequence (like a
list, string, or range) and executes a block of
code for each element in the sequence.
Syntax
Automatically stops when the
sequence is fully iterated.
Key Best suited for situations where the
Features: number of iterations is
predetermined.
Yogesh Tyagi
@ytyagi782
For Loop – Example
The for loop iterates over a sequence (like a
list, string, or range) and executes a block of
code for each element in the sequence.
Example 1: Iterating Over a List
The loop iterates over each element
in the fruits list.
Explanation
Each element is assigned to fruit
during each iteration.
Output
Yogesh Tyagi
@ytyagi782
For Loop – Range Function
The range() function generates a sequence of
numbers, commonly used in for loops.
Syntax:
start: Starting number (default is 0).
stop: End (exclusive).
step: Increment (default is 1).
Example
Output
Yogesh Tyagi
@ytyagi782
Loop Control Statements
Python provides special statements to control
the flow of loops:
Immediately exits the loop.
break
Skips the current iteration and
moves to the next.
continue
A placeholder that does nothing.
pass
Yogesh Tyagi
@ytyagi782
While vs. For Loop
While For
Use Case
Repeats until a Iterates over
condition is False. sequences or ranges.
Condition
Must be explicitly Must be explicitly
defined. defined.
Common Use Cases
Indeterminate Determinate
iterations (e.g., user iterations (e.g.,
input). processing lists).
Yogesh Tyagi
@ytyagi782
Nested Loops
Loops can be nested inside
each other to handle complex
scenarios.
Example
The outer loop runs 3 times, and for
Explanation each iteration, the inner loop runs 2
times.
Yogesh Tyagi
@ytyagi782
Using Else with Loops
In Python, else can be used with loops to execute
a block of code after the loop finishes normally.
Example with for
Example with while:
Yogesh Tyagi
@ytyagi782
Common Errors in Loops
Infinite Loops in while:
Forgetting to update the condition variable leads to
infinite loops.
Index Errors in for:
Accessing elements outside the sequence range.
Misusing break and continue:
Forgetting how they affect loop flow can lead to
unexpected behavior.
Yogesh Tyagi
@ytyagi782
Wrap-Up
"Mastering Loops for Efficient Python
Programming"
Loops are fundamental for automating
repetitive tasks and iterating over data
structures. Use while for flexible, condition-
driven loops and for for sequence-based
iterations. Experiment with control statements
and nested loops to build powerful logic.
Happy coding!
Yogesh Tyagi
@ytyagi782
Follow for More