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:

for item in sequence: # Code block to execute for each item

Example 1: Iterating Over a List

fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)

Output:

apple banana cherry

Here, the for loop iterates over each item in the list fruits and prints it.

Example 2: Iterating Over a String

for char in "hello": print(char)

Output:

h e l l o

Example 3: Using range() in a for Loop

The range() function generates a sequence of numbers, which can be used with a for loop.

for i in range(5): print(i)

Output:

0 1 2 3 4

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

for i in range(3): for j in range(2): print(f"i: {i}, j: {j}")

Output:

i: 0, j: 0 i: 0, j: 1 i: 1, j: 0 i: 1, j: 1 i: 2, j: 0 i: 2, j: 1

2. while Loop

The while loop keeps executing a block of code as long as a specified condition is True.

Syntax:

while condition: # Code block to execute while the condition is True

Example 1: Basic while Loop

i = 1 while i <= 5: print(i) i += 1

Output:

1 2 3 4 5

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.

i = 1 while True: print(i) i += 1 if i > 3: break

Output:

1 2 3

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

for i in range(5): if i == 3: break print(i)

Output:

0 1 2

The loop exits when i == 3 due to the break statement.

Example: Using continue

for i in range(5): if i == 2: continue print(i)

Output:

0 1 3 4

The continue statement skips the iteration when i == 2, so 2 is not printed.

Example: Using pass

for i in range(5): if i == 2: pass # Placeholder for future code print(i)

Output:

0 1 2 3 4

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:

for i in range(3): print(i) else: print("Loop finished")

Output:

0 1 2 Loop finished

The else block executes after the loop finishes iterating through all elements.

Example with break:

for i in range(3): if i == 1: break print(i) else: print("Loop finished")

Output:

0

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 is True.
  • 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 by break.

Loops are a fundamental way to handle repetitive tasks and iterating over collections in Python.