Python Final Practicals
Python Final Practicals
pi=22/7
height = float(input('Height of cylinder: '))
radian = float(input('Radius of cylinder: '))
volume = pi * radian * radian * height
sur_area = ((2*pi*radian) * height) + ((pi*radian**2)*2)
print("Volume is: ", volume)
print("Surface Area is: ", sur_area)
3. Write a program that takes the marks of 5 subject and display the grade.
if num > 0:
print("It is a positive number")
elif num == 0:
print("It is zero")
else:
print("It is a negative number")
1010101
10101
101
def print_pattern(rows):
for i in range(rows, 0, -1):
for j in range(2 * i - 1):
if j % 2 == 0:
print("1", end="")
else:
print("0", end="")
print()
sum = 0
r=0
while (n != 0):
r = n % 10
sum = sum * 10 + r
n //= 10
if (temp == sum):
print("Palindrome")
else:
print("Not a palindrome.")
sum = 0
9. Write a program takes a number and finds the sum of digits in a number.
sum = 0
total_sum = 0
OR
Taking Input:
total_sum = 0
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
if common_items:
print("Common items:", common_items)
else:
print("There are no common items between the two lists.")
Create Sets: Convert the lists into sets (set(list1) and set(list2)). Sets are unordered collections of
unique elements, so converting lists to sets automatically removes any duplicate elements.
Intersection: Find the intersection of the two sets using the & operator. The intersection of two sets
contains only the elements that are common to both sets.
Output: If the intersection set is not empty, it means there are common items between the two lists.
In this case, print the common items. Otherwise, print a message indicating that there are no
common items between the two lists.
OR
my_tuple = (1, 2, 3, 4, 2, 5, 6, 7, 2, 8, 9, 2)
14. Print the number in words for example : 1234 => One Two Three Four
digit_words = {
'0': 'Zero', '1': 'One', '2': 'Two', '3': 'Three', '4': 'Four',
'5': 'Five', '6': 'Six', '7': 'Seven', '8': 'Eight', '9': 'Nine'
}
my_set = set()
my_set.add(1)
my_set.add(2)
my_set.add(3)
my_set.add(4)
removed_item = my_set.pop()
16. Write a program to perform following operations on set: intersection of set, union of set, set
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
intersection_set = set1.intersection(set2)
print("Intersection:", intersection_set)
union_set = set1.union(set2)
print("Union:", union_set)
difference_set = set1.difference(set2)
print("Set Difference (set1 - set2):", difference_set)
symmetric_difference_set = set1.symmetric_difference(set2)
print("Symmetric Difference:", symmetric_difference_set)
set1.clear()
print("Set1 after clearing:", set1)
17. Write a program to find the highest 3 values in a dictionary.
my_dict = {'a': 10, 'b': 30, 'c': 20, 'd': 50, 'e': 40}
highest_values = sorted_dict[:3]
my_dict = {'a': 10, 'b': 30, 'c': 20, 'd': 50, 'e': 40}
19. Write a python function that accept a string and calculate the number of upper case letters and
upper_count = 0
lower_count = 0
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
math module.
import random
21. Write a python function that takes a number as a parameter and check the number is prime or
not.
def is_prime(number):
if number < 2:
return False
return True
22. Write a program to create a user defined module that will ask your college name and will display
college.py
def get_college_name():
if __name__ == "__main__":
get_college_name()
main.py
import college
college.get_college_name()
23. Write a program that will display calendar of given month using calendar module.
import calendar
24. Write a Numpy program to generate six random integers between 10 and 30.
import numpy as np
25. Write a program to create a class “Degree” having a method “getdegree” that prints “I got a
degree”.it has two subclasses namely “undergraduate” and “postgraduate” each having a
method with the same name that print “I am an undergraduate” and “I am a postgraduate”
respectively .call the method by creating an object of each of the three classes.
class Degree:
def getdegree(self):
print("I got a degree")
class Undergraduate(Degree):
def getdegree(self):
print("I am an undergraduate")
class Postgraduate(Degree):
def getdegree(self):
print("I am a postgraduate")
degree_obj = Degree()
degree_obj.getdegree()
undergrad_obj = Undergraduate()
undergrad_obj.getdegree()
postgrad_obj = Postgraduate()
postgrad_obj.getdegree()
class A:
def method_A(self):
print("Method A from class A")
class B:
def method_B(self):
print("Method B from class B")
obj_c = C()
obj_c.method_A() # Method from class A
obj_c.method_B() # Method from class B
obj_c.method_C() # Method from class C
try:
numerator = int(input("Enter the numerator: "))
denominator = int(input("Enter the denominator: "))
except ZeroDivisionError:
print("Error: Division by zero!")
except ValueError:
print("Error: Please enter valid integers for numerator and denominator.")