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 from0
and stopping just before5
. 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:
-
range(5)
generates a sequence of numbers:0, 1, 2, 3, 4
. It starts from0
and stops just before5
. So the loop will run 5 times. -
for i in range(5)
means the variablei
will take each number inrange(5)
one by one. -
The code inside the loop (
print("Hello")
) runs once for each value ofi
.
Breaking It Down:
Here’s what happens step by step:
- First Iteration (
i = 0
): "Hello" is printed. - Second Iteration (
i = 1
): "Hello" is printed. - Third Iteration (
i = 2
): "Hello" is printed. - Fourth Iteration (
i = 3
): "Hello" is printed. - Fifth Iteration (
i = 4
): "Hello" is printed. - After
i = 4
, there are no more numbers inrange(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:
-
range(1, 6)
generates a sequence of numbers:1, 2, 3, 4, 5
. It starts from1
and stops just before6
. -
for number in range(1, 6)
means the variablenumber
will take each value inrange(1, 6)
one by one. -
The code inside the loop (
print(number)
) runs once for each value ofnumber
.
Breaking It Down:
Here’s what happens step by step:
- First Iteration (
number = 1
):1
is printed. - Second Iteration (
number = 2
):2
is printed. - Third Iteration (
number = 3
):3
is printed. - Fourth Iteration (
number = 4
):4
is printed. - Fifth Iteration (
number = 5
):5
is printed. - After
number = 5
, there are no more numbers inrange(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 from1
and stops just before6
, so the numbers are1, 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 asi
is less than 5.i += 1
: This increasesi
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!