0% found this document useful (0 votes)
19 views222 pages

10 User Defined Function

Uploaded by

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

10 User Defined Function

Uploaded by

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

User defined

Functions
def fun( ):
print(“Hello Aditi”)

fun( ) Output
Hello
Aditi
function
Argument
def fun( ): function
definition
print(“Hello Aditi”)

fun( ) function Calling


Function without Argument
and no return type

def fun():
print("Welcome Teachers")

fun()
Order of execution

2 def fun( ):
3 print(“Hello Aditi”)

1 fun( )
def fun( ):
print(“Hello Aditi”)

fun( )
fun( ) Output
Hello Aditi
Hello Aditi
Order of execution

2 5 def fun( ):
3 6 print(“Hello Aditi”)

1 fun( )
4 fun( ) Output
Hello Aditi
Hello Aditi
Order of Execution
2. def fun1( ):
3. print("Hello ")

5. def fun2( ):
6. print("Aditi ")

1. fun1( ) Output

4. fun2( ) Hello
Aditi
Order of Execution
5. def fun1( ):
6. print("Hello ")

2. def fun2( ):
3. print("Aditi ")

1. fun2( ) Output

4. fun1( ) Aditi
Hello
Order of Execution
5. def fun2( ):
6. print("Hello ")

2. def fun1( ):
3. print("Aditi ")

1. fun1( ) Output

