While loop: Printing more numbers in Python


Practice makes perfect! Let's check two more examples of printing numbers using the while loop.


Example 1:

Let's print all the even numbers from 4 through 14:

i = 4
while i <= 14:
    print(i)
    i += 2

The output of this program is:

4
6
8
10
12
14

Example 2:

We can also loop in reverse. Let's print all even numbers from 12 to 4 in decreasing order:

i = 12
while i >= 4:
    print(i)
    i -= 2

The output of this program is:

12
10
8
6
4

Assignment
Let's print all the odd numbers from 23 to 7 in decreasing order!


Hint
Look at the examples above if you get stuck.


Introduction

In this lesson, we will explore the use of the while loop in Python to print sequences of numbers. The while loop is a fundamental control flow statement that allows code to be executed repeatedly based on a given Boolean condition. Understanding how to use while loops effectively is crucial for tasks that require repetitive actions, such as iterating over a range of numbers.

Understanding the Basics

The while loop continues to execute a block of code as long as the specified condition is True. The basic syntax of a while loop is:

while condition:
    # code block to be executed

It is important to ensure that the condition will eventually become False; otherwise, the loop will run indefinitely, causing an infinite loop.

Main Concepts

Let's break down the key concepts and techniques involved in using the while loop:

We will apply these concepts in the examples provided.

Examples and Use Cases

Let's revisit the examples provided in the lesson:

Example 1: Printing Even Numbers from 4 to 14

i = 4
while i <= 14:
    print(i)
    i += 2

In this example, we initialize i to 4. The condition i <= 14 ensures the loop runs as long as i is less than or equal to 14. Inside the loop, we print the value of i and then increment it by 2. This results in printing all even numbers from 4 to 14.

Example 2: Printing Even Numbers from 12 to 4 in Reverse

i = 12
while i >= 4:
    print(i)
    i -= 2

Here, we initialize i to 12. The condition i >= 4 ensures the loop runs as long as i is greater than or equal to 4. Inside the loop, we print the value of i and then decrement it by 2. This results in printing all even numbers from 12 to 4 in decreasing order.

Assignment: Printing Odd Numbers from 23 to 7 in Decreasing Order

Now, let's solve the assignment by printing all odd numbers from 23 to 7 in decreasing order:

i = 23
while i >= 7:
    print(i)
    i -= 2

In this solution, we initialize i to 23. The condition i >= 7 ensures the loop runs as long as i is greater than or equal to 7. Inside the loop, we print the value of i and then decrement it by 2. This results in printing all odd numbers from 23 to 7 in decreasing order.

Common Pitfalls and Best Practices

When using while loops, be mindful of the following common pitfalls:

Best practices for writing clear and maintainable while loops include:

Advanced Techniques

For more advanced use cases, consider combining while loops with other control flow statements such as if statements and break or continue statements to create more complex logic.

Code Implementation

Here is the complete code implementation for the assignment:

i = 23
while i >= 7:
    print(i)
    i -= 2

This code will print all odd numbers from 23 to 7 in decreasing order.

Debugging and Testing

When debugging while loops, consider the following tips:

To test the function, you can write test cases that check the output for different ranges and conditions.

Thinking and Problem-Solving Tips

When approaching problems involving while loops:

Conclusion

In this lesson, we explored the use of the while loop to print sequences of numbers. We covered the basics, provided examples, and discussed common pitfalls and best practices. Mastering while loops is essential for tasks that require repetitive actions, and practicing these concepts will help you become more proficient in Python programming.

Additional Resources

For further reading and practice, consider the following resources: