# Python User define Function,How to create function in python program

56

## if we create a function-based program in python then it is called the procedural pattern of python code structure.

### Example of User define a function to calculate addition and subtraction in a Single File:-

a = int(input("enter first number"))
b = int(input("enter second number"))
c = a+b
print(c)
def substraction():
a = int(input("enter first number"))
b = int(input("enter second number"))
c = a-b
print(c)

substraction()

Another example of a User define a function to calculate Simple Interest using Single File:-

def calculatesi():
p = float(input("enter value for amount"))
r = float(input("enter value for rate"))
t = float(input("enter value for time"))
si = (p*r*t)/100
print("result is ",si)

calculatesi()

### substraction()

Example of Addition, Subtraction, Multiplication, and Division-based program using six different functions for input, operation, and result.
def acceptInput():
global a,b
a=int(input("Enter first number"))
b=int(input("Enter second number"))
c=a+b
print(c)
def sub():
c=a-b
print(c)
def multi():
c=a*b
print(c)

def division():
c=a/b
print(c)
def executeFunction():
while(True):
ch=input("Press + for addition \nfor Substraction \n * for multiplication and \n / for division")
if ch=='+':
acceptInput()
elif ch=='-':
acceptInput()
sub()
elif ch=='*':
acceptInput()
multi()
elif ch=="/":
acceptInput()
division()
else:
break
executeFunction()

Same Program using Parametrised Function:-
c=a+b
print(c)
def sub(a,b):
c=a-b
print(c)
def multi(a,b):
c=a*b
print(c)
def division(a,b):
c=a/b
print(c)
def executeFunction():
while(True):

ch=input("Press + for addition \nfor Substraction \n * for multiplication and \n / for division")
if ch=='+':
elif ch=='-':
sub(int(input("Num1")),int(input("Num2")))
elif ch=='*':
multi(int(input("Num1")),int(input("Num2")))
elif ch=="/":
division(int(input("Num1")),int(input("Num2")))
else:
break
executeFunction()

Example of Addition, Subtraction, Multiplication, and Division using Two Separate files, the First file is used to write the code and the second file is used to access the code.
Save this file using Ope.py
def Add():    #Default Without Return Type
a=100
b=200
c=a+b
print(c)
def Sub():   #Default with Return Type
a=100
b=20
c=a-b
return c
def Multiplication(a,b):   #parametrised without return
c=a*b
print(c)
def Division(a,b):    #parametrised with return type
c = a/b
return c
Create Another File to   Access the function
import Ope   # include Addition.py file here
x=Ope.Sub()
print(x)
Ope.Multiplication(100,2)
y=Ope.Division(10,2)
print(y)
Type of function:-

### print(res)

1.2 Without Return Type:-

### Assignment of default function:-

1)  WAP to reverse a five-digit number using the no return type function.
2) WAP to perform addition of complex number using return type function
3)  WAP to create a salary calculator using no return type function
4)  WAP to calculate compound interest using no return type function.
2) Parametrised:-
The parameter is used to pass input value from calling function to called function, python provides a different type of parameters based on the requirement.

### using this type of function we will create a keyword for the argument which will contain the value of calling arguments to called the argument, name of the keyword argument should be the same as called argument.

Keyword means the name of the argument that will be used in the calling function.
def Functionname(x,y):

### Functionname(x=value1,y=value2)\

\
print(x/y)
2.4)  variable length arguments:-

### Syntax:-

var = lambda arg1,arg2:statement

### print(sum(10,20))

Q) WAP to find max element using variable-length arguments?

#wap to find max element using variable length arguments

def calcMax(*x):
m=0
for d in x:
if m<d:
m=d
else:
print(m)
calcMax(10,23,67,89,11,45)
Q)  How to pass dictionary object and List Object as a default function and keyword-based function in Python program?
def fun(x={'rno':0,'sname':'unknown'},m=[10,20,30]):
print(x,m)
student= {'rno':1001,'sname':'manish kumar'}
marks = [12,45,78,89]
#fun(x=student,m=marks)  #keyword
#fun(m=marks,x=student)
#fun(student,marks)
fun()
fun(student)
fun('',marks)
fun(x=student)
fun(m=marks)
fun(x=student,m=marks)
fun(m=marks,x=student)

