Practical No. 1. Program to find the largest of three numbers.
Practical No. 1.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
print("Largest:", a)
elif b >= a and b >= c:
print("Largest:", b)
else:
print("Largest:", c)
OUTPUT:
Practical No. 2. Program to check palindrome string.
Practical No. 2.
s = input("Enter a string: ")
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
OUTPUT:
Practical No. 3. Program to count vowels in a string.
Practical No. 3.
s = input("Enter a string: ").lower()
count = 0
for ch in s:
if ch in "aeiou":
count += 1
print("Vowels:", count)
OUTPUT:
Practical No. 4. Program to check whether a substring is present in string.
Practical No. 4.
s = input("Enter main string: ")
sub = input("Enter substring: ")
if sub in s:
print("Substring found")
else:
print("Substring not found")
OUTPUT:
Practical No. 5. Program to check whether a string is pangram (contains all alphabets).
Practical No. 5.
import string
s = input("Enter a string: ").lower()
alphabet = set(string.ascii_lowercase)
if alphabet <= set(s):
print("Pangram String")
else:
print("Not Pangram")
OUTPUT:
Practical No. 6. I. Define a base class Person with the following:
A constructor that initializes the name.
A method display() that prints the name.
II. Define a derived class Student that inherits from Person with the following:
A constructor that initializes both name and roll number.
A method show() that calls the base class method to display the name and also
displays the roll number.
III. Create an object of the Student class with your own details and call the method to display
them.
Practical No. 6.
class Person:
def __init__(self, name):
[Link] = name
def display(self):
print("Name:", [Link])
class Student(Person):
def __init__(self, name, roll):
super().__init__(name) # Calling parent constructor
[Link] = roll
def show(self):
[Link]()
print("Roll:", [Link])
# Creating object of Student class
s = Student("Rahul", 101)
[Link]()
OUTPUT:
Practical No. 7. Design Develop and Implement a python program that accepts two
integers from the user and print a message saying if first number is divisible by second
number or if it is not.
Practical No. 7.
a = int(input("Enter the first integer: "))
b = int(input("Enter the second integer: "))
if b == 0:
print("Division by zero is not allowed.")
else:
if a % b == 0:
print(f"{a} is divisible by {b}.")
else:
print(f"{a} is not divisible by {b}.")
OUTPUT:
Practical No. 8. Design, Develop and Implement a program that prompts for a phone
number of 10 digit and two dashes, with dashes after the area code and the next three
number. Display if the phone number enter is valid format or not and display if the
phone number is valid or not.
Practical No. 8
def is_valid_number(phone):
if len(phone) != 12: # Check format length must be 12 (including 2 dashes)
return False
# Check dashes in correct places
if phone[3] != '-' or phone[7] != '-':
return False
# Check digits in all other positions
if not (phone[:3].isdigit() and phone[4:7].isdigit() and phone[8:].isdigit()):
return False
return True
phone_number = input("Enter a phone number in format 7830-2078-0799: ")
if is_valid_number(phone_number):
print("Phone number is in valid format.")
else:
print(" Invalid phone number format.")
OUTPUT:
Practical No. 9. Design, Develop and Implement dictionary whose keys are month name
and whose values are number of days in the corresponding month:
I. Ask the user to enter the name of a month and use the dictionary to display how many
days are in that month.
II. Print out all of the keys (month names) in alphabetical order.
III. Print out all of the months that have 31 days.
Display all the (key–value) pairs sorted by the number of days in each month.
Practical No. 9
# Dictionary of months and days
months = {
"January": 31,
"February": 28, # not handling leap year here
"March": 31,
"April": 30,
"May": 31,
"June": 30,
"July": 31,
"August": 31,
"September": 30,
"October": 31,
"November": 30,
"December": 31
}
# I. Ask user for month name and show number of days
user_month = input("Enter the month name: ").capitalize()
if user_month in months:
print(f"{user_month} has {months[user_month]} days.")
else:
print("Invalid month name entered.")
print("\n--- Output Section ---")
# II. Print all keys in alphabetical order
print("\nMonths in alphabetical order:")
for month in sorted([Link]()):
print(month)
# III. Print months with 31 days
print("\nMonths with 31 days:")
for month, days in [Link]():
if days == 31:
print(month)
# IV. Key-value pairs sorted by number of days
print("\nMonths sorted by number of days:")
for month, days in sorted([Link](), key=lambda item: item[1]):
print(f"{month}: {days}")
OUTPUT:
Practical No. 10. Design, Develop and Implement a Python program to demonstrate
multiple inheritance. Consider 3 classes with the following description:
Student class has 3 protected data members roll number, mark l and mark 2 of type
integer. It has a get() function to get these details from the user. Sports class has a
protected data member sports marks of type integer and a function getsm() to get the
sports mark.
Statement class uses the marks from Student class and the sports marks from the Sports
class to calculate the total and average and displays the final result.
Practical No. 10
class Student:
def __init__(self):
# Protected data members
self._rollno = 0
self._mark1 = 0
self._mark2 = 0
def get(self):
"""Get student academic details from user"""
self._rollno = int(input("Enter Roll Number: "))
self._mark1 = int(input("Enter Mark 1: "))
self._mark2 = int(input("Enter Mark 2: "))
class Sports:
def __init__(self):
# Protected data member
self._sports_marks = 0
def getsm(self):
"""Get sports marks"""
self._sports_marks = int(input("Enter Sports Marks: "))
class Statement(Student, Sports):
def __init__(self):
# Initialize parent classes
Student.__init__(self)
Sports.__init__(self)
def display_result(self):
"""Calculate and display total and average"""
total = self._mark1 + self._mark2 + self._sports_marks
average = total / 3
print("\n--- Final Statement ---")
print(f"Roll Number : {self._rollno}")
print(f"Mark 1 : {self._mark1}")
print(f"Mark 2 : {self._mark2}")
print(f"Sports Mark : {self._sports_marks}")
print(f"Total Marks : {total}")
print(f"Average : {average:.2f}")
obj = Statement()
[Link]() # get academic marks
[Link]() # get sports marks
obj.display_result()
OUTPUT: