Sitemap
Raksheshashank

Raksheshashank

Python Exercise for Function

2 min readNov 30, 2021
  1. Write a program to calculate the payable amount for the tent:

print(“Enter values for the cyclindrical part of the”
“ tent in meters\n “)
h=float(input(“Enter height of cylinderical part:”))
r=float(input(“Enter radius:”))
l=float(input(“Enter the slant height of conical part in meter”))
csa_conical=3.14*r*l
csa_cyclindrical=2*3.14*r*l
canvas_area=csa_conical+csa_cyclindrical
print(“The area of the canvas is”, int(canvas_area),”m²”)
unit_price=float(input(“Enter the cost of 1 m² canvas:”))
total_cost=unit_price*canvas_area
print(“The total cost of canvas=”,int(total_cost))
tax=0.18*total_cost
net_price=total_cost+tax
print(“Net amount payable=”, int(net_price))

  1. The above same problem is solved using function:

print(“Enter values for the cyclindrical part of the”
“ tent in meters\n “)
h=float(input(“Enter height of cylinderical part:”))
r=float(input(“Enter radius:”))
l=float(input(“Enter the slant height of conical part in meter:”))
unit_price=float(input(“Enter the cost of 1 m² canvas:”))
def cyclinderical_area(h,r):
return(2*3.14*r*h)

def conical_area(r,l):
return(3.14*r*l)

def canvas_area():
return(cyclinderical_area(h,r)+conical_area(r,l))

print(“Canvas_Area:”,canvas_area())

def total_price(unit_price):
cost=canvas_area()*unit_price
return(cost+cost*0.18)

print(“total_price:”,total_price(unit_price))

2. Write a user defined function to add 2 numbers and display their sum.

Ans:n1=float(input(“Enter number 1:”))
n2=float(input(“Enter number 2:”))
def sum(n1,n2):
a=n1+n2
return(a)
print(sum(n1,n2))

3. Write a program using a user defined function that display sum of first n natural numbers, where n is passed as argument

Get Raksheshashank’s stories in your inbox

Join Medium for free to get updates from this writer.

def sum_1(n):
a=0
for i in range (1,n+1):
a=a+i
return(a)

4. Write a program using user defined function that accepts an integer and increment the value by 5. Also display the id of argument (before function call), id of parameter before increment and after increment

n=int(input(“Enter the value: “))
print(id(n))
def incr(n):
print(id(n))
n1=n+5
return(n1)
print(incr(n))
print(id(incr(n)))

5. Write a program using a user defined function myMean() to calculate the mean of floating values stored in a list.

Ans: n=[5,10,15,20]

def myMean(n):
total=0
count=0
for i in n:
total=total+i
count=count+1
mean=total/count
return(mean)

print(myMean(n))

Ans if user define a list:

n=input(‘Enter element of list separated by space’)
n_list=n.split()
print(n_list)
for i in range(len(n_list)):
n_list[i]=int(n_list[i])
def myMean(n_list):
total=0
count=0
for i in n_list:
total=total + i
count=count + 1
mean=(total/count)
return(mean)
print(myMean(n_list))

6. Write a program using a user defined function calcFact() to calculate and display the factorial of a number num passed as an argument.

Ans:def calcFact(n):
fact=1
for i in range(1,n+1):
fact=fact*i
print(fact)

7. Write a program using a user defined function that accepts the first name and last name as arguments, concatenate them to get full name and displays the output as: Hello Full Name

Ans:f=input(‘Enter your first name:’)
l=input(‘Enter your second name:’)
def full_name(f,l):
a=(f)+’ ‘+(l)
return(a)
print(“Hello”,full_name(f,l))

7. Write a program that accepts a numerator and denominator of a fractional number and call a user defined function mixed Fraction() when the fraction formed is not a proper function. The default value of denominator is 1. The function displays a mixed fraction formed by the parameters does not evaluate to a whole number.

Ans:num=int(input(‘Enter a value:’))
deno=int(input(‘Enter a value:’))
def mixedFraction(num,deno=1):
remainder=num%deno
if remainder!=0:
quotient=int(num/deno)
print(quotient,”(“,remainder,”/”,deno,”)”)
else:
print(“The given fraction is whole num”)

--

--