Looping Statement Practice Questions
M3-R5.1 · Chapter 4: Operators, Expressions and Python Statements · 12 min read
Basic Looping Questions
Note : - Practice All these Questions and Also do the same questions with the help of While Loop in you notebook
Q1. Write a program to print the first 10 natural numbers using for loop.
Code:
for i in range(1, 11):
print(i)
Output:
1 2 3 4 5 6 7 8 9 10
Explanation:
1. range(1, 11) generates numbers from 1 up to 10 (11 is excluded).
2. The loop variable i takes each value: 1, 2, 3, ... 10 one by one.
3. print(i) displays each number. The loop runs exactly 10 times.
________________________________________
Q2. Write a Python program to print all the even numbers up to 20.
Code:
for i in range(21):
if i % 2 == 0:
print(i)
Output:
0 2 4 6 8 10 12 14 16 18 20
Explanation:
1. range(21) generates numbers 0 to 20.
2. The condition i % 2 == 0 checks if the number is divisible by 2 (even).
3. Only even numbers satisfy the condition and get printed.
4. Note: 0 is also even, so it appears in the output.
________________________________________
Q3. Write a Python program to calculate the sum of all numbers from 1 to a given number n.
Code:
n = int(input('Plz enter the number: '))
sum = 0
for i in range(1, n+1):
sum += i
print(sum)
Output:
Plz enter the number: 5
15
Explanation:
1. The user enters n. We use int() to convert the string input to an integer.
2. sum is initialized to 0 before the loop.
3. range(1, n+1) generates 1, 2, 3, ... n (n+1 ensures n is included).
4. sum += i adds each value of i to the running total.
5. After the loop, print(sum) displays the final answer. For n=5: 1+2+3+4+5 = 15.
________________________________________
Q4. Write a Python program to calculate the sum of all the odd numbers up to 10.
Code:
num = 10
sum = 0
for i in range(num):
if i % 2 != 0:
sum += i
print(sum)
Output:
25
Explanation:
1. range(10) gives 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
2. i % 2 != 0 is True for odd numbers only: 1, 3, 5, 7, 9.
3. These are added to sum: 1 + 3 + 5 + 7 + 9 = 25.
4. Note: range(num) starts from 0, so 0 is also checked but 0 is even — it is skipped.
________________________________________
Q5. Write a Python program to print the multiplication table of a given number.
Code:
number = 5
for i in range(1, 11):
print(number, 'x', i, '=', number * i)
Output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50
Explanation:
1. number = 5 is the table we want to print.
2. range(1, 11) gives multipliers 1 through 10.
3. Each iteration prints: number x i = result.
4. number * i computes the product correctly each time.
________________________________________
Q6. Write a Python program to count the total number of digits in a number.
Code:
num = input('Plz Enter the Number: ')
count = 0
for i in num:
count += 1
print('Total digits in number =', count)
Output:
Plz Enter the Number: 12345
Total digits in number = 5
Explanation:
1. input() returns a string — no need for int() here because we want individual characters.
2. A string is iterable — the for loop visits each character (digit) one by one.
3. count += 1 increments the counter for every character found.
4. After the loop, count holds the total number of digits.
________________________________________
Q7. Write a Python program that accepts a word from the user and reverses it.
Code:
word = input('Please enter the word: ')
reversed_word = ''
for i in word:
reversed_word = i + reversed_word
print(reversed_word)
Output:
Please enter the word: Python
nohtyP
Explanation:
1. reversed_word starts as an empty string ''.
2. For each character i in word, we prepend it: i + reversed_word puts the new character at the front.
3. For 'Python': P→'P', y→'yP', t→'tyP', h→'htyP', o→'ohtyP', n→'nohtyP'.
4. After the loop, reversed_word contains the reversed string.
________________________________________
Q8. Write a Python program to check if the given string is a palindrome.
Code:
word = input('Please enter the word: ')
reversed_word = ''
for i in word:
reversed_word = i + reversed_word
if word == reversed_word:
print('The string', word, 'is a Palindrome.')
else:
print('The string', word, 'is NOT a Palindrome.')
Output:
Please enter the word: madam
The string madam is a Palindrome.
Please enter the word: hello
The string hello is NOT a Palindrome.
Explanation:
1. This uses the same reversal logic from Example 7.
2. After reversing, we compare word == reversed_word.
3. A palindrome reads the same forwards and backwards (e.g., madam, racecar, level).
4. If they match → Palindrome. If they don't → Not a Palindrome.
________________________________________
Q9. Write a Python program to print all even numbers between 50 and 100, inclusive of both.
Code:
for i in range(50, 101):
if i % 2 == 0:
print(i, end=' ')
Output:
50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
Explanation:
1. range(50, 101) includes 50 and 100 (101 is excluded, so 100 is the last value).
2. The if condition filters only even numbers using the modulo operator.
3. end=' ' in print() keeps all numbers on the same line separated by spaces.
4. Without end=' ', each number would print on a new line.
________________________________________
Q10. Write a Python program to print the series: 1, 4, 9, 16, 25, ... up to 15 terms.
Code:
for i in range(1, 16):
j = i * i
print(j, end=' ')
Output:
1 4 9 16 25 36 49 64 81 100 121 144 169 196 225
Explanation:
1. This series is the squares of natural numbers: 1², 2², 3², ... 15².
2. range(1, 16) gives values 1 through 15.
3. j = i * i computes the square of each number i.
4. end=' ' prints all values on one line with spaces.
________________________________________
Q11. Write a Python program to print numbers divisible by 3 between 30 and 70.
Code:
# Method 1 — using step in range()
for i in range(30, 70, 3):
print(i, end=' ')
print()
# Method 2 — using if condition
for i in range(30, 71):
if i % 3 == 0:
print(i, end=' ')
Output:
30 33 36 39 42 45 48 51 54 57 60 63 66 69
30 33 36 39 42 45 48 51 54 57 60 63 66 69
Explanation:
1. Method 1: range(30, 70, 3) uses a step of 3 — starts at 30 and jumps by 3 each time. Stops before 70.
2. Method 2: range(30, 71) checks every number from 30 to 70 and uses i % 3 == 0 to filter multiples of 3.
3. Both methods produce the same output. Method 1 is more efficient (fewer iterations).
4. Note: range(30, 70, 3) stops before 70, so 69 is the last value.
________________________________________
Q12. Write a Python program to count the number of times a given character appears in a string.
Code:
c = 0
string = input('Enter the string: ')
ch = input('Enter the character to search: ')
for i in string:
if i == ch:
c += 1
print('Entered string:', string)
print('Count of', ch, 'in string =', c)
Output:
Enter the string: hello world
Enter the character to search: l
Entered string: hello world
Count of l in string = 3
Explanation:
1. c is initialized to 0 (the counter).
2. The for loop iterates through every character i in the string.
3. The if condition checks if the current character i matches the target character ch.
4. Each time they match, c is incremented by 1.
5. After the loop, c holds the total count of occurrences.
________________________________________
Q13. Write a Python program to print the position of each occurrence of a character in a string.
Code:
string = input('Enter the string: ')
ch = input('Enter the character to search: ')
pos = 0
for i in string:
pos += 1
if i == ch:
print(ch, 'found at position:', pos)
Output:
Enter the string: hello
Enter the character to search: l
l found at position: 3
l found at position: 4
Explanation:
1. pos starts at 0 and is incremented at the start of every iteration — before the check.
2. This means pos tracks the current position (1-based index) in the string.
3. When i == ch is True, we print the position where the character was found.
4. The loop continues even after finding a match — so all positions are printed.
________________________________________
Q14. Write a Python program to print the factorial of a number.
Code:
num = int(input('Enter a number: '))
fact = 1
for i in range(1, num+1):
fact = fact * i
print('Factorial =', fact)
Output:
Enter a number: 5
Factorial = 120
Explanation:
1. fact is initialized to 1 (not 0, because multiplying by 0 gives 0).
2. range(1, num+1) generates 1, 2, 3, ... num.
3. fact = fact * i multiplies the running product by each number.
4. For num=5: fact = 1×1=1, then 1×2=2, then 2×3=6, then 6×4=24, then 24×5=120.
5. Factorial of 5 (written as 5!) = 1 × 2 × 3 × 4 × 5 = 120.
________________________________________