🌟 Diplomatech Academy 🌟
Manual Solutions and outputs
Python Programming Course Code: 314004
-Mohade Sir©
Practical No. 20: Develop a python program to perform following
operations: 1. Creating a Class with method 2. Creating Objects of class
3. Accessing method using object
ir
eS
ad
oh
M
IX Conclusion:
Ans
Conclusion:
We have successfully developed a Python program to demonstrate
Object-Oriented Programming (OOP) concepts, including:
✅ Creating a Class with a method to initialize and process data.
✅ Creating Objects of the class to represent real-world entities.
✅ Accessing Methods using objects to perform specific operations.
By implementing classes and objects, we achieve better code organization,
reusability, and modularity, making Python programs more efficient and
scalable.
ir
X Practical related questions
eS
1. Write a Python class to implement pow(x, n).
Ans.
class Power:
def calculate(self, x, n):
return x ** n # Using exponentiation operator
ad
# Creating object of class
obj = Power()
# Testing the method
oh
x = int(input("Enter base (x): "))
n = int(input("Enter exponent (n): "))
result = obj.calculate(x, n)
print(f"{x}^{n} = {result}")
M
2. Write a Python class to reverse a string word by word.
Ans.
class StringReverser:
def reverse_words(self, sentence):
return ' '.join(sentence.split()[::-1]) # Splitting and reversing words
# Creating object of class
obj = StringReverser()
# Taking user input
sentence = input("Enter a sentence: ")
# Reversing and displaying the result
print("Reversed sentence:", obj.reverse_words(sentence))
output/
ir
Enter a sentence: Hello world Python
Reversed sentence: Python world Hello
eS
3. Write a Python class to convert an integer to a roman numeral.
Ans.
class IntegerToRoman:
def int_to_roman(self, num):
roman_numerals = {
ad
1: "I", 4: "IV", 5: "V", 9: "IX", 10: "X", 40: "XL", 50: "L",
90: "XC", 100: "C", 400: "CD", 500: "D", 900: "CM", 1000: "M"
}
oh
result = ""
for value in sorted(roman_numerals.keys(), reverse=True): # Sorting keys in
descending order
while num >= value:
result += roman_numerals[value]
M
num -= value
return result
# Creating object of class
converter = IntegerToRoman()
# Taking user input
num = int(input("Enter an integer: "))
# Converting and displaying the result
print("Roman Numeral:", converter.int_to_roman(num))
4. Write a Python class that has two methods: get_String and print_String ,
get_String accept a
string from the user and print_String prints the string in upper case.
Ans.
ir
class StringManipulator:
def __init__(self):
self.user_string = ""
eS
def get_String(self):
self.user_string = input("Enter a string: ")
def print_String(self):
ad
print("Uppercase String:", self.user_string.upper())
# Creating object of class
str_obj = StringManipulator()
oh
# Getting user input
str_obj.get_String()
# Printing the string in uppercase
M
str_obj.print_String()
5. Write a Python class named Rectangle constructed from length and width
and a method that
will compute the area of a rectangle.
Ans
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
# Taking user input for length and width
length = float(input("Enter the length of the rectangle: "))
ir
width = float(input("Enter the width of the rectangle: "))
# Creating an object of Rectangle class
eS
rect = Rectangle(length, width)
# Calculating and displaying the area
print("Area of the rectangle:", rect.area())
ad
oh
M