Python Loops Practice Questions with Solutions

Python Loops Practice Questions with Solutions allow you to execute a block of code multiple times without writing the same code repeatedly. In this practice set, you’ll learn how to use for loops, while loops, the range() function, and loop control statements through practical Python programs.


1. Python Program to Print Numbers from 1 to 10 Using a for Loop

Problem Statement

Write a Python program to print numbers from 1 to 10 using a for loop.

Expected Output

1
2
3
4
5
6
7
8
9
10

Python Solution

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

Explanation

The range(1, 11) function generates numbers from 1 to 10.

Concepts Covered

  • for loop
  • range()

2. Python Program to Print Even Numbers from 1 to 20

Problem Statement

Write a Python program to print all even numbers from 1 to 20.

Expected Output

2
4
6
8
10
12
14
16
18
20

Python Solution

for number in range(2, 21, 2):
    print(number)

Explanation

The third argument of range() specifies the step value.

Concepts Covered

  • for loop
  • range(start, stop, step)

3. Python Program to Calculate the Sum of First 10 Natural Numbers

Problem Statement

Write a Python program to calculate the sum of numbers from 1 to 10.

Expected Output

Sum: 55

Python Solution

total = 0

for number in range(1, 11):
    total += number

print("Sum:", total)

Explanation

The variable total stores the running sum of all numbers.

Concepts Covered

  • for loop
  • Accumulator pattern

4. Python Program to Print the Multiplication Table of a Number

Problem Statement

Write a Python program to print the multiplication table of a given number.

Python Solution

number = int(input("Enter a number: "))

for i in range(1, 11):
    print(f"{number} x {i} = {number * i}")

Sample Output

Enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50

Explanation

The loop repeats 10 times to generate the multiplication table.

Concepts Covered

  • for loop
  • range()
  • Formatted output

5. Python Program to Count the Digits in a Number

Problem Statement

Write a Python program to count the total number of digits in a given number.

Python Solution

number = int(input("Enter a number: "))
count = 0

while number != 0:
    number //= 10
    count += 1

print("Total Digits:", count)

Sample Output

Enter a number: 54892
Total Digits: 5

Explanation

Each iteration removes the last digit until the number becomes zero.

Concepts Covered

  • while loop
  • Integer division

6. Python Program to Reverse a Number

Problem Statement

Write a Python program to reverse a given number.

Python Solution

number = int(input("Enter a number: "))
reverse = 0

while number > 0:
    digit = number % 10
    reverse = reverse * 10 + digit
    number //= 10

print("Reversed Number:", reverse)

Sample Output

Enter a number: 12345
Reversed Number: 54321

Explanation

The last digit is extracted and added to the reversed number in each iteration.

Concepts Covered

  • while loop
  • Modulus operator

7. Python Program to Check Whether a Number is Prime

Problem Statement

Write a Python program to check whether a given number is prime.

Python Solution

number = int(input("Enter a number: "))

if number <= 1:
    print("Not a Prime Number")
else:
    is_prime = True

    for i in range(2, number):
        if number % i == 0:
            is_prime = False
            break

    if is_prime:
        print("Prime Number")
    else:
        print("Not a Prime Number")

Sample Output

Enter a number: 13
Prime Number

Explanation

A prime number has exactly two factors: 1 and itself.

Concepts Covered

  • for loop
  • break
  • Prime number logic

8. Python Program to Print a Right Triangle Star Pattern

Problem Statement

Write a Python program to print the following star pattern.

Expected Output

*
**
***
****
*****

Python Solution

for row in range(1, 6):
    print("*" * row)

Explanation

The number of stars increases by one in each row.

Concepts Covered

  • Nested logic
  • Pattern printing

9. Python Program to Demonstrate break Statement

Problem Statement

Write a Python program that prints numbers from 1 to 10 but stops when it reaches 6.

Python Solution

for number in range(1, 11):
    if number == 6:
        break

    print(number)

Expected Output

1
2
3
4
5

Explanation

The break statement immediately terminates the loop.

Concepts Covered

  • break
  • Loop control statements

10. Python Program to Demonstrate continue Statement

Problem Statement

Write a Python program to print numbers from 1 to 10, but skip the number 5.

Python Solution

for number in range(1, 11):
    if number == 5:
        continue

    print(number)

Expected Output

1
2
3
4
6
7
8
9
10

Explanation

The continue statement skips the current iteration and moves to the next one.

Concepts Covered

  • continue
  • Loop control statements

11. Python Program to Print the Multiplication Table of a Number

Problem Statement

Write a Python program to print the multiplication table of a number entered by the user using a for loop.

Python Solution

number = int(input("Enter a number: "))

