Loops in Python

In Python, loops are used to repeat a block of code multiple times. There are two common types of loops: for loops and while loops.

For Loop in Python: A Beginner's Guide

A for loop in Python is used to repeat a block of code multiple times. It’s like saying, “Do this action again and again until we are done.”

Why Use a For Loop?

Imagine you want to print "Hello" 5 times. Instead of writing the same line 5 times, you can use a for loop to repeat it easily.

Example: Print "Hello" 5 Times

        
# This will print "Hello" 5 times
for i in range(5):  # range(5) creates numbers 0 to 4
    print("Hello")
        
    

Important Note:

  • range(5) creates a sequence of numbers starting from 0 and stopping just before 5. So, the numbers generated are: 0, 1, 2, 3, 4.
  • This is why the loop runs exactly 5 times, one for each number.

Output:

Hello
Hello
Hello
Hello
Hello
    

How Does a For Loop Work?

A for loop in Python repeats a block of code for each item in a sequence (like numbers). Let’s break it down step by step with simple examples.

Example 1: Printing "Hello" 5 Times

        
# This will print "Hello" 5 times
for i in range(5):  # range(5) creates numbers 0 to 4
    print("Hello")
        
    

Step-by-Step Explanation:

  1. range(5) generates a sequence of numbers: 0, 1, 2, 3, 4. It starts from 0 and stops just before 5. So the loop will run 5 times.
  2. for i in range(5) means the variable i will take each number in range(5) one by one.
  3. The code inside the loop (print("Hello")) runs once for each value of i.

Breaking It Down:

Here’s what happens step by step:

  1. First Iteration (i = 0): "Hello" is printed.
  2. Second Iteration (i = 1): "Hello" is printed.
  3. Third Iteration (i = 2): "Hello" is printed.
  4. Fourth Iteration (i = 3): "Hello" is printed.
  5. Fifth Iteration (i = 4): "Hello" is printed.
  6. After i = 4, there are no more numbers in range(5), so the loop stops.

Output:

Hello
Hello
Hello
Hello
Hello
    

Example 2: Counting Numbers from 1 to 5

        
# Print numbers from 1 to 5
for number in range(1, 6):  # range(1, 6) gives 1, 2, 3, 4, 5
    print(number)
        
    

Step-by-Step Explanation:

  1. range(1, 6) generates a sequence of numbers: 1, 2, 3, 4, 5. It starts from 1 and stops just before 6.
  2. for number in range(1, 6) means the variable number will take each value in range(1, 6) one by one.
  3. The code inside the loop (print(number)) runs once for each value of number.

Breaking It Down:

Here’s what happens step by step:

  1. First Iteration (number = 1): 1 is printed.
  2. Second Iteration (number = 2): 2 is printed.
  3. Third Iteration (number = 3): 3 is printed.
  4. Fourth Iteration (number = 4): 4 is printed.
  5. Fifth Iteration (number = 5): 5 is printed.
  6. After number = 5, there are no more numbers in range(1, 6), so the loop stops.

Output:

1
2
3
4
5
    

Key Points to Remember:

  • range() always starts from the first number and stops just before the second number.
  • A for loop runs the code inside it for each number in the sequence generated by range().
  • Loops save time by repeating actions automatically, without writing the same code again and again.

Explanation:

  • range(1, 6) starts from 1 and stops just before 6, so the numbers are 1, 2, 3, 4, 5.

Output:

1
2
3
4
5
    

Why Learn For Loops?

- For loops help you repeat actions without writing the same code again and again.
- They are useful when you know how many times you want to repeat something.
- They make your programs shorter and easier to understand.

Practice writing your own for loops to get comfortable!

While Loop in Python

A while loop is used to repeat a set of instructions as long as a condition is true. It is useful when we don’t know exactly how many times we need the loop to run.

Think of it like this:

  • First, the computer checks if the condition is true.
  • If it is true, the code inside the loop runs.
  • After running, it checks the condition again.
  • This continues until the condition becomes false.

Here’s an example to make it clear:

        
# This code will print "Hello" 5 times
i = 0  # Start with i = 0
while i < 5:  # Check if i is less than 5
    print("Hello")  # Print "Hello"
    i += 1  # Add 1 to i (so it gets closer to 5)
        
    

Explanation of the example:

  • i = 0: This is the starting point.
  • while i < 5: The loop will keep running as long as i is less than 5.
  • i += 1: This increases i by 1 after each loop. If we forget this step, the loop will run forever!

Final Output: The word "Hello" will be printed 5 times because the loop stops when i becomes 5.

Practice Challenge

Try using both loops to print numbers from 1 to 10.

For Loop Example:

        
for i in range(1, 11):
    print(i)
        
        

While Loop Example:

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

Test Your Knowledge: Loops in Python

Now, let's check your understanding of loops in Python! Answer the following questions:

1. Which loop is used to iterate over a sequence (like a list or a range) in Python?

2. What will be the output of the following code?

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

3. What will be the output of the following code?

x = 0
while x < 3:
    print(x)
    x += 1
                

4. How many times will the following loop execute?

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

Answer the questions and check how well you've understood loops in Python!