0% found this document useful (0 votes)
36 views

Working With Functions Practical

The document contains 8 Python programs that demonstrate the use of functions. Program 1 defines a math function and calls it to evaluate an expression. Program 2 defines a cube function and prints the cube of numbers from 1 to 5. Program 3 defines an area function to calculate the area of a circle. The remaining programs demonstrate additional uses of functions like calculating sums and averages, returning multiple values from a function, and showing how mutable and immutable data types are handled when passed to functions.

Uploaded by

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

Working With Functions Practical

The document contains 8 Python programs that demonstrate the use of functions. Program 1 defines a math function and calls it to evaluate an expression. Program 2 defines a cube function and prints the cube of numbers from 1 to 5. Program 3 defines an area function to calculate the area of a circle. The remaining programs demonstrate additional uses of functions like calculating sums and averages, returning multiple values from a function, and showing how mutable and immutable data types are handled when passed to functions.

Uploaded by

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

Chapter = 1 = Working With Functions

# PROGRAM 1-'To solve a Mathematical Function with a


# given value':
#definition of the function
def mathfunc(x):
func1=4*x**4+3*x**3+2*x**2+x+1
return func1
# main function
num=int(input('Enter the number:'))
function1=mathfunc(num)
print('The value of the func1 with value',num,'is',function1,'.')
# OUTPUT:
Enter the number:5
The value of the func1 with value 5 is 2931 .
# PROGRAM 2-'Printing the cube of various numbers:'
def cube(x):
func2=x**3
return func2
for i in range(1,6):
print('The cube of',i,'is',cube(i))
# OUTPUT:
The cube of 1 is 1
The cube of 2 is 8
The cube of 3 is 27
The cube of 4 is 64
The cube of 5 is 125
# PROGRAM 3-'Finding the area of square:'
pi=3.14
def area(r):
area1=pi*r**2
return area1
rad=int(input('Enter the radius:'))
area2=area(rad)
print('The area of the circle with radius',rad,'is',area2,'unit
square')
# OUTPUT:
Enter the radius:10
The area of the circle with radius 10 is 314.0 unit square.
# PROGRAM 4-'To display the sum and average of three
# numbers:'
def calcsum(a,b,c):
s=a+b+c
return s
def average(x,y,z):
sm=calcsum(x,y,z)
return sm/3
num1=int(input('Enter the first number:'))
num2=int(input('Enter the second number:'))
num3=int(input('Enter the third number:'))
print('The sum of the numbers is',calcsum(num1,num2,num3))
print('The average of the numbers
is',average(num1,num2,num3))
# OUTPUT:
Enter the first number:150
Enter the second number:300
Enter the third number:450
The sum of the numbers is 900
The average of the numbers is 300.0
# PROGRAM 5-'To display simple interest with default and
# given values:'
def interest(principal,time=4,rate=0.09):
return principal*time*rate
prin=float(input('Enter the pricipal amount:'))
print('Simple interest with default time and rate is:')
si1=interest(prin)
print('Rs.',si1)
time=int(input('Enter the duration in years:'))
rate=float(input('Enter the rate of interest:'))
print('Simple interest with given principal, time and rate is:')
si2=interest(prin,time,rate)
print('Rs.',si2)
# OUTPUT:
Enter the pricipal amount:7000
Simple interest with default time and rate is:
Rs. 2520.0
Enter the duration in years:2
Enter the rate of interest:0.10
Simple interest with given principal, time and rate is:
Rs. 1400.0
# PROGRAM 6-'Program to return multiple values:'
def arcalc(x,y):
return x+y,x-y,x*y,x/y,x%y
num1=int(input('Enter the first number:'))
num2=int(input('Enter the second number:'))
add,sub,mult,div,mod=arcalc(num1,num2)
print('Sum of the given numbers:',add)
print('Subtraction of the given numbers:',sub)
print('Product of the given numbers:',mult)
print('Division of the given numbers:',div)
print('Modulo of the given numbers:',mod)
# OUTPUT:
Enter the first number:50
Enter the second number:20
Sum of the given numbers: 70
Subtraction of the given numbers: 30
Product of the given numbers: 1000
Division of the given numbers: 2.5
Modulo of the given numbers: 10
# PROGRAM 7-'To show mutable data type show changes:'
def myfunc1(mylist):
print('Inside Called Function now')
print('List rececived:',mylist)
mylist[2]+=10
mylist.append(7)
mylist.extend([8,9])
print('List within called function, after changes:',mylist)
return
List1=[1,3,4,5,6]
print('List before function call:',List1)
myfunc1(List1)
print('List after function call:',List1)
# OUTPUT:
List before function call: [1, 3, 4, 5, 6]
Inside Called Function now
List rececived: [1, 3, 4, 5, 6]
List within called function, after changes: [1, 3, 14, 5, 6, 7, 8, 9]
List after function call: [1, 3, 14, 5, 6, 7, 8, 9]
# PROGRAM 8-'To show immutable data type don't show
changes:'
def myfunc2(a):
print('Inside myfunc2()')
print("Value received in 'a' as",a)
a+=52
print("Value of 'a' now changes to",a)
print('Returning from myfunc2()')
num=int(input('Enter the number:'))
print("Calling myfun2() by passing 'num' with value",num)
myfunc2(num)
print('Back from myfunc2:')
print("Value of 'num' is",num)
# OUTPUT:
Enter the number:10
Calling myfun2() by passing 'num'with value 10
Insidemyfunc2()
Value received in 'a' as 10
Value of 'a' now changes to 62
Returning from myfunc2()
Back from myfunc2:
Value of 'num' is 10

You might also like