print(f"\nMultiplication Table of {number}")

for i in range(1, 11):
    print(f"{number} x {i} = {number * i}")

Sample Output

Enter a number: 7

Multiplication Table of 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Explanation

The program uses a for loop with the range() function to iterate from 1 to 10 and prints the multiplication table.

Concepts Covered

  • for Loop
  • range()
  • Arithmetic Operations
  • User Input

12. Python Program to Count Even and Odd Numbers in a Given Range

Problem Statement

Write a Python program to count how many even and odd numbers exist between 1 and a number entered by the user.

Python Solution

limit = int(input("Enter the ending number: "))

even = 0
odd = 0

for i in range(1, limit + 1):
    if i % 2 == 0:
        even += 1
    else:
        odd += 1

print("Even Numbers:", even)
print("Odd Numbers:", odd)

Sample Output

Enter the ending number: 20

Even Numbers: 10
Odd Numbers: 10

Explanation

The loop checks every number in the range and uses the modulus operator to determine whether it is even or odd.

Concepts Covered

  • for Loop
  • if Statement
  • Modulus Operator
  • Counter Variables

13. Python Program to Find the Sum of Digits of a Number

Problem Statement

Write a Python program to calculate the sum of all digits of a given number using a while loop.

Python Solution

number = int(input("Enter a number: "))

sum_digits = 0

while number > 0:
    digit = number % 10
    sum_digits += digit
    number //= 10

print("Sum of Digits:", sum_digits)

Sample Output

Enter a number: 5842

Sum of Digits: 19

Explanation

The program extracts each digit using the modulus operator (%) and removes the last digit using floor division (//) until the number becomes zero.

Concepts Covered

  • while Loop
  • Modulus Operator
  • Floor Division
  • Number Manipulation

14. Python Program to Check Whether a Number is an Armstrong Number

Problem Statement

Write a Python program to check whether a given number is an Armstrong number.

Python Solution

number = int(input("Enter a number: "))

original = number
digits = len(str(number))
result = 0

while number > 0:
    digit = number % 10
    result += digit ** digits
    number //= 10

if result == original:
    print("Armstrong Number")
else:
    print("Not an Armstrong Number")

Sample Output

Enter a number: 153

Armstrong Number

Explanation

An Armstrong number is equal to the sum of its digits raised to the power of the total number of digits.

For example:

153 = 1³ + 5³ + 3³ = 153

Concepts Covered

  • while Loop
  • Exponent Operator (**)
  • Number Manipulation
  • Conditional Statements

15. Python Program to Print All Prime Numbers Within a Given Range

Problem Statement

Write a Python program to print all prime numbers between two numbers entered by the user.

Python Solution

start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))

print("Prime Numbers:")

for num in range(start, end + 1):
    if num > 1:
        for i in range(2, int(num ** 0.5) + 1):
            if num % i == 0:
                break
        else:
            print(num)

Sample Output

Enter the starting number: 10
Enter the ending number: 30

Prime Numbers:
11
13
17
19
23
29

Explanation

The outer loop checks every number in the given range, while the inner loop determines whether the number has any divisors other than 1 and itself. If no divisors are found, the number is prime.

Concepts Covered

  • Nested Loops
  • for Loop
  • Prime Number Logic
  • break Statement
  • range()
  • Mathematical Optimization

16. Python Program to Print Floyd’s Triangle

Problem Statement

Write a Python program to print Floyd’s Triangle using nested loops.

Python Solution

rows = int(input("Enter the number of rows: "))

num = 1

for i in range(1, rows + 1):
    for j in range(i):
        print(num, end=" ")
        num += 1
    print()

Sample Output

Enter the number of rows: 5

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Explanation

The program uses nested for loops to print numbers sequentially in the shape of Floyd’s Triangle.

Concepts Covered

  • Nested Loops
  • Pattern Printing
  • Counter Variable

17. Python Program to Print Pascal’s Triangle

Problem Statement

Write a Python program to print Pascal’s Triangle for a given number of rows.

Python Solution

rows = int(input("Enter the number of rows: "))

for i in range(rows):
    number = 1

    print(" " * (rows - i), end="")

    for j in range(i + 1):
        print(number, end=" ")
        number = number * (i - j) // (j + 1)

    print()

Sample Output

Enter the number of rows: 5

     1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1

Explanation

Pascal’s Triangle is generated using combinations. Each number is calculated from the previous value instead of using factorials.

Concepts Covered

  • Nested Loops
  • Mathematical Formula
  • Pattern Printing

18. Python Program to Find All Perfect Numbers in a Given Range

Problem Statement

Write a Python program to print all perfect numbers within a given range.

Python Solution

