Python Loops Exercise and Solutions


Question 1 : Calculate the sum of numbers from 1 to n.

Expected Output:

Enter the number : 10
Sum : 55

Using the for loop and the range function.

num = int(input("Enter the number : "))

sum = 0

for i in range(1, num+1):
    sum = sum + i

print("Sum :", sum)

Output:

Enter the number : 10
Sum : 55

Question 2 : Print a multiplication table of 5.

Expected Output:

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Using the for loop and the range function.

for i in range(1, 11):
    print("5 x", i, "=",5*i)

Output:

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Question 3 : How can we find the total number of digits in a number?

Given Number:

654321

Expected Output:

6

Way 1 : Using the while loop and the Counter Variable.

number = 654321
counter = 0

while number != 0:
    number = number//10
    counter = counter + 1
    
print(counter)

Way 2 : Converting number to string ad using the len() function.

number = 654321
my_str = str(number)
  
print(len(my_str))

Output:

6

Question 4 : Reverse the elements of the list using the for loop.

Given Number:

[10, 20, 30, 40, 50, 60]

Expected Output:

[60, 50, 40, 30, 20, 10]
lst = [10, 20, 30, 40, 50, 60]
lst_length = len(lst)

new_lst = []

for i in range(lst_length-1, -1, -1):
    new_lst.append(lst[i])
    
print(new_lst)

Output:

[60, 50, 40, 30, 20, 10]

Question 5 : Reverse the number using the for loop.

Given Number:

23456

Expected Output:

65432
number = 23456
new_number = 0

while number != 0:
    last_digit = number % 10
    new_number = (new_number * 10) + last_digit
    number = number // 10
    
print(new_number)

Output:

65432

Question 6 : Find the sum of a series of numbers up to n.

Given Number:

Enter the number = 3
Enter the range = 5

Expected Output:

37035
number = int(input("Enter the number = "))
n = int(input("Enter the range = "))
sum = 0

for i in range(1, n+1):
    final_number = i * str(number)
    sum = sum + int(final_number)

print(sum)

Output:

37035

Question 7 : Number guessing game with hints.

import random

number = random.randint(1,10)
guess = int(input("Enter the number between 1 and 10 : "))

while guess != number:
    if guess < number:
        print("Grow your guess.")
    else:
        print("Bring down your guess")
    guess = int(input("Try again! Enter the number between 1 and 10 : "))
print("Congrats! You are right." )

Output:

Enter the number between 1 and 10 : 6
Grow your guess.
Try again! Enter the number between 1 and 10 : 7
Grow your guess.
Try again! Enter the number between 1 and 10 : 8
Grow your guess.
Try again! Enter the number between 1 and 10 : 9
Congrats! You are right.

Question 8 : Check if the year is the leap year or not.

year = int(input("Please enter a year : "))
leap = 0

if year % 100 == 0:
    if year % 400 == 0:
        leap = 1
elif year % 4 == 0:
    leap = 1
        
if leap == True:
    print(year, "is a leap year.")
else:
    print(year, "is not a leap year.")

Output:

Please enter a year : 2004
2004 is a leap year.

Question 9 : Print pattern like -6 12 -18 24 -30

limit = int(input("Enter the last number :"))
sign = -1

for i in range(6, limit, 6):
    value = i * sign
    sign = sign * -1
    print(value, end = ' ')

Output:

Enter the last number :40
-6 12 -18 24 -30 36 

Question 10 : Print a pattern like 1 + 1/8 + 1/27… and the sum.

num = int(input('Enter the range : '))

sum = 0
print("1", end = ' ')
sum = sum + 1
                
for i in range(2, num+1):
    cube = i ** 3
    value = 1/cube
    sum = sum + value
    print('+ 1 /', cube, end = ' ')
print('=', sum)

Output:

Enter the range : 6
1 + 1 / 8 + 1 / 27 + 1 / 64 + 1 / 125 + 1 / 216 = 1.1902916666666665