···
35 Python Programming Exercises and Solutions
To understand a programming language deeply, you need to practice what you’ve learned. If
you’ve completed learning the syntax of Python programming language, it is the right time to do
some practice programs.
In this article, I’ll list down some problems that I’ve done and the answer code for each exercise.
Analyze each problem and try to solve it by yourself. If you have any doubts, you can check the
code that I’ve provided below. I’ve also attached the corresponding outputs.
1. Python program to check whether the given number is
even or not.
number = input("Enter a number: ")
x = int(number)%2
if x == 0:
print("The number is Even.")
else:
print("The number is Odd.")
···
Output:
Enter a number: 7
The number is Odd.
Enter a number: 6
The number is Even.
2. Python program to convert the temperature in degree
centigrade to Fahrenheit
c = input("Enter temperature in Centigrade: ")
f = (9*(int(c))/5)+32
print("Temperature in Fahrenheit is: ", f)
Output:
Enter temperature in Centigrade: 30
Temperature in Fahrenheit is: 86.0
3. Python program to find the area of a triangle whose
sides are given
import math
a = float(input("Enter the length of side a: "))
b = float(input("Enter the length of side b: "))
c = float(input("Enter the length of side c: "))
s = (a+b+c)/2
area = math.sqrt(s*(s-a)*(s-b)*(s-c))
print("Area of the triangle is: ", area)
Output:
Enter the length of side a: 4.0
Enter the length of side b: 3.0
Enter the length of side c: 6.0
Area of the triangle is: 5.332682251925386
4. Python program to find out the average of a set of
integers
count = int(input("Enter the count of numbers: "))
i = 0
sum = 0
for i in range(count):
x = int(input("Enter an integer: "))
sum = sum + x
avg = sum/count
print("The average is: ", avg)
Output:
Enter the count of numbers: 5
Enter an integer: 3
Enter an integer: 6
Enter an integer: 8
Enter an integer: 5
Enter an integer: 7
The average is: 5.8
5. Python program to find the product of a set of real
numbers
i = 0
product = 1
count = int(input("Enter the number of real numbers: "))
for i in range(count):
x = float(input("Enter a real number: "))
product = product * x
print("The product of the numbers is: ", product)
···
Output:
Enter the number of real numbers: 4
Enter a real number: 3.2
Enter a real number: 2.9
Enter a real number: 7.4
Enter a real number: 5.5
The product of the numbers is: 377.69599999999997
6. Python program to find the circumference and area of a
circle with a given radius
import math
r = float(input("Input the radius of the circle: "))
c = 2 * math.pi * r
area = math.pi * r * r
print("The circumference of the circle is: ", c)
print("The area of the circle is: ", area)
Output:
Input the radius of the circle: 4.3
The circumference of the circle is: 27.01769682087222
The area of the circle is: 58.088048164875275
7. Python program to check whether the given integer is a
multiple of 5
number = int(input("Enter an integer: "))
if(number%5==0):
print(number, "is a multile of 5")
else:
print(number, "is not a multiple of 5")
Output:
Enter an integer: 23
23 is not a multiple of 5
Enter an integer: 55
55 is a multile of 5
8. Python program to check whether the given integer is a
multiple of both 5 and 7
number = int(input("Enter an integer: "))
if((number%5==0)and(number%7==0)):
print(number, "is a multiple of both 5 and 7")
else:
print(number, "is not a multiple of both 5 and 7")
···
Output:
Enter an integer: 33
33 is not a multiple of both 5 and 7
Enter an integer: 35
35 is a multiple of both 5 and 7
9. Python program to find the average of 10 numbers using
while loop
count = 0
sum = 0.0
while(count<10):
number = float(input("Enter a real number: "))
count=count+1
sum = sum+number
avg = sum/10
print("Average is :",avg)
Output:
Enter a real number: 2.3
Enter a real number: 3.3
Enter a real number: 1.7
Enter a real number: 4.1
Enter a real number: 1.65
Enter a real number: 7.32
Enter a real number: 6.66
Enter a real number: 4.53
Enter a real number: 9.01
Enter a real number: 2.15
Average is : 4.272
10. Python program to display the given integer in a
reverse manner
number = int(input("Enter a positive integer: "))
rev = 0
while(number!=0):
digit = number%10
rev = (rev*10)+digit
number = number//10
print(rev)
Output:
···
···
Enter a positive integer: 739
937
11. Python program to find the geometric mean of n
numbers
c = 0
p = 1.0
count = int(input("Enter the number of values: "))
while(c<count):
x = float(input("Enter a real number: "))
c = c+1
p = p * x
gm = pow(p,1.0/count)
print("The geometric mean is: ",gm)
Output:
Enter the number of values: 6
Enter a real number: 3.2
Enter a real number: 1.54
Enter a real number: 4.11
Enter a real number: 9.12
Enter a real number: 7.56
Enter a real number: 3.14
The geometric mean is: 4.045694954202098
12. Python program to find the sum of the digits of an
integer using a while loop
sum = 0
number = int(input("Enter an integer: "))
while(number!=0):
digit = number%10
sum = sum+digit
number = number//10
print("Sum of digits is: ", sum)
Output:
···
Enter an integer: 395
Sum of digits is: 17
13. Python program to display all the multiples of 3 within
the range 10 to 50
for i in range(10,50):
if (i%3==0):
print(i)
Output:
···
12
15
18
21
24
27
30
33
36
39
42
45
48
14. Python program to display all integers within the range
100-200 whose sum of digits is an even number
for i in range(100,200):
num = i
sum = 0
while(num!=0):
digit = num%10
sum = sum + digit
num = num//10
if(sum%2==0):
print(i)
Output:
···
101
103
105
107
109
110
112
114
116
118
121
123
125
127
129
130
132
134
136
138
141
143
145
147
149
150
152
154
156
158
161
163
165
167
169
170
172
174
176
178
181
183
185
187
189
190
192
194
196
198
15. Python program to check whether the given integer is a
prime number or not
num = int(input("Enter an integer greater than 1: "))
isprime = 1 #assuming that num is prime
for i in range(2,num//2):
if (num%i==0):
isprime = 0
break
if(isprime==1):
print(num, "is a prime number")
else:
print(num, "is not a prime number")
Output:
···
Enter an integer greater than 1: 7
7 is a prime number
Enter an integer greater than 1: 24
24 is not a prime number
16. Python program to generate the prime numbers from 1
to N
num =int(input("Enter the range: "))
for n in range(2,num):
for i in range(2,n):
if(n%i==0):
break
else:
print(n)
···
Output:
···
Enter the range: 6
2
3
5
17. Python program to find the roots of a quadratic
equation
import math
a = float(input("Enter the first coefficient: "))
b = float(input("Enter the second coefficient: "))
c = float(input("Enter the third coefficient: "))
if (a!=0.0):
d = (b*b)-(4*a*c)
if (d==0.0):
print("The roots are real and equal.")
r = -b/(2*a)
print("The roots are ", r,"and", r)
elif(d>0.0):
print("The roots are real and distinct.")
r1 = (-b+(math.sqrt(d)))/(2*a)
r2 = (-b-(math.sqrt(d)))/(2*a)
print("The root1 is: ", r1)
print("The root2 is: ", r2)
else:
print("The roots are imaginary.")
rp = -b/(2*a)
ip = math.sqrt(-d)/(2*a)
print("The root1 is: ", rp, "+ i",ip)
print("The root2 is: ", rp, "- i",ip)
else:
print("Not a quadratic equation.")
Output:
···
Enter the first coefficient: 4
Enter the second coefficient: 7
Enter the third coefficient: 2
The roots are real and distinct.
The root1 is: -0.3596117967977924
The root2 is: -1.3903882032022077
18. Python program to print the numbers from a given
number n till 0 using recursion
def print_till_zero(n):
if (n==0):
return
print(n)
n=n-1
print_till_zero(n)
print_till_zero(8)
Output:
···
8
7
6
5
4
3
2
1
19. Python program to find the factorial of a number using
recursion
def fact(n):
if n==1:
f=1
else:
f = n * fact(n-1)
return f
num = int(input("Enter an integer: "))
result = fact(num)
print("The factorial of", num, " is: ", result)
Output:
···
Enter an integer: 5
The factorial of 5 is: 120
20. Python program to display the sum of n numbers using
a list
numbers = []
num = int(input('How many numbers: '))
for n in range(num):
x = int(input('Enter number '))
numbers.append(x)
print("Sum of numbers in the given list is :", sum(numbers))
Output:
How many numbers: 3
Enter number 2
Enter number 3
Enter number 4
Sum of numbers in the given list is : 9
21. Python program to implement linear search
numbers = [4,2,7,1,8,3,6]
f = 0 #flag
x = int(input("Enter the number to be found out: "))
for i in range(len(numbers)):
if (x==numbers[i]):
print("Successful search, the element is found at position", i)
f = 1
break
if(f==0):
print("Oops! Search unsuccessful")
Output:
···
Enter the number to be found out: 7
Successful search, the element is found at position 2
22. Python program to implement binary search
def binarySearch(numbers, low, high, x):
if (high >= low):
mid = low + (high - low)//2
if (numbers[mid] == x):
return mid
elif (numbers[mid] > x):
return binarySearch(numbers, low, mid-1, x)
else:
return binarySearch(numbers, mid+1, high, x)
else:
return -1
numbers = [ 1,4,6,7,12,17,25 ] #binary search requires sorted numbers
x = 7
result = binarySearch(numbers, 0, len(numbers)-1, x)
if (result != -1):
print("Search successful, element found at position ", result)
else:
print("The given element is not present in the array")
Output:
···
Search successful, element found at position 3
23. Python program to find the odd numbers in an array
numbers = [8,3,1,6,2,4,5,9]
count = 0
for i in range(len(numbers)):
if(numbers[i]%2!=0):
count = count+1
print("The number of odd numbers in the list are: ", count)
Output:
The number of odd numbers in the list are: 4
24. Python program to find the largest number in a list
without using built-in functions
numbers = [3,8,1,7,2,9,5,4]
big = numbers[0]
position = 0
for i in range(len(numbers)):
if (numbers[i]>big):
big = numbers[i]
position = i
print("The largest element is ",big," which is found at position
",position)
Output:
···
The largest element is 9 which is found at position 5
25. Python program to insert a number to any position in a
list
numbers = [3,4,1,9,6,2,8]
print(numbers)
x = int(input("Enter the number to be inserted: "))
y = int(input("Enter the position: "))
numbers.insert(y,x)
print(numbers)
Output:
[3, 4, 1, 9, 6, 2, 8]
Enter the number to be inserted: 11
Enter the position: 2
[3, 4, 11, 1, 9, 6, 2, 8]
26. Python program to delete an element from a list by
index
numbers = [3,4,1,9,6,2,8]
print(numbers)
x = int(input("Enter the position of the element to be deleted: "))
numbers.pop(x)
print(numbers)
···
Output:
[3, 4, 1, 9, 6, 2, 8]
Enter the position of the element to be deleted: 4
[3, 4, 1, 9, 2, 8]
27. Python program to check whether a string is
palindrome or not
def rev(inputString):
return inputString[::-1]
def isPalindrome(inputString):
reverseString = rev(inputString)
if (inputString == reverseString):
return True
return False
s = input("Enter a string: ")
result = isPalindrome(s)
if result == 1:
print("The string is palindrome")
else:
print("The string is not palindrome")
Output:
···
Enter a string: malayalam
The string is palindrome
Enter a string: car
The string is not palindrome
28. Python program to implement matrix addition
X = [[8,5,1],
[9 ,3,2],
[4 ,6,3]]
Y = [[8,5,3],
[9,5,7],
[9,4,1]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for k in result:
print(k)
Output:
···
[16, 10, 4]
[18, 8, 9]
[13, 10, 4]
29. Python program to implement matrix multiplication
X = [[8,5,1],
[9 ,3,2],
[4 ,6,3]]
Y = [[8,5,3],
[9,5,7],
[9,4,1]]
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for x in result:
print(x)
Output:
···
···
[118, 69, 60, 0]
[117, 68, 50, 0]
[113, 62, 57, 0]
30. Python program to check leap year
year = int(input("Enter a year: "))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
else:
print(year, "is a leap year")
else:
print(year, " is not a leap year")
Output:
Enter a year: 2023
2023 is not a leap year
Enter a year: 2024
2024 is a leap year
31. Python program to find the Nth term in a Fibonacci
series using recursion
def Fib(n):
if n<0:
print("The input is incorrect.")
elif n==1:
return 0
elif n==2:
return 1
else:
return Fib(n-1)+Fib(n-2)
print(Fib(7))
Output:
32. Python program to print Fibonacci series using
iteration
a = 0
b = 1
n=int(input("Enter the number of terms in the sequence: "))
print(a,b,end=" ")
while(n-2):
c=a+b
a,b = b,c
print(c,end=" ")
n=n-1
Output:
Enter the number of terms in the sequence: 8
0 1 1 2 3 5 8 13
33. Python program to print all the items in a dictionary
phone_book = {
'John' : [ '8592970000', '[email protected]' ],
'Bob': [ '7994880000', '[email protected]' ],
'Tom' : [ '9749552647' , '[email protected]' ]
}
for k,v in phone_book.items():
print(k, ":", v)
Output:
Bob : ['7994880000', '
[email protected]']
Tom : ['9749552647', '
[email protected]']
34. Python program to implement a calculator to do basic
operations
def add(x,y):
print(x+y)
def subtract(x,y):
print(x-y)
def multiply(x,y):
print(x*y)
def divide(x,y):
print(x/y)
print("Enter two numbers")
n1=input()
n2=input()
print("Enter the operation +,-,*,/ ")
op=input()
if op=='+':
add(int(n1),int(n2))
elif op=='-':
subtract(int(n1),int(n2))
elif op=='*':
multiply(int(n1),int(n2))
elif op=='/':
divide(int(n1),int(n2))
else:
print(" Invalid entry ")
Output:
5
6
Enter the operation +,-,*,/
*
30
35. Python program to draw a circle of squares using
Turtle
import turtle
x=turtle.Turtle()
def square(angle):
x.forward(100)
x.right(angle)
x.forward(100)
x.right(angle)
x.forward(100)
x.right(angle)
x.forward(100)
x.right(angle+10)
for i in range(36):
square(90)
Output:
Conclusion
For practicing more such exercises, I suggest you go to hackerrank.com and sign up. You’ll be
able to practice Python there very effectively.
Once you become comfortable solving coding challenges, it’s time to move on and build
something cool with your skills. If you know Python but haven’t built an app before, I suggest
you check out my Create Desktop Apps Using Python & Tkinter course. This interactive course
will walk you through from scratch to building clickable apps and games using Python.
···
I hope these exercises were helpful to you. If you have any doubts, feel free to let me know in the
comments.
Happy coding.
Share
Ashwin Joy
I'm the face behind Pythonista Planet. I learned my first programming language back in 2015.
Ever since then, I've been learning programming and immersing myself in technology. On this
site, I share everything that I've learned about computer programming.
11 thoughts on “35 Python Programming Exercises and
Solutions”
Peter M says:
February 12, 2021 at 3:23 PM
I don’t mean to nitpick and I don’t want this published but you might want to check code for
#16.
4 is not a prime number.
Reply
Ashwin Joy says:
February 13, 2021 at 3:56 PM
Thanks man for pointing out the mistake. I’ve updated the code.
Reply
Adam says:
August 15, 2021 at 8:44 PM
# 8. Python program to check whether the given integer is a multiple of both 5 and 7:
You can only check if integer is a multiple of 35.
It always works the same – just multiply all the numbers you need to check for multiplicity.
Reply
PRAVEEN KC says:
January 6, 2022 at 1:58 PM
For reverse the given integer
n=int(input(“enter the no:”))
n=str(n)
n=int(n[::-1])
print(n)
Reply
Poo says:
March 29, 2022 at 5:21 PM
Very nice
Reply
Daniel says:
November 9, 2022 at 10:03 PM
very good, tnks
Reply
John.A says:
March 27, 2023 at 12:03 PM
Please who can help me with this question asap
A particular cell phone plan includes 50 minutes of air time and 50 text messages for $15.00
a month. Each additional minute of air time costs $0.25, while additional text messages cost
$0.15 each. All cell phone bills include an additional charge of $0.44 to support 911 call
centers, and the entire bill (including the 911 charge) is subject to 5 percent sales tax.
We are so to run the code in phyton
Reply
Naveen kumar says:
April 25, 2023 at 11:37 PM
this is best app
Reply
Manoj K says:
May 15, 2023 at 2:41 PM
Hello Ashwin, Thanks for sharing a Python practice
Reply
ASISH PATRO says:
August 10, 2023 at 4:15 PM
May be in a better way for reverse.
#”’ Reverse of a string
v_str = str ( input(‘ Enter a valid string or number :- ‘) )
v_rev_str=”
for v_d in v_str:
v_rev_str = v_d + v_rev_str
print( ‘reverse of th input string / number :- ‘, v_str ,’is :- ‘, v_rev_str.capitalize() )
#Reverse of a string ”’
Reply
Dave says:
October 27, 2023 at 12:26 PM
Problem 15. When searching for prime numbers, the maximum search range only needs to be
sqrt(n). You needlessly continue the search up to //n. Additionally, you check all even
numbers. As long as you declare 2 to be prime, the rest of the search can start at 3 and check
every other number. Another big efficiency improvement.
Reply
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Save my name and email in this browser for the next time I comment.
POST COMMENT
Recent Posts
Introduction to Modular Programming with Flask
Modular programming is a software design technique that emphasizes separating the
functionality of a program into independent, interchangeable modules. In this tutorial, let's
understand what modular...
CONTINUE READING
Introduction to ORM with Flask-SQLAlchemy
While Flask provides the essentials to get a web application up and running, it doesn't force
anything upon the developer. This means that many features aren't included in the core
framework....
CONTINUE READING
ABOUT ME
Hi, I’m Ashwin Joy. I’m a Computer Science and Engineering graduate who is passionate about
programming and technology. Pythonista Planet is the place where I nerd out about computer
programming. On this blog, I share all the things I learn about programming as I go.
READ MORE
···
LEGAL INFORMATION
Pythonista Planet is the place where you learn technical skills and soft skills to become a better
programmer. This site is owned and operated by Ashwin Joy. Pythonista Planet is a participant in
the Amazon Services LLC Associates Program, an affiliate advertising program designed to
provide a means for sites to earn advertising fees by advertising and linking to Amazon.com.
This site also participates in affiliate programs of Udemy, Treehouse, Coursera, and Udacity, and
is compensated for referring traffic and business to these companies.
···
LinkedIn TeacherOn Pinterest Instagram Twitter Podcast Privacy Policy
Terms and Conditions Sitemap
© 2024 Copyright Pythonista Planet