Python Programming Practical No.20 Answers
Python Programming Practical No.20 Answers
ir
eS
ad
oh
M
IX Conclusion:
Ans
Conclusion:
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()
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
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())
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
ir
width = float(input("Enter the width of the rectangle: "))
eS
rect = Rectangle(length, width)