4. fun2( ) Aditi
Hello
def fun2( ):
print(“Bhalu ")
print("Ramu ")

def fun1( ):
print("Aditi ")
fun2( )
print("Ritu ")

fun1( )
print("Raju ")
fun1()
print("Kalu ")
Order of Execution
5 14 def fun2( ):
6 15 print(“Bhalu ")
7 16 print("Ramu ")

2 11 def fun1( ):
3 12 print("Aditi ")
4 13 fun2( )
8 17 print("Ritu ")

1 fun1( )
9 print("Raju ")
10 fun1()
18 print("Kalu ")
Function within function
def f1():
s = 'I love My India'
def f2():
print(s)
f2()

f1()
Function within function
def f1():
s = 'I love My India'
def f2():
print(s)
f2()

f2()
def fun():
print("Shambhavi")
def fun1():
print("Jha")

OUTPUT
Shambhavi
fun()
def fun():
print("Shambhavi")
def fun1():
print("Jha")
OUTPUT
Shambhavi
Error
fun()
fun1()
def fun():
print("Shambhavi")
fun1()
def fun1():
print("Jha")
OUTPUT
Shambhavi
Error
fun()
def fun():
print("Shambhavi")
def fun1():
print("Jha")
fun1()
OUTPUT
Shambhavi
Jha
fun()
Function with Argument and
no return type

def fun(x):
print(“Welcome Teachers”*x)

fun(5)
Passing Parameters to function
def fun1(a):
print(a)

fun1(10) Output
10
Passing Parameters to function
def fun1(a):
print(a)

b=10
fun1(b) Output
10
Passing Parameters to function
def fun1(a):
print(a)

b=int(input(“Enter a number”))
fun1(b) Output
depends on input
Passing Parameters to function
def fun1(a):
print(a)

b=input(“Enter name”)
fun1(b) Output
depends on input
Passing Parameters to function
def fun1(a):
print(a)

b=input(“Enter name”)
fun1(b) Output
depends on input
Passing Parameters to function
def fun1(a):
print(“ Hello ”, a)

b=input(“Enter name”)
fun1(b) Output
depends on input
Passing Parameters to function
def fun1(d,e):
print(d+e)

b=int(input("Enter a number "))


c=int(input("Enter another number "))
fun1(b,c)
Passing Parameters to function
def fun2(g):
print(g)

def fun1(d,e):
f=d+e
fun2(f)

b=int(input("Enter a number "))


c=int(input("Enter another number "))
fun1(b,c)
def b(j):
print("sum is ",j)

def a(f,g):
h=f+g
b(h)

def k():
c=int(input("enter number "))
d=int(input("enter number "))
a(c,d)

k()
Python Return

def ed(x):
print(x)
return

ed(12)
Function with Argument and
return type
def fun(x):
print(“Welcome Teachers”*x)
return x*x

y=fun(5)
print(y)
Function with Argument and
return type

def fun(x):
print(“Welcome Teachers”*x)
return x*x

print(fun(5))
Python Return

def ed(x):
print(x)
return
print(x*x)

ed(12)
Function Returning Nothing

def ed(x):
print(x)
return

y=ed(12)
print(y)
Returning values by the function

def fun2(g):
h=g*g
return h

b=int(input("Enter a number "))


c=fun2(b)
print(c)
Returning values by the function

def fun2():
b=int(input("Enter a number "))
h=b*b
return h

c=fun2()
print(c)
Returning values by the function

def fun2():
b=int(input("Enter a number "))
return b*b

c=fun2()
print(c)
Returning Integer
def b(c,d):
if c>d:
return c
else:
return d

a=b(10,20)
print(a)
Returning String
def b(c,d):
if c>d:
return "debanshi"
else:
return 'sampurna'

a=b(10,20)
print(a)
def y(z):
print("smahi")

for x in range(1,5):
y(x)
def y(a,b):
print(a,b,end=' ')

z=input("your name please ")


for x in range(1,5):
y(x,z)
def y(a):
for b in range(1,11):
print(a,'x',b,'=',a*b)

z=int(input("enter number
"))
y(z)
def y(m):
return m*m

x=int(input("enter number
"))
print("square of ",x,"is",y(x))
def y(m):
return m*m

def z(f):
return f*f*f

x=int(input("enter number
"))
print("square is",y(x),"cube
is",z(x))
def z(a,b):
c=a+b-b*b+14-a-29
return c

x=10
y=20
if z(x,y)>12:
print ("RITU")
else:
print("ADITI")
Feeling like c/c++ and java
def add(num1: int, num2: int) -> int:
num3 = num1 + num2
return num3

num1, num2 = 5, 15
ans = add(num1, num2)
print(f" {num1} + {num2} = {ans}.")
Function Returning 1 Value

def ed(x):
return x+x

y=ed(12)
print(y)
Function Returning 2 Values

def ed(x):
return x+x,x*x

y,z=ed(12)
print(y,“ ”,z)
Function Returning more
than 1 Values

def ed(x):
return x+x,x*x

y=ed(12)
print(y)
def z(a,b):
e=a+b-24
f=24+a-b
return e,f

x=10
y=20
c,d=z(x,y)
print(c,d)
def z(a,b):
e=a+b-24
f=24+a-b
return e,f

x=10
y=20
c=z(x,y)
print(c)
def z(a,b):
e=a+b-24
f=24+a-b
return e,f

x=10
y=20
c=z(x,y)
print(c[0]+c[1])
def z(a,b):
e=a+b
f=a-b
g=a*b
h=a/b
i=a//b
j=a%b
return e,f,g,h,i,j

x=10
y=20
c=z(x,y)
for k in c:
print (k)
def z(a+3,b):
print
(a*23+b-32/a+b)

x=20
y=10
z(x,y)
def z(a,'b'):
print ("hello",b,a)

x=20
y='aditi'
z(x,y)
def x(y,z):
print(y+z)

x(10,20,30)
def x(y,z,a,b):
print(y+z+a*b)

x(10,20,30)
def x(y,z,a,b):
a=4
b=3
print(y+z+a*b)

x(10,20,30)
def x(y="RITU"):
print("hello ",y)

z="ADITI"
x()
x(z)
def x(y="RITU"):
print("hello "+y)

z="ADITI"
x()
x(z)
def x(y=100):
print("hello "+y)

z=50
x()
x(z)
def x(y=100):
print("hello ",y)

z=50
x()
x(z)
def x(y="RITU"):
print(y*5)

z="ADITI"
x()
x(z)
def x(a,y="RITU "):
print(y*a)

z="ADITI "
x(4)
x(3,z)
def x(a,r):
print("hello
",a,"and",r)

a="ritu"
r="aditi"
x(a,r)
def x(a,r):
print("hello
",a,"and",r)

a="ritu"
r="aditi"
x(r,a)
Default Arguments
def myFun(x, y=50):
print("x: ", x)
print("y: ", y)

myFun(10)
myFun(10,20)
Default Arguments
def myFun(x=25, y=50):
print("x: ", x)
print("y: ", y)

myFun(10)
myFun(10,20)
myFun()
Default Arguments
def myFun(x=25, y):
print("x: ", x)
print("y: ", y)

myFun(10)
myFun(10,20)
myFun()
Default Arguments
def myFun(x=25, y): ERROR
print("x: ", x)
print("y: ", y)

myFun(10)
myFun(10,20)
myFun()
Keyword Arguments

def student(F, L):


print(F, L)

student(F=‘Sonu’, L=‘Monu’)
student(L=‘Hira’, F=‘Moti’)
def x(a,r):
print("hello
",a,"and",r)

x(r="Aditi",a="ritu")
def
x(s,a="Aditi",r="ritu"):
print("hello ",a,"and",r,"or",s)

s="sampurna"
x(s)
def
x(s,a="Aditi",r="ritu"):
print("hello ",a,"and",r,"or",s)

s="sampurna"
x(s,a="akanksha")
def
x(s,a="Aditi",r="ritu"):
print("hello
",a,"and",r,"or",s)

s="sampurna"
x(s,a="akanksha",r="tanvi")
def
x(s,a="Aditi",r="ritu"):
print("hello
",a,"and",r,"or",s)

s="sampurna"
x(s,r="akanksha",a="tanvi")
def
x(s,a="Aditi",r="ritu"):
print("hello
",a,"and",r,"or",s)

s="sampurna"
x(r="akanksha",s,a="tanvi")
def
x(s,a="Aditi",r="ritu"):
print("hello
",a,"and",r,"or",s)

s="sampurna"
x(r="akanksha",a="tanvi",s)
def
x(s,a="Aditi",r="ritu"):
print("hello ",a,"and",r,"or",s)

s="sampurna"
x(s,r="akanksha")
def
x(s,a="Aditi",r="ritu"):
print("hello ",a,"and",r,"or",s)

t="sampurna"
x(t,r="akanksha")
def
x(s,a="Aditi",r="ritu"):
print("hello
",a,"and",r,"or",s)

x(r="akanksha",s="sampur
na")
def x(*s):
print (s)

x(5)
def x(*s):
print (s)

x(5,6)
Variable Length Arguments

def myFun(*ar):
print(type(ar))

myFun(‘Sonu’, ‘Monu, ‘Hira’, ‘Moti’)


Variable Length Arguments

def myFun(*ar):
for arg in ar:
print(arg)

myFun(‘Sonu’, ‘Monu, ‘Hira’, ‘Moti’)


def x(*s):
print (s)

x()
def x(*s):
for y in s:
print (y)

x(5)
def x(*s):
for y in s:
print (y)

x(5,8)
def x(*s):
for y in s:
print (y)

x(5,10,15)
def x(*s):
for y in s:
print (y)

x(‘amit’, ‘sumit’)
def x(*s):
for y in s:
print (y)

x(‘amit’, 45)
def x(*s):
for y in s:
print (y)

x([‘amit’, 45],[56,
‘sumit’])
def x(*s):
for y in s:
print (y)

x((‘amit’, 45),(56,
‘sumit’))
def x(*s):
for y in s:
print (y)

x((‘amit’, 45),[56,
‘sumit’])
def x(*s):
for y in s:
print (y)

x((‘amit’, 45),56,
‘sumit’)
def x(*s):
print (s)

x((‘amit’, 45),56,
‘sumit’)
def x(*s):
print (s[0])

x((‘amit’, 45),56,
‘sumit’)
def x(*s):
print (s[0],s[1])

x((‘amit’, 45),56,
‘sumit’)
def x(*s):
How to print amit

x((‘amit’, 45),56,
‘sumit’)
def x(*s):
print(s[0][0])

x((‘amit’, 45),56,
‘sumit’)
Variable Length Keyword
Arguments

def myFun(**kar):
print(type(kar))
myFun(a='ZoneA', b='ZoneB', d='ZoneD')
Variable Length Keyword
Arguments

def myFun(**kar):
for key, value in kar.items():
print(key, value)

myFun(a='ZoneA', b='ZoneB', d='ZoneD')


Printing Comments
def ed(x):
"""Ram Nam"""
print("Ram")
"""Satya Hai"""
print("Shayam")

print(ed.__doc__)
Printing Comments

def ed(x):
print("Ram")
"""Satya Hai"""
print("Shayam")

print(ed.__doc__)
GLOBA
L
Vs
LOCAL
x=1
def y():
print(x)
x=1
def y():
print(x)

y()
x=1
def y():
print(x)

y()
print(x)
def y():
x=1
print(x)

y()
print(x)
def y():
x=1
print(x)

y()
print(x) ERROR
def f1():
x=15
print(x) 15
x=12
f1()
def f1():
x=100
print(x) 100

x=+1
f1()
def san(x):
print(x+1)
13

x=-2
x=4
san(12)
def myfunc():
x = 300
def myinnerfunc():
print(x)
myinnerfunc()

myfunc()
x=1 GLOBAL
def y():
print(x)
z=2 LOCAL
y()
print(z)
ERROR
def y():
z=1
print(x)
OKAY
print(z)
OKAY

y()
print(x) OKAY
print(z) ERROR
x=1 GLOBAL
def y():
x=2 LOCAL
print(x)

y() OUTPUT
print(x) 2
x=3 1
y() 2
print(x) 3
x=1 GLOBAL
def y():
x=2 LOCAL
print(x)
x=4 LOCAL
y() OUTPUT
print(x) 2
x=3 1
y() 2
print(x) 3
x=1
def y():
print(x)
x=4 Error trying to
change global x
y()
print(x)
x=3
y()
print(x)
x=1
def y():
x=4 OK creating
local x
print(x)

y()
print(x)
x=3
y()
print(x)
x=1
def y():
x=x+1 ERROR due to
x in Red
print(x)

y()
print(x)
x=3
y()
print(x)
x=12
def f1(a,b=x):
print(a,b)
4 12
x=15
f1(4)
12
x=15 15
12
f1(4) 10
15
f1(x)
f1(10,x)
f1(a,b=x):
print(a,b)
x=15
f1(4)
Error
def f1():
global x
x+=1
print(x)
x=12
print(“x”)
def f1():
global x
x+=1
print(x)
x=12
f1()
print(“x”)
def f1():
global x
x+=1
print(x)
x=12
f1()
print(x)
ERROR
x+=1 use of local

and global

print(x) both in same


fun. not

allowed

f1(15)
print("hello")
def f1(x):
ERROR
global x
x+=1
print(x)
x=10
f1(15)
print("hello")
global x
x+=1
print(x)
11
x=10
hello
f1(15)
print("hello")
global x
x+=1+z
print(x)
26
x=10
hello
f1(15)
print("hello")
print(a)
world
a = "hello"
print(a)
hello
a = "world"
f()
print(a)
hello
def f(p, q, r):
global s
p = 10
q = 20
r = 30
s = 40
print(p,q,r,s)
p,q,r,s = 1,2,3,4
f(5,10,15)
def f(p, q, r):
global s
p = 10
q = 20
r = 30
s = 40
print(p,q,r,s)
p,q,r,s = 1,2,3,4
f(5,10,15)
print(p,q,r,s)
a=1 print(a)
def f(): f()
print(a) print(a)
g()
def g(): print(a)
a=2 h()
print(a) print(a)

def h():
global a
a=3
print(a)
g()
def g(): print(a)
a=2 h()
1
print(a) print(a)
1
1
def h(): 2
global a 1
a=3
3
print(a) 3
Passing
LIST/TUPL
E
def x(a):
print(a)

b=[10,20,30,40]
x(b)
Passing LIST to a function

def ed(x):
for z in x:
print(z)

y=[1,2,3,4,5]
ed(y)
def x(a):
print(a)

b=(10,20,30,40)
x(b)
def x(a):
for c in a:
print(c)

b=[10,20,30,40]
x(b)
def x(a):
d=0
for c in a:
d=d+c
print(c)

b=[10,20,30,40]
x(b)
def x(a):
d=0
for c in a:
d=d+c
print(d)

b=[10,20,30,40]
x(b)
def x(a):
d=0
for c in a:
d=d+c
print(d+c)

b=[10,20,30,40]
x(b)
Pass
By
VALUE
def x(y):
print(y)
y=30
print(y)

y=20
x(y)
print(y)
Call By Value
def myFun(x):
print(x)
x = 20
print(x)
x = 10
myFun(x)
print(x)
Call By Value
def myFun(x):
x = [20, 30, 40]

lst = [10, 11, 12, 13, 14,


15]
myFun(lst)
print(lst)
Pass By
REFERENCE
Call By Reference
def myFun(x):
print(x)
x[0] = 20

lst = [10, 11, 12, 13, 14,


15]
myFun(lst)
print(lst)
Call By Reference
def myFun(x):
y=x
x[0] = 20
y[0] = x[0] + y[0]

lst = [10, 11, 12, 13, 14, 15]


myFun(lst)
print(lst)
def x(a):
print(a)
a[0]=a[1]+a[2]
a[1]=a[0]+a[2]
a[2]=a[0]+a[1]
print(a)
PASS BY REFERENCE
b=[10,20,30]
print(b)
x(b)
print (b)
def x(a):
print(a)
a[0]=a[1]+a[2] ERROR
a[1]=a[0]+a[2]
a[2]=a[0]+a[1]
print(a)

b=(10,20,30)
print(b)
x(b)
print (b)
def set_list(list):
list = ["A", "B", "C"]
return list

def add(list):
list.append("D")
return list

my_list = ["E"]
print(set_list(my_list))
print(add(my_list))
x=[10,20,30]
def func(xx):
xx[0]=40
return 1

x[func(x)]=21
print(x)
Function with statement(s)

def f1():
s = 'I love My India'
print(s)

f1()
Function without statement

def f1():

f1()
Function without statement

def f1():
pass

f1()
x="Ritu and Aditi is the best
friend"
y=x.split()
print (y)
x="Ritu and Aditi is the best
friend"
y=x.split(" ")
print (y)
x="Ritu,and,Aditi,is,the,best,frie
nd"
y=x.split(",")
print (y)
x="Ritu@and@Aditi@is@the@best@f
riend"
y=x.split("@")
print (y)
x="Ritu@and@Aditi@is@the@best@f
riend"
y=x.split("#")
print (y)
x="Ritu and aditi is the best friend"
y=x.split("a")
print (y)
def x(y):
z.append(45)
print(z)

z=input("enter comma separated numbers


")
print(z)
z=z.split(",")
print(z)
for t in range(len(z)):
z[t]=int(z[t])
print(z)
x(z)
print (z)
def x(y):
for z in range(len(y)):
if z%2==0:
y[z]+=2
else:
y[z]-=1
print (y)

a=[9,8,3,5,3,7,2,5,5,6]
print(a)
x(a)
print(a)
def x(y):
for z in range(len(y)):
if y[z]%2==0:
y[z]+=2
else:
y[z]-=1
print (y)

a=[9,8,3,5,3,7,2,5,5,6]
print(a)
x(a)
print(a)
def x(y):
for z in range(len(y)):
if y[z]%2!=0:
y[z]=y[z+1]
else:
y[z]=y[z]+1
print (y)

a=[9,8,3,5,3,7,2,5,5,6]
print(a)
x(a)
print(a)
def x(y):
for z in range(len(y)):
if y[z]%2!=0:
y.append(23)
else:
y.clear()
print (y)

a=[9,8,3,5,3,7,2,5,5,6]
print(a)
x(a)
print(a)
def x(y):
for z in range(len(y)):
if y[z]%2!=0:
y.append(22)
else:
y.clear()
print (y)

a=[9,8,3,5,3,7,2,5,5,6]
print(a)
x(a)
print(a)
def x(y):
for z in range(len(y)):
if y[z]%2!=0:
y.append(22)
print (y, len(a))

a=[9,8,3,5,3,7,2,5,5,6]
print(a,len(a))
x(a)
print(a,len(a))
def x(y):
for z in range(len(y)):
if y[z]%2!=0:
y.append(23)
print (y)

a=[9,8,3,5,3,7,2,5,5,6]
print(a)
x(a)
print(a)
x=3
for y in range(x):
print(y)
x=x+1
x=3
for y in range(x):
print(y)
x=x+1
print(x)
x=3
for y in range(x):
print(y)
x=x+1
print(x)
print(x)
x=3
for y in range(x):
print(y)
x=x+1
print(x)
print(x)
print(y)
x=3
for y in range(x):
print(y)
x=x+1
print(x)
y=100
print(x)
print(y)
x=3
for y in range(x):
print(y)
x=x+1
print(x)
y=y+1
print(x)
print(y)
def x():
print("Debanshi ")
x()

x()
Recursion

def f1():
print(“Ram”)
f1()

f1()
Recursion
def t(k):
if(k > 0):
result = k + t(k - 1)
print(result)
else:
result = 0
return result

t(6)
def x():
print("Debanshi ")
x()

print("SAMPURNA ")
x()
def x():
print("Debanshi ")
y()

def y():
print("Smahi ")
x()

print("SAMPURNA ")
x()
def x(z):
if z==10:
print("Ritu ")
else:
x(z-1)

y=10
x(y)
def x(z):
if z==1:
return z
else:
x(z-1)

return z

y=4
print(x(y))
def x(z):
if z==1:
return z
else:
x(z-1)

return z

y=4
x(y)
def x(z):
if z==1:
return z
else:
print(x(z-1))

return z

y=4
print(x(y))
small anonymous
function.

A lambda function can


take any number of
arguments, but can
only have one
expression.
Unnamed Function
def t(k): Here name
of the function is t and is
defined using the keyword
def.
A function can also be
defined without the
keyword def
Unnamed Function
This type of
functions are known
as unnamed
function, and best
example for this will
be “lambda function”
print((lambda a : a +
10)(5))
OUTPUT 15

Calling a unknown
function with one
argument 5 inside print
function
lambda function
with 1 argument

x = lambda a : a + 10
print(x(5))
lambda function
with more than 1
argument

x = lambda a, b : a * b
print(x(5, 6))
lambda function
def myfunc(n):
return lambda a : a *
n

mydoubler = myfunc(2)
print(mydoubler(11))
lambda function
def myfunc(n):
return lambda a : a * n

mydoubler = myfunc(2)
mytripler = myfunc(3)
print(mydoubler(11))
print(mytripler(11))
Yield and Return

Yield is generally used to convert a


regular Python function into a
generator.

Return is generally used for the end of


the execution and “returns” the result to
the caller statement.
Difference between yield and
return
def printresult(String) :
for i in String:
if i == “i":
return i

String = “i love my india"


ans = 0
String = String.strip()
for j in printresult(String):
ans = ans + 1
print (ans)
Difference between yield and
return
def printresult(String) :
for i in String:
if i == “i":
yield i

String = “i love my india"


ans = 0
String = String.strip()
for j in printresult(String):
ans = ans + 1
print (ans)
Yield and Return

Code written after yield


statement execute in next
function call. while, code
written after return
statement wont execute.
def printresult(String) :
for i in String:
if i == "i":
yield i
print("Ram")

String = "i love my india"


ans = 0
String = String.strip()
for j in printresult(String):
ans = ans + 1
print (ans)
def printresult(String) :
for i in String:
if i == "i":
return i
print("Ram")

String = "i love my india"


ans = 0
String = String.strip()
for j in printresult(String):
ans = ans + 1
print (ans)
Function
Overloadi
ng
Method Overloading:
Two or more methods have the same name but
different numbers of parameters or different
types of parameters, or both. These methods
are called overloaded methods and this is
called method overloading.
Like other languages (for example,
method overloading in C++) do, python does
not support method overloading.
The problem with method overloading in
Python is that we may overload the
methods but can only use the latest
defined method
def a():
print(“No
Problem ”)

a()
No
Problem
def a():
print("Problem ")

def a():
print("No
Problem ")

a()
No
Problem
def a():
print("Problem ")

def a():
print("No Problem ")

a(10)
Error
def a(b):
print("Problem ")

def a():
print("No Problem ")

a(10)
Error
def a():
print("Problem ")

def a(b):
print("No Problem ")

a(10)
No
Problem
def a(n):
print("Problem ")

def a(b):
print("No Problem ")

a(10)
No
Problem
def a():
print("Problem ")

def a(b):
print("No Problem ")

a(10)
a()
No
Problem
Error
def a():
print("Problem ")

def a(b):
print("No Problem ")

a()
a(10)
Error
Thus, to overcome the
above problem we can use
different ways to achieve
the method overloading.
We can use the arguments
to make the same
function work differently
i.e. as per the arguments.
def add(dt, *args):
if dt == 'int':
answer = 0
if dt == 'str':
answer = ''
for x in args:
answer = answer + x
print(answer)

add('int', 5, 6)
add('str', 'Hi ', 'Dear')
We can achieve method overloading in python by user
defined function using “None” keyword as default
parameter.
Code explanation:
The first parameter of “add” method is set to None. This
will give us the option to call it with or without a
parameter.
When we pass arguments to the add method (Working):
The method checks if both the parameters are available
or not.
As we have already given default parameter values as
“None”, if any of the value is not passed it will remain
“None”.
Using If-Else statements, we can achieve method
overloading by checking each parameter as single
statement.
def add(a=None, b=None):
if a != None and b ==
None:
print(a)
else:
print(a+b)

add(2, 3)
add(2)

You might also like