35 Python Programming Exercises and Solutions - Pythonista Planet
35 Python Programming Exercises and Solutions - Pythonista Planet
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.
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
Output:
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:
Output:
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:
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:
Output:
Enter an integer: 23
23 is not a multiple of 5
Enter an integer: 55
55 is a multile of 5
···
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
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:
Output:
···
···
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:
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:
···
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
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
Output:
···
Enter an integer greater than 1: 7
7 is a prime number
···
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:
···
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
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:
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:
···
Output:
···
Search successful, element found at position 3
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:
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:
···
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]
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]
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:
···
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]
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:
···
···
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:
Output:
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:
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.
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
Manoj K says:
May 15, 2023 at 2:41 PM
Hello Ashwin, Thanks for sharing a Python practice
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
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.
···