Python Programs
Python Programs
PROGRAMMING
PRACTICAL SESSION
2023 - 24
INDEX
SLNO DATE PROGRAM PAGE No. SIGNATURE
1 02 – 11 – 2022 Write a program to test if a given number is 1
Positive, Negative or Zero.
2 02 – 11 – 2022 Write a program to input a year and check if it 2
is a leap year or not.
3 02 – 11 – 2022 Write a program to input a number and print 3
the sum of digits & No of digits in the given
number
4 03 – 11 – 2022 Write a program to input a number and print 4
its reversed number
5 03 – 11 – 2022 Write a program to input a number and check 5
if it is Armstrong or Not
6 03 – 11 – 2022 Write a program to input a number and check 6
the number is Palindrome or Not
7 04 – 11 – 2022 Write a program to input a positive number 7
and print its factorial
8 04 – 11 – 2022 Write a program to obtain length and breadth of a 8
rectangle and calculate its area.
9 04 – 11 – 2022 Write a program to input a number and print its 9
cube.
10 05 – 11 – 2022 Write a program that takes a number and check 10
whether the given number is odd or even
11 05 – 11 – 2022 Write a program using NumPy package: Create an 11
array of 5 marks and display the average of all
marks.
12 05 – 11 – 2022 Write a Python program to upload an image of 12
your favourite flower on the screen and give
proper title to it.
13 06 – 11 – 2022 Program to create a LINE CHART 13
14 06 – 11 – 2022 Program to create a BAR CHART 14
15 06 – 11 – 2022 Program to create a PIE CHART 15
Program 1
Write a Python program to test if a given number is Positive, Negative or Zero
# program to test if a given number is Positive, Negative or Zero
num = float(input("Enter a number: "))
if num >0:
print("Input Number is Positive.")
elif num<0:
print("Input Number is Negative")
else:
print("Input Number is Zero")
Output 1
Enter a number: 5
Input Number is Positive.
Output 2
Enter a number: -1
Input Number is Negative
Output 3
Enter a number: 0
Input Number is Zero
Program 2
Write a Python program to input a year and check if it is a leap year or not
# program to input a year and check if it is a leap year or not
year = int(input(“Enter a four digit Year (yyyy):”))
if((year % 400 == 0) or (year % 100 !=0) and (year % 4 == 0)):
print(“Given Year is Leap Year”)
else:
print(“Given Year is Not a Leap Year”)
Output 1
Enter a four digit Year (yyyy): 2020
Given Year is Leap Year
Output 2
Enter a four digit Year (yyyy): 1900
Given Year is Not a Leap Year
Program 3
Write a program to input a number and print the sum of digits & No of digits in the given
number
# program to input a number and print the sum of digits & No of digits in the given number
num = int(input(“Enter a number Greater than 100:”))
count = 0
sum = digit = 0
while num != 0:
digit = num % 10
sum = sum + digit
num = num // 10
count = count +1
print(“Sum of Digits is:”, sum)
print(“Number of Digits in the given number is: “, count)
Output
Enter a number Greater than 100: 7802
Sum of Digit is: 17
Number of Digits in the given number is: 4
Program 4
Write a program to input a number and print its reversed number
# program to input a number and print its reversed number
orignumber = int(input(“Enter a number Greater than 100:”))
num = orignumber
rev = 0
while num != 0:
digit = num % 10
rev = (rev * 10) + digit
num = num // 10
print(“Given Number is:”, orignumber)
print(“Reverse of the given Number is:”, rev)
Output
Given Number is: 7802
Reverse of the given Number is:2087
Program 5
Write a program to input a number and check if it is Armstrong or Not
# program to input a number and check if it is Armstrong or Not
num = int(input(“Enter a number:”))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum = sum + (digit **3)
temp = temp // 10
if(num == sum):
print(num, “is an Armstrong Number”)
else:
print(num, “is Not an Armstrong Number“)
Output 1
Enter a number: 153
153 is an Armstrong
Output 2
Enter a number: 417
417 is Not an Armstrong
Program 6
Write a program to input a number and check the number is Palindrome or Not
# program to input a number and check the number is Palindrome or Not
orignumber = int(input(“Enter a number:”))
num = orignumber
rev = 0
while num != 0:
digit = num % 10
rev = (rev*10) + digit
num = num // 10
if orignumber == rev:
print(orignumber, “is a Palindrome Number.”)
else:
print(orignumber, “is Not a Palindrome Number.“)
Output 1
Enter a number: 789
789 is Not a Palindrome Number
Output 2
Enter a number: 12321
12321 is a Palindrome Number
Program 7
Write a program to input a positive number and print its factorial
# program to input a positive number and print its factorial
num = int(input(“Enter a Positive Number:”))
factorial = 1
for i in range(1, num+1):
factorial = factorial * i
print(“The Factorial of ”, num, “is:”, factorial)
Output
Enter a Positive Number: 6
The Factorial of 6 is: 120
Program 8
Write a program to obtain length and breadth of a rectangle and calculate its area.
# to input length and breadth of a rectangle and calculate its area
length = float(input(“Enter Length of the rectangle: ”))
breadth = float(input(“Enter breadth of the rectangle: ”))
area = length * breadth
print (“Rectangle specifications”)
print(“Length = ”, length, end =‘ ’)
print(“Breadth = ”, breadth)
print(“Area = ”, area)
Output
Enter Length of the rectangle: 8.75
Enter breadth of the rectangle: 35.0
Rectangle specifications
Length = 8.75 Breadth = 35.0
Area = 306.25
Program 9
Write a program to input a number and print its cube.
# to input a number and print its cube
num = int(input(“Enter a number : ”))
cube = num * num * num
print(“Number is : ”, num)
print(“Cube is : ”, cube)
OUTPUT
Enter a number : 7
Number is : 7
Cube is : 343
Program 10
Write a program that takes a number and check whether the given number is odd or even
# to check whether the given number is odd or even.
num = int(input(“Enter an integer : ”))
if num %2 == 0:
print(num, “is Even number”)
else:
print(num, “is Odd number”)
Output 1
Enter an integer : 8
8 is Even number
Output 2
Enter an integer : 5
5 is Odd number
Program 11
Write a program using NumPy package: Create an array of 5 marks and display the average of all
marks.
#Program to Create an array of 5 marks and display the average of all marks.
Import numpy as np
Import statistics
a = np.array({95, 90, 49, 71, 80})
m = statistcs.mean(a)
print(“The Average is : ”, m)
Output
The Average is : 77
Program 12
Write a Python program to upload an image of your favourite flower on the screen and
give proper title to it.
# program to upload an image of your favourite flower on the screen and give proper title to it.
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread(„D:\ Rose.jpg‟)
plt.imshow(img)
plt.title(„MY Favourite Flower‟)
plt.axis(„off‟)
plt.show
Output
My Favourite Flower
Program 13
Program to create a LINE CHART
# Program to create a LINE CHART
import matplotlib.pyplot as plt
week = [1, 2, 3, 4]
prices = [40, 80, 100, 50]
# Plotting Line Graph
plt.plot(week, prices)
# set the x axis and y axis labels.
plt.xlabel(‘Week’)
plt.ylabel(‘Onion Prices (Rs.)’)
plt.show()
Program 14
Program to create a BAR CHART
# Program to create a BAR CHART
import matplotlib.pyplot as plt
info = *‘Gold’, ‘Silver’, ‘Bronze’, ‘Total’+
india = [80, 59, 59, 198]
# Plotting Bar Chart
plt.bar(info, india)
# set the x axis and y axis labels.
plt.xlabel(‘Medal Type’)
plt.ylabel(‘India Medal Count’)
plt.show()
Program 15
Program to create a PIE CHART
#Program to create a PIE CHART
import matplotlib.pyplot as plt
col = [8000, 12000, 9800, 11200, 15500, 7300]
sections = *‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’+
# Plotting pie chart
plt.title(“Volunteering Week Collection”)
plt.pie(col, labels = sections)
plt.show()