New Batch Starts on 16th Oct.
Module: Network Fundamentals (L2 L3 Protocol Testing)
Timing: 07:00 AM to 08:00 AM IST (Weekdays)
Sign Up Now !

Chapter 6: Python While and For Loops: Examples and Explanations

Python While and For Loops

In this chapter of our Python tutorial series, we will explore two essential looping structures in Python: while loops and for loops. Loops are a fundamental part of programming, allowing you to execute a block of code repeatedly under specific conditions. In this chapter, you’ll learn how to use while loops with the else clause, how to control output formatting using Python’s print() function, and how to iterate over a range of numbers or lists with for loops. By the end of this chapter, you’ll be able to write efficient loops in Python and apply them to various real-world scenarios.

While Loop Examples in Python :

Output:

Explanation:
In this example, the while loop in Python continues to run as long as the condition b <= 5 is true. It prints the multiplication table for a = 5 and increments b after each iteration until b exceeds 5.

Using else with While Loops in Python

Explanation:
In this Python example, the else block is executed only if the while loop completes normally without encountering a break statement. If the user enters 0, the loop breaks, and the else block is skipped.

Using print Without a Newline in Python

Output:

Explanation:
Here, the end=',' argument in the print() function prevents the automatic newline after each printed value. Instead, the numbers are printed on the same line, separated by commas.

Using the range() Function in Python For Loops

Output:

Explanation:
The range() function in Python generates numbers from 0 to 9, and the for loop iterates through each number, printing them on the same line with spaces in between.

Multiplication Table with For Loops

Output:

Explanation:
This for loop prints the multiplication table for a = 5 for values of b from 1 to 4, using Python’s range() function.

For Loops with Lists in Python

Output:

Explanation:
In this Python example, a for loop iterates over a list of integers, adds 5 to each element, and prints the result.


Programming Exercises:

  1. Write a Python program to print the square of all numbers from 0 to 10.
  2. Write a Python program to find the sum of all even numbers from 0 to 10.
  3. Write a Python program to read three numbers (a, b, c) and check how many numbers between ‘a’ and ‘b’ are divisible by ‘c’.
  4. Write a Python program to print two columns of numbers, where the first column starts from 1 and increments to 99, while the second column starts from 99 and decrements to 1. Each line should display a pair of numbers in the following format:
    • 1—–99
    • 2—–98
    • 3—–97
    • 98—–2
    • 99—–1
  5. Write a Python program to read a number and print its binary representation.
  6. Write a Python program to read four numbers (representing the four octets of an IP) and print the next five IP addresses.
  7. Write a Python program to print the factorial of a given number.
  8. Write a Python program to print the first 10 numbers of the Fibonacci series.
  9. Write a Python program to read a number and print a right triangle using “*”.
  10. Write a Python program to check whether a given number is prime or not.
  11. Write a Python program to print all prime numbers between 0 and 100, and also print how many prime numbers are found.
  12. Given a, b, c = 0, 0, 0, write a Python program to print all permutations of these three variables.
    • Output: 000, 001, 002, 003, 004, 005, 006, 007, 008, 009, 010, 011 … 999
  13. Write a Python program to print all numbers from 1 to 100 that are divisible by both 3 and 5.
  14. Write a Python program to find the sum of the digits of a given number. For example, if the input is 1234, the output should be 10 (since 1 + 2 + 3 + 4 = 10).
  15. Write a Python program to read a number and print its reverse. For example, if the input is 1234, the output should be 4321.
  16. Write a Python program to print all numbers from 1 to 100 that are divisible by both 3 and 5.
  17. Write a Python program to find the sum of the digits of a given number. For example, if the input is 1234, the output should be 10 (since 1 + 2 + 3 + 4 = 10).
  18. Write a Python program to read a number and print its reverse. For example, if the input is 1234, the output should be 4321.
  19. Write a Python program to print all perfect numbers between 1 and 100. (A perfect number is a number whose divisors sum up to the number itself, e.g., 6 = 1 + 2 + 3.)
  20. Write a Python program to find the greatest common divisor (GCD) of two numbers using a loop.
  21. Write a Python program to read a number and print a diamond pattern using “*”, where the height of the diamond is twice the number minus one. For example, if the input is 3, the pattern should look like:

We’d love to hear your feedback and suggestions about this article. Feel free to reach out to us using the WhatsApp number below.

Sajith Achipra has been a trainer and testing consultant at Zframez Technologies since 2009. With 15+ years of experience, he specializes in networking, Python, development, and testing. He conducts online courses to help students and professionals enhance their skills. You can reach him on WhatsApp at +91 8884 884 844 for your training and testing requirements.