Q) Create a Salary Calculator using user define function?
def SalaryCal():

### Execution File:-=

import SalaryCalc
num = int(input("Enter number of employee"))
for i in range(1,num+1):
SalaryCalc.SalaryCal()
Assignment of Parametrised function in Python:-
1) WAP to create a mark sheet program using function takes the dictionary as an input variable
2)  Create an ATM Program using a parametrized function?

Tags

1. #WAP to reverse a five-digit number using no return type function.
def reverse():#define a function
num=int(input("enter any 5-digit number"))
for i in range(0,5):
i=num%10
num=num//10
print(i)
reverse()#calling a function

2. #WAP to perform addition of complex number using return type function
def complex():
a=2+3j
b=4+5j

return a+b
res=complex()
print(res)

3. #WAP to create a salary calculator using no return type function
def salary():
name=input("enter name of employee")
basic=float(input("enter basic salary"))
da=float(10*basic)/100
hra=float(15*basic)/100
pf=float(5*basic)/100
ta=float(8.5*basic)/100
perday=float(basic/30)
netpay=float(basic+hra+ta+da-(2*perday))
grosspay=float(netpay-pf)
print(grosspay)
salary()

4. # WAP to calculate compound interest using no return type function.
def compoundinterest():
p=float(input("enter value of p"))
r=float(input("enter value of r"))
t=float(input("enter value of n"))
Amount = p * (pow((1 + r / 100), t))
CI = Amount - p
print("Compound interest is", CI)
compoundinterest()

5. #WAP to calculate Simple Interest where rate and time will be constant and
#the principal will be an integer? (by using function)
def simpleinterest():
p=int(input("enter principle amount"))
const=(2,4)
SI=p*const[0]*const[1]

print(SI)
simpleinterest()

6. #WAP to calculate the area of a triangle, circle, and rectangle in the same program where the value of pi, the base of the triangle, and the height of the rectangle will be constant.
def traingle():
H=int(input("enter value of H"))
const=(2,0)
area=(const[0]*H)/2
print(area)
def circle():
const=(3.14,0)
area=(const[0]*r*r)
print(area)
def rectangle():
B=int(input("enter value of base"))
const=(3,0)
area=(const[0]*B)
print(area)
traingle()
circle()
rectangle()

7. #WAP to convert temperature from Celsius to Fahrenheit?
def temp():
c=int(input("enter temprature in celsius"))
f=((9*c)+160)/5
print(f)
temp()

8. #WAP to swap two numbers without using a third variable by using function
def swap():
a=int(input("enter value of a"))
b=int(input("enter value of b"))
print("before swappine",a,b)
a,b=b,a
print("after swapping",a,b)
swap()

9. #WAP to calculate square and cube of any entered number(by using function)
def square():
a=int(input("enter value of a"))
sqr=a*a
print(sqr)
def cube():
b=int(input("enter value of b"))
cub=b*b*b
print(cub)

cube()
square()

10. sir this program not run properly is it right logic or not
# Create ATM Program using function ?

balance=float(input("enter balance"))

print(name,"welcome to our bank")
print("\n 1-debit,\n 2-credit,\n 3-viewbalance,\n 4-exit")
def debit():
print("enter amount that you want to withdraw",amount)
balance=balance-amount
def credit():
print("enter amount that you want deposit",amount)
balance=balance+amount
def showbalance():
print("total balance is",balance)

def ATM():
flag=False
PIN=1234

for i in range(1,4):
p=int(input("enter 4 digit pin number"))
if p==PIN:
flag=True
print(flag)
break
else:
print("pin is incorrect and number of attempt is attempted")
while(True):
print("\n 1-debit,\n 2-credit,\n 3-viewbalance,\n 4-exit")
if select==1:
print("name","amount to withdraw",debit())
if select==2:
print("name","amount to withdraw",credit())
if select==3:
print("name","amount to withdraw",showbalance())
if select==4:
print("name","amount to withdraw",exit())
called by
import atm
atm.ATM()
debit.atm()
credit.atm()
showbalance.atm()