start = int(input("Enter starting number: "))
end = int(input("Enter ending number: "))

print("Perfect Numbers:")

for num in range(start, end + 1):
    total = 0

    for i in range(1, num):
        if num % i == 0:
            total += i

    if total == num:
        print(num)

Sample Output

Enter starting number: 1
Enter ending number: 1000

Perfect Numbers:
6
28
496

Explanation

A perfect number is equal to the sum of its positive divisors excluding itself.

Example:

6 = 1 + 2 + 3

Concepts Covered

  • Nested Loops
  • Divisibility
  • Mathematical Logic

19. Python Program to Generate the Fibonacci Series Using Recursion

Problem Statement

Write a Python program to generate the Fibonacci series using recursion.

Python Solution

def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)

terms = int(input("Enter number of terms: "))

print("Fibonacci Series:")

for i in range(terms):
    print(fibonacci(i), end=" ")

Sample Output

Enter number of terms: 8

Fibonacci Series:
0 1 1 2 3 5 8 13

Explanation

The recursive function calculates each Fibonacci number by calling itself until the base condition is reached.

Concepts Covered

  • Recursion
  • Functions
  • for Loop
  • Base Condition

20. Python Program to Print a Diamond Star Pattern

Problem Statement

Write a Python program to print a diamond pattern using nested loops.

Python Solution

rows = int(input("Enter number of rows: "))

for i in range(rows):
    print(" " * (rows - i - 1) + "* " * (i + 1))

for i in range(rows - 2, -1, -1):
    print(" " * (rows - i - 1) + "* " * (i + 1))

Sample Output

Enter number of rows: 5

    *
   * *
  * * *
 * * * *
* * * * *
 * * * *
  * * *
   * *
    *

Explanation

The upper half of the diamond is printed using one loop, while the lower half is printed using another loop in reverse order.

Concepts Covered

  • Nested Loops
  • Pattern Printing
  • String Multiplication
  • Loop Control

Frequently Asked Questions (FAQs)

1. What are loops in Python?

Loops are programming constructs that allow you to execute a block of code repeatedly until a specific condition is met. They help automate repetitive tasks and make programs shorter and more efficient.


2. What are the different types of loops in Python?

Python provides two main types of loops:

  • for loop – Used when the number of iterations is known.
  • while loop – Used when the loop should continue until a condition becomes false.

Both loops are widely used in Python programming.


3. What is the difference between a for loop and a while loop?

A for loop iterates over a sequence such as a list, tuple, string, or range and is ideal when the number of iterations is fixed.

A while loop executes as long as a given condition is True and is useful when the number of iterations is unknown.

Example:

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

# while loop
count = 0
while count < 5:
    print(count)
    count += 1

4. What is the range() function in Python?

The range() function generates a sequence of numbers and is commonly used with for loops.

Example:

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

Output:

1
2
3
4
5

5. What are nested loops in Python?

A nested loop is a loop inside another loop. Nested loops are commonly used for pattern printing, matrix operations, and working with multidimensional data.

Example:

for i in range(3):
    for j in range(3):
        print("*", end=" ")
    print()

6. What are the break and continue statements?

  • break immediately terminates the loop.
  • continue skips the current iteration and moves to the next iteration.

These statements help control loop execution efficiently.


7. What is an infinite loop in Python?

An infinite loop occurs when the loop condition never becomes False, causing the loop to execute forever.

Example:

while True:
    print("This loop runs forever.")

Infinite loops should be used carefully and usually require a break statement to stop execution.


8. Where are loops used in real-world Python applications?

Loops are widely used in:

  • Data Analysis
  • Machine Learning
  • Web Scraping
  • Automation Scripts
  • File Handling
  • Game Development
  • Report Generation
  • Inventory Management
  • Banking Applications
  • Artificial Intelligence

9. How can I improve the performance of loops in Python?

You can improve loop performance by:

  • Avoiding unnecessary nested loops.
  • Using built-in functions like sum(), max(), and min() when possible.
  • Using list comprehensions for simple iterations.
  • Using efficient data structures.
  • Leveraging NumPy for large numerical computations.

10. Why are loops important in Python?

Loops are one of the most fundamental concepts in Python programming. They allow developers to process large datasets, automate repetitive tasks, perform calculations, generate reports, build algorithms, and solve complex real-world problems efficiently. Mastering loops is essential for fields such as data science, machine learning, web development, automation, and software engineering.

Chapter Summary

After completing this chapter, you have learned:

  • for loop
  • while loop
  • range() function
  • Nested loops
  • break statement
  • continue statement
  • Prime number logic
  • Pattern printing
  • Counting and reversing numbers
  • Real-world loop-based programs

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top