Python Loops
Loops in Python
Loops in Python are used to repeat a block of code multiple times based on a condition or over a sequence (like a list, tuple, string, etc.). Python provides two main types of loops: for
loop and while
loop.
1. for
Loop
The for
loop in Python is used to iterate over a sequence (such as a list, tuple, dictionary, set, or string) and execute a block of code for each element.
Syntax:
Example 1: Iterating Over a List
Output:
Here, the for
loop iterates over each item in the list fruits
and prints it.
Example 2: Iterating Over a String
Output:
Example 3: Using range()
in a for
Loop
The range()
function generates a sequence of numbers, which can be used with a for
loop.
Output:
In this example, the range(5)
function generates numbers from 0 to 4, and the loop prints each one.
Example 4: Nested for
Loops
You can nest for
loops to iterate over m
Output:
2. while
Loop
The while
loop keeps executing a block of code as long as a specified condition is True
.
Syntax:
Example 1: Basic while
Loop
Output:
Here, the while
loop keeps executing the code block as long as the condition (i <= 5
) is True
. Once i
becomes greater than 5, the loop terminates.
Example 2: while
Loop with a break
Statement
You can use the break
statement to exit the loop prematurely.
Output:
The loop runs indefinitely due to while True
, but the break
statement exits the loop when i
becomes greater than 3.
3. Loop Control Statements
Python provides three control statements to alter the behavior of loops:
break
: Terminates the loop entirely.continue
: Skips the current iteration and moves to the next iteration of the loop.pass
: Does nothing; it's a placeholder used when a statement is required syntactically, but no code is needed.
Example: Using break
Output:
The loop exits when i == 3
due to the break
statement.
Example: Using continue
Output:
The continue
statement skips the iteration when i == 2
, so 2
is not printed.
Example: Using pass
Output:
The pass
statement does nothing and simply serves as a placeholder, allowing the loop to continue normally.
4. else
with Loops
In Python, you can also use an else
statement with loops. The else
block is executed after the loop finishes, but not if the loop is terminated by a break
statement.
Example:
Output:
The else
block executes after the loop finishes iterating through all elements.
Example with break
:
Output:
In this case, the loop is terminated by the break
statement, so the else
block is not executed.
Summary of Loops in Python
for
loop: Iterates over a sequence (list, tuple, string, etc.).while
loop: Repeats a block of code as long as a condition isTrue
.- Loop control statements:
break
: Exits the loop prematurely.continue
: Skips the current iteration and continues with the next.pass
: Does nothing (a placeholder).
else
with loops: Runs after the loop completes normally, but skips if the loop is interrupted bybreak
.
Loops are a fundamental way to handle repetitive tasks and iterating over collections in Python.