Category: Python Programs
-
Python program to display student’s information using multiple inheritances
Here’s an example Python program that demonstrates how to use multiple inheritances to display student information: # Python program to display student’s information using multiple inheritances # define a class for student information class StudentInfo: def __init__(self, name, rollno): self.name = name self.rollno = rollno def display_info(self): print(“Name:”, self.name) print(“Roll No:”, self.rollno) # define a…
-
Python Program for handling file operation errors
Handling file operation errors is an important aspect of writing robust code that can handle unexpected situations. Here’s an example Python program that demonstrates how to handle file operation errors: # Python program to handle file operation errors try: # open the file for reading with open(“example.txt”, “r”) as file: # read the contents of…
-
Python program that transposes a matrix using nested loops:
Here’s a Python program that transposes a matrix using nested loops # Python program to transpose a matrix using nested loops # input matrix matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # get dimensions of the matrix rows = len(matrix) cols = len(matrix[0]) # create a new matrix to store the…
-
Python Program to sort the words alphabetically form a string given by the user
Here’s a Python program that sorts the words alphabetically in a string provided by the user: Approach 1: # Python program to sort words alphabetically in a string # input string string = input(“Enter a string: “) # split the string into words words = string.split() # sort the words alphabetically sorted_words = sorted(words) #…
-
Python Program to count the number of each vowel in a string
Here’s a Python program that counts the number of each vowel in a string: Approach 1: # Python program to count the number of each vowel in a string # input string string = input(“Enter a string: “) # initialize count variables a_count = 0 e_count = 0 i_count = 0 o_count = 0 u_count…
-
Print calendar of a given month and year Python program
Print calendar of a given month and year Python program import calendar y = int(input(“Input the year : “)) m = int(input(“Input the month : “)) print(calendar.month(y, m)) Input the year : 2022 Input the month : 9 September 2022 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6…
-
Calculate number of days between two dates Python program
Calculate number of days between two dates Python program from datetime import date date1 = date(2017, 1, 23) date2 = date(2022, 12, 1) total_days = date2 – date1 print(‘The number of days between the given two dates is :’) print(total_days.days) Output: The number of days between the given two dates is : 2138 >>>
-
Python Program to sort alphabetically the words form a string provided by the user
Python Program to sort alphabetically the words form a string provided by the user my_str = input(“Enter a string: “) # breakdown the string into a list of words words = my_str.split() # sort the list words.sort() # display the sorted words for word in words: print(word) Output: Enter a string: Welcome to the…
-
Python Program to find Volumes & Surface Areas of 3D Shapes Cube, Cuboid, Cylinder, Cone,Sphere,Hemisphere
Python Program to find Volumes & Surface Areas of 3D Shapes Cube, Cuboid, Cylinder, Cone,Sphere,Hemisphere ”’Python Program to find Volumes & Surface Areas of 3D Shapes Cube, Cuboid, Cylinder, Cone,Sphere,Hemisphere”’ e=1 while e!=0: print(“TO CALCULATE VOLUMES & SURFACE AREAS OF FOLLOWING SHAPES”) print(“1. Cube”) print(“2. Cuboid”) print(“3. Cylinder”) print(“4. Cone”) print(“5. Sphere”) print(“6. Hemisphere”) print(“0.…
-
Python program to find all the roots of a given cubic equation
Python program to find all the roots of a given cubic equation # Python program to find all the roots of a given cubic equation import fractions print(“The equation is-\n \t ax^3 + bx^2 + cx + d = 0”) a = eval(input(“Enter the value of a\t”)) b = eval(input(“Enter the value of b\t”))…