Learn with Yasir

Share Your Feedback

Python While Loop Exercises: Practice & Master Loops

harpen your Python skills with interactive while loop exercises. Ideal for students and developers, these practice problems help you master iterative programming concepts.

Topic: loops-while


๐Ÿงช Coding Exercises

๐ŸŸข Beginner Exercises

  1. โœ… Print Numbers from 1 to 10
  2. Write a `while` loop to print numbers from 1 to 10.
    

    Requirements

    • Use a while loop
    • Print numbers 1 through 10 inclusive

    Expected Output

    1 2 3 4 5 6 7 8 9 10

    ๐Ÿ“š ๐Ÿ“บ Watch the solution now!


  3. โœ… Sum of First N Natural Numbers
  4. Write a program that asks the user for a number `n` and calculates the sum of numbers from 1 to `n` using a `while` loop.
    

    Requirements

    • Use while loop
    • Handle user input
    • Calculate correct sum

    Input

    5

    Expected Output

    15

    ๐Ÿ“š ๐Ÿ“บ Watch the solution now!


  5. โœ… Multiplication Table
  6. Write a program to print the multiplication table of a given number using a `while` loop.
    

    Requirements

    • Use while loop
    • Print table up to 10
    • Format as 'num x i = result'

    Input

    5

    Expected Output

    5 x 1 = 5
    5 x 2 = 10
    ...
    5 x 10 = 50
    

    ๐Ÿ“š ๐Ÿ“บ Watch the solution now!


  7. โœ… Count Digits in a Number
  8. Write a program to count the number of digits in a given number using a `while` loop.
    

    Requirements

    • Use while loop
    • Handle all positive integers
    • Count digits correctly

    Input

    12345

    Expected Output

    5

    ๐Ÿ“š ๐Ÿ“บ Watch the solution now!


  9. โœ… Print Even Numbers
  10. Write a program to print even numbers between 1 and 20 using a `while` loop.
    

    Requirements

    • Use while loop
    • Print only even numbers
    • Include 20 if even

    Expected Output

    2 4 6 8 10 12 14 16 18 20

    ๐Ÿ“š ๐Ÿ“บ Watch the solution now!


  11. โœ… Sum Numbers Until Zero Input
  12. Write a Python program that continuously prompts the user to enter numbers. 
    The program should calculate and display the sum of all entered numbers. 
    The input process should terminate when the user enters the number `0`.
    

    Requirements

    • Use a `while` loop to repeatedly take input.
    • The loop must terminate when the user enters `0`.
    • Initialize a sum variable to accumulate the numbers.
    • Convert user input to an integer before performing calculations.
    • Print the final sum after the loop finishes.

    Input

    5
    7
    8
    2
    0

    Expected Output

    Sum of the numbers: 22

    ๐Ÿ“š ๐Ÿ“บ Watch the solution now!


๐ŸŸก Intermediate Exercises

  1. โœ… Reverse a Number
  2. Ask the user to enter a number and print its reverse using a `while` loop.
    

    Requirements

    • Use while loop
    • Handle multi-digit numbers
    • Properly reverse digits

    Input

    1234

    Expected Output

    4321

    ๐Ÿ“š ๐Ÿ“บ Watch the solution now!


  3. โœ… Sum of Digits
  4. Write a program to calculate the sum of digits of a given number using a `while` loop.
    

    Requirements

    • Use while loop
    • Handle multi-digit numbers
    • Calculate correct sum

    Input

    123

    Expected Output

    6

    ๐Ÿ“š ๐Ÿ“บ Watch the solution now!


  5. โœ… Count Occurrence of a Specific Digit
  6. Write a program to count occurrences of a specific digit in a number using Python.
    

    Requirements

    • Use while loop
    • Handle string or numeric input
    • Count all occurrences

    Input

    122333
    2
    

    Expected Output

    2

    ๐Ÿ“š ๐Ÿ“บ Watch the solution now!


๐Ÿ”ด Advanced Exercises

  1. โœ… Factorial Calculation
  2. Create a program to calculate the factorial of a given number using a `while` loop.
    

    Requirements

    • Use while loop
    • Handle 0 and 1 cases
    • Calculate correct factorial

    Input

    5

    Expected Output

    120

    ๐Ÿ“š ๐Ÿ“บ Watch the solution now!


๐Ÿง  Practice & Progress

Explore More Topics

๐Ÿ“˜ Learn Python

Tutorials, Roadmaps, Bootcamps & Visualization Projects

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




๐Ÿง  Python Advanced

Object-Oriented Programming in Python (OOP)

More...

๐Ÿง  Modules