1. Write a program to convert bits to Megabytes, Gigabytes and Terabytes.
bits = int(input("Enter the number of bits: "))
megabytes = bits / (1024 * 1024 * 8)
print("Megabytes:", megabytes)
gigabytes = bits / (1024 * 1024 * 1024 * 8)
print("Gigabytes:", gigabytes)
terabytes = bits / (1024 * 1024 * 1024 * 1024 * 8)
print("Terabytes:", terabytes)
2. Write a program to calculate surface volume and area of a cylinder.
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.
print("Enter Marks Obtained in 5 Subjects..!")
Sub1 = int(input("Enter Marks: "))
Sub2 = int(input("Enter Marks: "))
Sub3 = int(input("Enter Marks: "))
Sub4 = int(input("Enter Marks: "))
Sub5 = int(input("Enter Marks: "))
tot = Sub1 + Sub2 + Sub3 + Sub4 + Sub5
avg = tot / 5
if avg >= 91 and avg <= 100:
print("Your Grade is A1")
elif avg >= 81 and avg < 91:
print("Your Grade is A2")
elif avg >= 71 and avg < 81:
print("Your Grade is B1")
elif avg >= 61 and avg < 71:
print("Your Grade is B2")
elif avg >= 51 and avg < 61:
print("Your Grade is C1")
elif avg >= 41 and avg < 51:
print("Your Grade is C2")
elif avg >= 33 and avg < 41:
print("Your Grade is D")
elif avg >= 0 and avg < 33:
print("Fail")
else:
print("Invalid Input!")
4. Write a program to check if the input year is leap year or not.
year=int(input("Enter year to be checked:"))
if(year%4==0 and year%100!=0 or year%400==0):
print("The year is a leap year!")
else:
print("The year isn't a leap year!")
5. Write a program to check if a number is positive, negative and zero.
num = float(input("Input a number: "))
if num > 0:
print("It is a positive number")
elif num == 0:
print("It is zero")
else:
print("It is a negative number")
6. Write a program Print the following pattern :
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()
# Test the function with a sample input
rows = 4
print_pattern(rows)
7. Write a program that takes a number and check whether it is a palindrome or not
sum = 0
r=0
n = int(input("Enter an integer: "))
temp = n
while (n != 0):
r = n % 10
sum = sum * 10 + r
n //= 10
if (temp == sum):
print("Palindrome")
else:
print("Not a palindrome.")
8. Write a program to reverse a given number.
num = int(input("Enter a number: "))
sum = 0
while num > 0:
r = num % 10
sum = sum * 10 + r
num //= 10
print("Reversed number:", sum)
9. Write a program takes a number and finds the sum of digits in a number.
num = int(input("Enter a number: "))
sum = 0
while num > 0:
r = num % 10
sum = sum + r
num //= 10
print("Sum of Digits:", sum)
10. Write a program to sum all the items in a list.
Initialising the list in program:
my_list = [10, 20, 30, 40, 50]
total_sum = 0
for item in my_list:
total_sum += item
print("Sum of all items in the list:", total_sum)
OR
Taking Input:
input_list = input("Enter the list of numbers separated by space: ").split()
my_list = [int(num) for num in input_list]
total_sum = 0
for item in my_list:
total_sum += item
print("Sum of all items in the list:", total_sum)
11. Write a program to find common items from two list.
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
common_items = set(list1) & set(list2)
if common_items:
print("Common items:", common_items)
else:
print("There are no common items between the two lists.")
Explanation (Just for Program understanding purpose ):
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.
12. Write a python program to select the even items of a list.
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for item in my_list:
if (item%2==0):
print(item)
OR
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_items = [item for item in my_list if item % 2 == 0]
print("Even items:", even_items)
13. Write a program to find repeated items of a tuple.
my_tuple = (1, 2, 3, 4, 2, 5, 6, 7, 2, 8, 9, 2)
repeated_items = set(item for item in my_tuple if my_tuple.count(item) > 1)
print("Repeated items:", repeated_items)
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'
}
number = input("Enter a number: ")
words = [digit_words[digit] for digit in number]
print("Number in words:", ' '.join(words))
15. Write a program to create a set,add members in a set and remove one item from set.
my_set = set()
my_set.add(1)
my_set.add(2)
my_set.add(3)
my_set.add(4)
print("Set before removing an item:", my_set)
removed_item = my_set.pop()
print("Removed item:", removed_item)
print("Set after removing an item:", my_set)
16. Write a program to perform following operations on set: intersection of set, union of set, set
difference , symmetric difference ,clear a 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}
sorted_dict = sorted(my_dict.items(), key=lambda x: x[1], reverse=True)
highest_values = sorted_dict[:3]
print("Highest 3 values in the dictionary:")
for key, value in highest_values:
print(f"{key}: {value}")
18. Write a program to sort (ascending and descending) a dictionary by value.
my_dict = {'a': 10, 'b': 30, 'c': 20, 'd': 50, 'e': 40}
sorted_dict_asc = sorted(my_dict.items(), key=lambda x: x[1])
sorted_dict_desc = sorted(my_dict.items(), key=lambda x: x[1], reverse=True)
print("Sorted dictionary (ascending):")
for key, value in sorted_dict_asc:
print(f"{key}: {value}")
print("\nSorted dictionary (descending):")
for key, value in sorted_dict_desc:
print(f"{key}: {value}")
19. Write a python function that accept a string and calculate the number of upper case letters and
lower case letters.
input_string = input("Enter a string: ")
upper_count = 0
lower_count = 0
for char in input_string:
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
print("Number of uppercase letters:", upper_count)
print("Number of lowercase letters:", lower_count)
20. Write a program to generate a random float where the value is between 5 and 50 using python
math module.
import random
random_float = random.uniform(5, 50)
print("Random float between 5 and 50:", random_float)
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
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return False
return True
number = int(input("Enter a number: "))
if is_prime(number):
print(number, "is a prime number")
else:
print(number, "is not a prime number")
22. Write a program to create a user defined module that will ask your college name and will display
the name of the college.
college.py
def get_college_name():
college_name = input("Enter your college name: ")
print("Your college name is:", 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
year = int(input("Enter the year (e.g., 2022): "))
month = int(input("Enter the month (1-12): "))
if month < 1 or month > 12:
print("Invalid month! Please enter a month between 1 and 12.")
else:
cal = calendar.month(year, month)
print("\nCalendar:")
print(cal)
24. Write a Numpy program to generate six random integers between 10 and 30.
import numpy as np
random_integers = np.random.randint(10, 31, size=6)
print("Six random integers between 10 and 30:", random_integers)
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()
26. Write a program to implement multiple inheritance.
class A:
def method_A(self):
print("Method A from class A")
class B:
def method_B(self):
print("Method B from class B")
class C(A, B):
def method_C(self):
print("Method C from class C")
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
27. Write a program to check for ZeroDivisionrrorException.
try:
numerator = int(input("Enter the numerator: "))
denominator = int(input("Enter the denominator: "))
result = numerator / denominator
print("Result:", result)
except ZeroDivisionError:
print("Error: Division by zero!")
except ValueError:
print("Error: Please enter valid integers for numerator and denominator.")