11. #sir not run properly is it right logic for function
def marksheet(dict):
dict={"phy":60,"eng":40,"res":90,"hindi":40,"maths":50}
count=0
gm=0
s=""
dist=""
for i in dict:
if dict[i]>=0 and dict[i]<=100:
print(dict[i])
if dict[i]<33:
count=count+1
s=s+i+ " "
gm=dict[i]
else:
if dict[i]>=75:
dist = dist+ i + " "
else:
print("All subject marks should be 0 to 100")
break

total=total+marks[i]
else:
if count==0 or(count==1 and gm>=29):
per= total/5
if per>33 and per<45:
print("Third division with {0} %".format(per))
elif per<60:
print("Second division with {0} %".format(per))
else:
print("First division with {0} %".format(per))

if gm>0:
print("You are pass by grace and grace marks is ",str(33-gm), "grace subject is ",s)
if dist!=0:
print("Distinction subject name is ",dist)

elif count==1:
print("suppl in ",s)
else:
print("fail in ",s)

marksheet(dict)

12. #WAP to reverse a five-digit number using the no return type function.
#Ravi vyas
def reverse():
reverse=0
num=12345
while (num>0):
reminder=num%10
reverse=(reverse*10)+reminder
num=num//10
print("Reverse Number Is:-",reverse)
reverse()

13. #WAP to perform addition of complex number using return type function
#Ravi
def complex():
a =1+2j
b=3+4j
return a+b
print(res)

