Write a program to convert bits to Megabytes, Gigabytes and Terabytes.
bits = int(input("Enter the bits value: "))
print("Initial bits value:", bits)
megabytes = bits / 1000000
gigabytes = bits / (8 * 10**9)
terabytes = bits / (8 * 10**12)
print("Bits to megabytes:", megabytes)
print("Bits to megabytes:", gigabytes)
print("Bits to megabytes:", terabytes)
Write a program to calculate surface volume and area of a cylinder.
import math
# Input
radius = float(input("Enter the radius of the cylinder: "))
height = float(input("Enter the height of the cylinder: "))
# Calculations
volume = math.pi * radius ** 2 * height
surface_area = 2 * math.pi * radius * (radius + height)
# Output
print("Volume of the cylinder:", volume)
print("Surface area of the cylinder:", surface_area)
Write a program that takes the marks of 5 subject and display the grade.
marks = []
for i in range(5):
subject_mark = float(input(f"Enter the marks for subject {i+1}: "))
marks.append(subject_mark)
total_marks = sum(marks)
average_marks = total_marks / 5
grade = None
if average_marks >= 90:
grade = 'A'
elif 80 <= average_marks < 90:
grade = 'B'
elif 70 <= average_marks < 80:
grade = 'C'
elif 60 <= average_marks < 70:
grade = 'D'
else:
grade = 'F'
print("Total marks:", total_marks)
print("Average marks:", average_marks)
print("Grade:", grade)
Write a program to check if the input year is leap year or not.
year = int(input("Enter a year: "))
is_leap_year = False
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
is_leap_year = True
else:
is_leap_year = True
if is_leap_year:
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
Write a program to check if a number is positive, negative and zero.
number = float(input("Enter a number: "))
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
Write a program Print the following pattern :
1010101
10101
101
1
rows = 4
for i in range(rows, 0, -1):
# Print spaces
for j in range(rows - i):
print(" ", end="")
# Print numbers
for k in range(2 * i - 1):
if k % 2 == 0:
print("1", end="")
else:
print("0", end="")
print()
Write a program that takes a number and check whether it is a palindrome or not
# Input
number = input("Enter a number: ")
# Check if it's a palindrome
if number == number[::-1]:
print(number, "is a palindrome.")
else:
print(number, "is not a palindrome.")
Write a program to reverse a given number.
# Input
number = int(input("Enter a number: "))
# Initialize variables
reversed_number = 0
# Reverse the number
while number > 0:
digit = number % 10
reversed_number = reversed_number * 10 + digit
number //= 10
# Output
print("Reversed number:", reversed_number)
Write a program takes a number and finds the sum of digits in a number.
# Input
number = int(input("Enter a number: "))
# Initialize sum
sum_of_digits = 0
# Calculate sum of digits
while number > 0:
# Extract the last digit of the number
digit = number % 10
# Add the digit to the sum
sum_of_digits += digit
# Remove the last digit from the number
number //= 10
# Output
print("Sum of digits:", sum_of_digits)
Write a program to sum all the items in a list.
total = 0
# creating a list
list1 = [11, 5, 17, 18, 23]
# Iterate each element in list
# and add them in variable total
for ele in range(0, len(list1)):
total = total + list1[ele]
# printing total value
print("Sum of all elements in given list: ", total)
Write a program to find common items from two list.
def find_common_items(list1, list2):
common_items = []
for item in list1:
if item in list2:
common_items.append(item)
return common_items
# Example usage:
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
common_items = find_common_items(list1, list2)
print("Common items:", common_items)
Write a python program to select the even items of a list.
list1 = [10, 21, 4, 45, 66, 93]
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
print(num, end=" ")
Write a program to find repeated items of a tuple.
mt = (1,2,3,4,1,3,4)
my_list = []
for item in mt:
if mt.count(item) > 1:
if item not in my_list:
my_list.append(item)
print(my_list)
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)
my_set.add(5)
print("Set after adding members:", my_set)
my_set.remove(3)
print("Set after removing one item:", my_set)
Write a program to perform following operations on set: intersection of set, union of set, set
difference , symmetric difference ,clear a set.
# Create two sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
# Intersection of sets
intersection = set1.intersection(set2)
print("Intersection of sets:", intersection)
# Union of sets
union = set1.union(set2)
print("Union of sets:", union)
# Set difference
difference = set1.difference(set2)
print("Set difference (set1 - set2):", difference)
# Symmetric difference
symmetric_difference = set1.symmetric_difference(set2)
print("Symmetric difference:", symmetric_difference)
# Clear a set
set1.clear()
print("Set1 after clearing:", set1)
Write a program to find the highest 3 values in a dictionary.
def highest_3_values_1(my_dict):
result_dict = {}
sorted_dict = sorted(my_dict.items(), key=lambda x: x[1], reverse=True)
for i in range(3):
result_dict[sorted_dict[i][0]] = sorted_dict[i][1]
return result_dict
my_dict = {'A': 67, 'B': 23, 'C': 45, 'D': 56, 'E': 12, 'F': 69}
print(highest_3_values_1(my_dict))
Write a program to sort (ascending and descending) a dictionary by value.
my_dict = {'a': 10, 'b': 30, 'c': 20, 'd': 40, 'e': 25}
# Sort the dictionary by values in ascending order
sorted_ascending = sorted(my_dict.items(), key=lambda x: x[1])
# Sort the dictionary by values in descending order
sorted_descending = sorted(my_dict.items(), key=lambda x: x[1], reverse=True)
# Print sorted dictionaries
print("Sorted by value (ascending):", dict(sorted_ascending))
print("Sorted by value (descending):", dict(sorted_descending))
Write a python function that accept a string and calculate the number of upper case letters and
lower case letters.
def count_upper_lower(string):
upper_count = 0
lower_count = 0
for char in string:
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
return upper_count, lower_count
input_string = input("enter the string")
upper_count, lower_count = count_upper_lower(input_string)
print("Number of uppercase letters:", upper_count)
print("Number of lowercase letters:", lower_count)
Write a program to generate a random float where the value is between 5 and 50 using python
math module.
import random
# Generate a random float between 5 and 50
random_float = random.uniform(5, 50)
print("Random float between 5 and 50:", random_float)
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 <= 1:
print(number, "is not a prime number")
return False
for i in range(2, number):
if number % i == 0:
print(number, "is not a prime number")
return False
print(number, "is a prime number")
return True
# Example usage:
num = int(input("Enter a number: "))
is_prime(num)
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: ")
return college_name
def display_college_name():
college_name = get_college_name()
print("Your college name is:", college_name)
main.py
import college
college.display_college_name()
Write a program that will display calendar of given month using calendar module.
import calendar
def display_calendar(year, month):
cal = calendar.month(year, month)
print("Calendar:")
print(cal)
year = int(input("Enter the year: "))
month = int(input("Enter the month (1-12): "))
display_calendar(year, month)
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)
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")
# Create objects of each class and call the method
degree_obj = Degree()
degree_obj.getdegree()
undergrad_obj = Undergraduate()
undergrad_obj.getdegree()
postgrad_obj = Postgraduate()
postgrad_obj.getdegree()
Write a program to implement multiple inheritance.
class Animal:
def make_sound(self):
print("Some generic sound")
class Dog(Animal):
def make_sound(self):
print("Woof")
class Cat(Animal):
def make_sound(self):
print("Meow")
class DogCat(Dog, Cat):
pass
# Create an object of DogCat class and call the make_sound method
dog_cat = DogCat()
dog_cat.make_sound()
Write a program to check for ZeroDivisionrrorException.
try:
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
result = num1 / num2
print("Result:", result)
except ZeroDivisionError:
print("Error: Division by zero!")
except ValueError:
print("Error: Please enter valid integers for numerator and denominator.")