0% found this document useful (0 votes)
3 views5 pages

Experiment 20 Python

The document contains multiple Python code snippets demonstrating various functionalities. It includes a method for calculating power, reversing words in a string, converting numbers to Roman numerals, and calculating the area of a rectangle. Each code snippet is followed by a print statement to display the output of the respective function.

Uploaded by

snchandanshiv12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Experiment 20 Python

The document contains multiple Python code snippets demonstrating various functionalities. It includes a method for calculating power, reversing words in a string, converting numbers to Roman numerals, and calculating the area of a rectangle. Each code snippet is followed by a print statement to display the output of the respective function.

Uploaded by

snchandanshiv12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

.

EXPERIMENT 20

class py_solution:

def pow(self, x, n):

if x==0 or x==1 or n==1:

return x

if x==-1:

if n%2 ==0:

return 1

else:

return -1

if n==0:

return 1

if n<0:

return 1/self.pow(x,-n)

val = self.pow(x,n//2)

if n%2 ==0:

return val*val
return val*val*x

print(py_solution().pow(2, -3));

print(py_solution().pow(3, 5));

print(py_solution().pow(100, 0));

OUTPUT:

Q.2
class py_solution:

def reverse_words(self, s):

return ' '.join(reversed(s.split()))

print(py_solution().reverse_words('hello .py'))

OUTPUT:
Q.3
def printRoman(number):

num = [1, 4, 5, 9, 10, 40, 50, 90,

100, 400, 500, 900, 1000]

sym = ["I", "IV", "V", "IX", "X", "XL",

"L", "XC", "C", "CD", "D", "CM", "M"]

i = 12

while number:

div = number // num[i]

number %= num[i]

while div:

print(sym[i], end = "")

div -= 1

i -= 1
if __name__ == "__main__":

number = 3549

print("Roman value is:", end = " ")

printRoman(number)

OUTPUT:

Q.5
class Rectangle():

def __init__(self, l, w):

self.length = l

self.width = w

def rectangle_area(self):

return self.length*self.width

newRectangle = Rectangle(12, 10)


print(newRectangle.rectangle_area())

OUTPUT:

You might also like