14. #WAP to reverse a five-digit number using no return type function.
#AKASH PATEL
def reverse():
rev=0
num=int(input("enter five digit number")
while (num>0):
rem=num%10
rev=(rev*10)+rem
num=num//10
print("Reverse Number Is:-",rev)
reverse()

15. # WAP to calculate compound interest using no return type function
#AKASH PATEL
def compoundinterest():
p=float(input("enter value of principal "))
r=float(input("enter value of rate"))
t=float(input("enter value of time"))
Amount = p * (pow((1 + r / 100), t))
CI = Amount - p
print("Compound interest is", CI)
compoundinterest()

16. #Create an ATM Program using a function?
balance=5000
pin=9171
def atm():
if psw==pin:
option=int(input())
if option==1:
elif option==2:
Debit=int(input("enter Amount"))
print("Available balance is",balance-Debit)
elif option==3:
elif option==4:
print("thanks for visit")
else:

atm()

17. def sub(x,y):
x=int(input("Enter The first value"))
y=int(input("Enter The second value"))
print("substraction is",(x-y))

sub(x=10,y=5)

18. WAP to reverse a five-digit number using the no return type function

def reverse():
i=input("enter number")
x=i[::-1]
print(x)
reverse()

19. WAP to perform addition of complex number using return type function

def complex():
x=(4+3j)
y=(9+1j)
return(x+y)
r=complex()
print(r)

20. WAP to create a salary calculator using no return type function

def salary():
s=int(input("enter basic salary"))
ta=int(input("enter tallowance"))
da=int(input("enter dallowance"))
pf=int(input("enter pf"))
g=int(input("enter gratuity"))
gs=s+ta+da-pf-g
print("Gross salary",gs)
salary()

21. WAP to calculate compound interest using no return type function

def compound():
p=int(input("enter principle"))
r=int(input("enter rate of interest"))
t=int(input("enter time period"))
i=(1+(r/100))**t
a=p*i
it=a-p
print("The interst is",it)
compound()

22. WAP to calculate compound interest using no return type function

def compound():
p=int(input("enter principle"))
r=int(input("enter rate of interest"))
t=int(input("enter time period"))
i=(1+(r/100))**t
a=p*i
it=a-p
print("The interst is",it)
compound()

23. def reverseno():
n=int("enter a number")
a=n%10
n=n//10
b=n%10
n=n//10
c=n%10
n=n//10
d=n%10
n=n//10
e=n%10
n=n//10
n2=a*10000+b*1000+c*100+d*10+e
print(n2)
reverseno()

a=complex(input("enter a 1st complex number"))
b=complex(input("enter a 2nd complex number"))
c=a+b
return c
print(res)

25. def sal():
s=int(input("enter monthly salary"))
d=s/30
l=int(input("enter leaves"))
ts=d*(30-l)
print(ts)
sal()

26. def calci():
p=int(input("enter principal"))
r=float(input("enter interest rate"))
n=int(input("enter time period"))
ta=(p*(1+r/100)**n)-p
print(ta)
calci()

27. def marksheet():
ms={}
ss=5
tm=0
for i in range(0,5):
sub=input("enter subject")
marks=int(input("enter obtained marks out of 100"))
ms.update({sub:marks})

tm=tm+marks
p=(tm/500)*100
if p>75:
elif p<=75 and p>60:
elif p<=60 and p>45:
elif p<=45 and p>33:
else:

marksheet()

28. def atm():
bal=13000
pin=4133
if psw==pin:
print("1.Balance\n2.Debit\n3.credit\n4 exit")
select=int(input())
if select==1:
elif select==2:
Debit=int(input("enter Amount"))
print("Available balance is",bal-Debit)
elif select==3:
Credit=int(input("enter Amount"))
print("Available balance is",Credit+bal)
elif select==4:
print("thanks for visit")
else:

atm()

29. to reverse a five-digit number using the no return type function

def rev():
i=input("enter number")
x=i[::-1]

print(x)
rev()

30. to perform addition of complex number using return type function

def complex():
x=(4+3j)
y=(9+1j)
return(x+y)
r=complex()
print(r)

31. # WAP to calculate compound interest using no return type function.
def comInte():
p=float(input("enter principal:-"))
r=float(input("enter interest:-"))
t=float(input("enter time:-"))
n=float(input("compounding frequency:-"))
r=r/100
i=p*((1+r/n)**(n*t))-p
print("compound interest is:-",i)
print("total amount is:-",i+p)
comInte()

32. # WAP to create a salary calculator using no return type function.
def salary():
sal=int(input("enter empolyee salary:="))
wd=int(input("enter wworking days:-"))
ot=int(input("enter over time in hours:-"))
pf=int(input("enter pf charges:-"))
can=int(input("enter canteen charges:-"))
if wd>23:
sal=sal/26*wd+2000

elif wd>20 and wd<=23:
sal=sal/26*wd+1500
else:
sal=sal/26*wd+0
ot=ot/8
a=ot*sal/26
total=sal+a-pf-can
print(total)
salary()

33. # WAP to perform addition of complex number using return type function.
a=2+2j
b=4+9j
return a+b
print(res)

34. #WAP to reverse a five-digit number using the no return type function.
def rev():
num=int(input("enter five digits numbers:-"))
a=num%10
num=num//10
b=num%10
num=num//10
c=num%10
num=num//10
d=num%10
num=num//10
e=num%10
num=num//10
print(a,b,c,d,e)
rev()

35. #WAP to reverse a five-digit number using the no return type function.

def revnum():
num=int(input("Enter number to reverse: "))
for i in range(0,5):
i=num%10
num=num//10
print(i)
revnum()

36. def credit(balance):
bal=balance
print("deposite balance is:-",bal)
def debit(balance,dbi):
bal=balance
d=dbi
print("withdrawal balance is:-",d)
print("available balance is:-",bal)
def checkBal(balance):
bal=balance
print("available amount is:-",bal)
def exit1():
exit()
def atm():
bal=0
for i in range(0,3):
if pin==1234:
ch=input(" 1.press c for credit \n 2.press d for debit \n 3.press b for check balance \n 4.press e for exit \n 5.press r for repeat \n")

if ch=='c':
if ac==123456789:
bal=bal+cb
credit(bal)
else:
print("wrong account number")

if ch=='d':
db=int(input("enter withdrawal amount:-"))
if bal>=db:
bal=bal-db
debit(bal,db)
else:
print("insufficient balance")
if ch=='e':
exit1()
if ch=='b':
checkBal(bal)

else:
print("you entered wrong pin ")

atm()

37. #WAP to reverse a five-digit number using the no return type function.
def reverse():
num=1234
for i in range(0,4):
a=num%10
num= num//10
print(a,end=" ")
reverse()

38. #WAP to print charecter A to Z using user difine function?
def funAZ(asc):
if asc>90:
return 1
print(chr(asc))

funAZ(asc+1)

funAZ(65)

39. #WAP to reverse a five-digit number using the no return type function without useing loop.

def REVpro():
num=int(input("Enter any Number "))
a=num%10
num=num//10
b=num%10
num=num//10
c=num%10
num=num//10
d=num%10
num=num//10
e=num%10
rev=a*10000+b*1000+c*100+d*10+e*1
return rev
n=REVpro()
print(n)

40. #WAP to perform addition of complex number using return type function
def ComplexNum():
t=2+9j
d=6+8j
com=t+d
return com
num=ComplexNum()
print(num)

41. #reverse 5 digit using no return
def reverse():
num=int(input("enter number "))
a=num%10
num=num//10
b=num%10
num=num//10
c=num%10
num=num//10
d=num%10
num=num//10
e=num%10
print(a*10000+b*1000+c*100+d*10+e*1)
reverse()

42. #WAP to perform addition of complex number using return type function

def complex():
a=3+4j
b=6+7j
return(a+b)
x=complex()
print(x)

43. #WAP to perform addition of complex number using no return type function

def complex():
a=3+4j
b=6+7j
c=a+b
print(c)
complex()

44. #factorial using return?
def fact():
a=1
n=int(input("enter number "))
for i in range(1,n+1):
a=a*i
return(a)
x=fact()
print(x)

45. # WAP to create a salary calculator using no return type function
def calSalary():
basic=10000
ta=1000
da=500
pf=300
hra=200
leave=3
sal=(basic+ta+da+pf+hra)
oneday=(sal/30)
dl=oneday*leave
totalsal=sal-dl
print(totalsal)
calSalary()

46. #WAP to calculate Campound Intrest using user difine function with return type?

def calCI():
p=int(input("Enter The Amount "))
r=int(input("Enter The Rate "))
t=int(input("Enter The Time"))
ci=(p*(1+r/100)**t)-p
return ci
s=calCI()
print("Campound Intrest %.2f"%s)

47. #WAP to reverse a five-digit number using the no return type function.

def rev():
n=int(input("Enter a no. to reverse:"))
for i in range(0,5):
i=n%10
n=n//10
print(i,end='')
rev()

48. #Program to add complex no. with return type

def comp():
a=complex(input("Enter a complex number"))
b=complex(input("Enter a complex number"))
return (a+b)
res=comp()
print(res)

49. #program to reverse a string

def rev_strng(s):
x=''
i=len(s)
while i > 0:
x=x+s[i-1]
i=i-1
return x
print(rev_strng("Nagpur"))

50. #WAP to find minimum element using variable-length arguments?

def swap(*a):
m=a[1]
for data in a:
if m>data:
m=data

print(m)

swap(12,32,44,3,55,66,10,5)

51. #Wap to swap two number using variable length?
def swap(*s):
print(s[1],s[0])

swap(100,99)

52. #WAP to find max element using variable-length arguments?
def maximum(*x):
print('maximum',max(x))
maximum(61,22,32,44,85,63,29,27)

def maximum(*x):#diffrent type
m=0
for data in x:
if m<data:
m=data
else:
print('maximum',m)
maximum(61,22,32,44,85,63,29,27)

53. WAP to reverse a five-digit number using the no return type function.

def reverse():
n=int(input("Enter a five digit number: "))
s=""
b=n
for i in range(0,5):
a=b%10
b=b//10
s=s+str(a)
print(s)

reverse()

54. WAP to perform addition of complex number using return type function

import cmath
def complexFun():
x=2+6j
y=3+4j
z=x+y
return z
a=complexFun()
print(a)

55. WAP to create a salary calculator using no return type function

def salaryCal(s,l,b):
t=s-(l*250)
if l==0:
t=t+b
print("You will get a bonus for 0 leaves")

salaryCal(int(input("salary: ")),int(input("leaves: ")),5000)

56. #complex number multiplication
a=10+1j
b=20+2j

c=a+b
print(c)
def subtraction(a,b):
c=a-b
print(c)
def multi(a,b):
c=a*b
print(c)
def division(a,b):
c=a//b
print(c)

multi(a,b)