4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
35 Python Programming Exercises and Solutions
Written by Ashwin Joy ● in Python
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.
Table of Contents
1. Python program to check whether the given number is even or not.
2. Python program to convert the temperature in degree centigrade to Fahrenheit
3. Python program to find the area of a triangle whose sides are given
4. Python program to find out the average of a set of integers
5. Python program to find the product of a set of real numbers
6. Python program to find the circumference and area of a circle with a given radius
7. Python program to check whether the given integer is a multiple of 5
8. Python program to check whether the given integer is a multiple of both 5 and 7
9. Python program to find the average of 10 numbers using while loop
10. Python program to display the given integer in reverse manner
11. Python program to find the geometric mean of n numbers
12. Python program to find the sum of the digits of an integer using while loop
13. Python program to display all the multiples of 3 within the range 10 to 50
14. Python program to display all integers within the range 100-200 whose sum of digits is an
even number
15. Python program to check whether the given integer is a prime number or not
16. Python program to generate the first N prime numbers
17. Python program to find the roots of a quadratic equation
18. Python program to print the numbers from a given number n till 0 using recursion
19. Python program to find the factorial of a number using recursion
[Link] 1/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
20. Python program to display the sum of n numbers using a list
21. Python program to implement linear search
22. Python program to implement binary search
23. Python program to find the odd numbers in an array
24. Python program to find the largest number in a list without using built-in functions
25. Python program to insert a number to any position in a list
26. Python program to delete an element from a list by index
27. Python program to check whether a string is palindrome or not
28. Python program to implement matrix addition
29. Python program to implement matrix multiplication
30. Python program to check leap year
31. Python program to find the Nth term in a Fibonacci series using recursion
32. Python program to print Fibonacci series using iteration
33. Python program to print all the items in a dictionary
34. Python program to implement a calculator to do basic operations
35. Python program to draw a circle of squares using Turtle
Conclusion
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:
[Link] 2/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
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:
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 = [Link](s*(s-a)*(s-b)*(s-c))
print(" Area of the triangle is: ", area)
Output:
[Link] 3/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
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:
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:
[Link] 4/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
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 * [Link] * r
area = [Link] * r * r
print("The circumference of the circle is: ", c)
print("The area of the circle is: ", area)
Output:
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:
[Link] 5/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
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:
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:
[Link] 6/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
10. Python program to display the given integer in 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:
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:
[Link] 7/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
12. Python program to find the sum of the digits of an
integer using 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:
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:
[Link] 8/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
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:
[Link] 9/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
15. Python program to check whether the given integer is a
prime number or not
num = int(input("Enter an integer: "))
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:
16. Python program to generate the first N prime numbers
[Link] 10/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
num = int((input("Enter the number of prime numbers required: ")))
count = 0
i = 2
while(count<num):
isprime=1
for j in range(2,i//2):
if(i%j==0):
isprime = 0
break
if(isprime==1):
print(i)
count= count + 1
i = i+1
Output:
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 = (bb)-(4ac)
if (d==0.0):
print("The roots are real and equal.")
r = -b/(2a)
print("The roots are ", r,"and", r)
elif(d>0.0):
[Link] 11/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
print("The roots are real and distinct.")
r1 = (-b+([Link](d)))/(2a)
r2 = (-b-([Link](d)))/(2a)
print("The root1 is: ", r1)
print("The root2 is: ", r2)
else:
print("The roots are imaginary.")
rp = -b/(2a) ip = [Link](-d)/(2a)
print("The root1 is: ", rp, "+ i",ip)
print("The root2 is: ", rp, "- i",ip)
else:
print("Not a quadratic equation.")
Output:
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:
[Link] 12/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
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:
[Link] 13/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
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 '))
[Link](x)
print("Sum of numbers in the given list is :", sum(numbers))
Output:
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:
[Link] 14/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
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 = [ 9,4,6,7,2,1,5 ]
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:
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:
[Link] 15/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
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:
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: "))
[Link](y,x)
print(numbers)
Output:
[Link] 16/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
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: "))
[Link](x)
print(numbers)
Output:
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:
[Link] 17/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
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:
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],
[Link] 18/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
[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:
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")
[Link] 19/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
Output:
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
[Link] 20/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
print(c,end=" ")
n=n-1
Output:
33. Python program to print all the items in a dictionary
phone_book = {
'John' : [ '8592970000', 'john@[Link]' ],
'Bob': [ '7994880000', 'bob@[Link]' ],
'Tom' : [ '9749552647' , 'tom@[Link]' ]
}
for k,v in phone_book.items():
print(k, ":", v)
Output:
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()
[Link] 21/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
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:
35. Python program to draw a circle of squares using
Turtle
import turtle
x=[Link]()
def square(angle):
[Link](100)
[Link](angle)
[Link](100)
[Link](angle)
[Link](100)
[Link](angle)
[Link](100)
[Link](angle+10)
[Link] 22/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
for i in range(36):
square(90)
Output:
Conclusion
For practicing more such exercises, I suggest you go to [Link] and sign up. You’ll be
able to practice Python there very effectively.
I hope these exercises were helpful to you. If you have any doubts, feel free to let me know in
the comments. I would appreciate it if you would be willing to share this article. It will
encourage me to create more useful tutorials like this.
[Link] 23/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
Happy Coding 🙂
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.
Leave a Reply
Your email address will not be published. Required fields are marked *
[Link] 24/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
Comment
Name *
Email *
Save my name and email in this browser for the next time I comment.
POST COMMENT
Recent Content
The Ultimate Guide to LinkedIn for Programmers
LinkedIn is an incredible platform for all programmers. You should take the maximum
advantage of it. But how do you create an attractive LinkedIn profile? In this article, I'll share a
few helpful...
CONTINUE READING
[Link] 25/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
How To Learn Data Science - A Roadmap for Beginners
Data science is a pretty trending field nowadays, and the data scientist profession is the
sexiest job of the modern era. But how do you learn data science? In this article, I'll share a
roadmap for...
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.
[Link] 26/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
LEGAL INFORMATION
This site is owned and operated by Ashwin Joy. [Link] 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 [Link].
This site also participates in other affiliate programs and is compensated for referring traffic
and business to these companies.
Privacy Policy
© 2020 Copyright Pythonista Planet
[Link] 27/28
4/27/2020 35 Python Programming Exercises and Solutions – Pythonista Planet
[Link] 28/28