Advertisement
# Python - Function with default arguments
# Defining a function with two default arguments
def add(a =10, b = 20):
print("result : ", a+b)
# Calling the add() function without any arguments
add()
# Calling the add() function with only one argument
add(30.5)
# Calling the add() function with two arguments
add(30,40)
result : 30
result : 50.5
result : 70
# Python - Function with default arguments
# Defining a function with two default arguments
def display(a = 10, b = 24.99):
print('a : ', a)
print('b : ', b)
print("Calling add() function without any arguments :")
display()
print("Calling add() function with one arguments :")
display(20)
print("Calling add() function with two arguments :")
display(20,30)
print("Calling add() function with two arguments :")
display(20,30,40)
print("Calling add() function without any arguments :")
a : 10
b : 24.99
print("Calling add() function with one arguments :")
a : 20
b : 24.99
print("Calling add() function with two arguments :")
a : 20
b : 30
Traceback (most recent call last):
File "D:/Python Programs/default2.py", line 23, in <module>
display(20,30,40)
TypeError: display() takes from 0 to 2 positional arguments but 3 were given
Advertisement
# Python - Function with mixing non-default and default arguments
# Defining a function with a non-default(x) and a default argument(s)
def display(x, s = 'Hi'):
print('x : ', x)
print('s : ', s)
print('Calling the show() function with only one argument')
show(20)
print('Calling the show() function with two arguments')
show(55.9, 'Hello')
Calling the show() function with only one argument
x : 20
s : Hi
Calling the show() function with two arguments
x : 55.9
s : Hello
# Python - Function with mixing non-default and default arguments
# Defining a function with a non-default(x) and a default argument(s)
def display(s = 'Hi',x):
print('x : ', x)
print('s : ', s)
print('Calling the show() function with only one argument')
show('Hello', 20)
print('Calling the show() function with two arguments')
show('Hello', 50)
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement