0% found this document useful (0 votes)
56 views4 pages

Python Functions and Classes Examples

Uploaded by

xorir27780
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)
56 views4 pages

Python Functions and Classes Examples

Uploaded by

xorir27780
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

Lab Cycle 2

1. Write a Python function to calculate the factorial of a number (a non-negative integer).


The function accepts the number as an argument.
2. Write a function to find sum,difference,product,division of two numbers (use MULTIPLE
RETURN VALUES)
3. Write a function to perform double each elements of a list
4. Write a python program to define a module to find Fibonacci Numbers and import the
module to another program
5. Write a python progr
am to define a module and import a specific function in that module to another program
6. Write a Python class to convert an integer to a roman numeral
7. Write a Python class to reverse a string word by word.
8. Write a Python class to implement pow(x, n)
4.# Fibonacci numbers module

def fib(n): # write Fibonacci series up to n


a, b = 0, 1
while b < n:
print(b, end =" ")
a, b = b, a+b

'''Write a python program to define a module to find Fibonacci Numbers and import the
module to another program'''
#import fibonacci module
import fibonacci
num=int(input("Enter any number to print Fibonacci series "))
[Link](num)

5 Write a python program to define a module and import a specific function in that module
to another program

''' Arithmetic Operations Module with Multiple functions'''


def Add(a,b):
c=a+b
return c
def Sub(a,b):
c=a-b
return c
def Mul(a,b):
c=a*b
return c

'''Write a python program to define a module and import a specific function in that
module to another program.'''
from arth import Add
num1=float(input("Enter first Number : "))
num2=float(input("Enter second Number : "))
print("Addition is : ",Add(num1,num2))
print("Subtraction is : ",Sub(num1,num2)) #gives error:Not importing Sub function from arth
Module
6.
'''Write a Python class to convert an integer to a roman numeral.'''

class irconvert:
num_map = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'),(50, 'L'), (40,
'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')]

def num2roman(self,num):
roman = ''
while num > 0:
for i, r in self.num_map:
while num >= i:
roman += r
num -= i
return roman

num=int(input("Enter any Number :"))


print("Roman Number is : ",irconvert().num2roman(num))

7. '' Write a Python class to reverse a string word by word. '''


class py_reverse:
def revr(self, strs):
sp=[Link]()
[Link]()
res=" ".join(sp)
return res

str1=input("Enter a string with 2 or more words : ")


print("Reverse of string word by word: \n",py_reverse().revr(str1));

8.
'''Write a Python class to implement pow(x, n)'''
class py_pow:
def powr(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/[Link](x,-n)
val = [Link](x,n//2)
if n%2 ==0:
return val*val
return val*val*x

x=int(input("Enter x value :"))


n=int(input("Enter n value :"))
print("pow(x,n) value is :",py_pow().powr(x,n));

You might also like