Types:
Type of Functional Arguments
Default arguments in Python are the function arguments that will be used if no arguments are passed to the function call.
def printinfo( name, age = 35 ):
print ("Name: ", name)
print ("Age ", age)
return
printinfo( age=50, name="miki" )
printinfo( name="miki" )
Python allows to pass function arguments in the form of keywords which are also called named arguments.
def printinfo( name, age ):
print ("Name: ", name)
print ("Age ", age)
return
printinfo( "Sunil", 30 ) # by positional arguments
printinfo( name="miki", age = 50 ) # by Keyword arguments
Keyword-only parameters indicate that arguments can only be passed to function by keywords. You can create keyword-only parameters using * symbol. All the parameters that come after * are strictly keyword-only parameters.
Ex: def func(a, b, *, c, d=40):
# Keyword only Arguments
print("---position only Arguments---")
def my_position(a, b, *, c, d=40):
print('a =',a)
print('b = ',b)
print('c = ',c)
print('d = ',d)
my_position(10, 20, c=30 )
Positional-only parameters indicate that arguments can be passed to function only by position. You can create positional-only parameters using / symbol. All the parameters that come before / are strictly positional-only parameters.
Ex: def func(a, b, /, c, d=40):
# position only Arguments
print("---position only Arguments---")
def my_position(a, b, /, c, d=40):
print('a =',a)
print('b = ',b)
print('c = ',c)
print('d = ',d)
my_position(10, 20, c=30 )
# You cannot pass my_position(a=10, 20, c=30) - SyntaxError: positional argument follows keyword argument
def add(*args):
s=0
for x in args:
s=s+x
return s
result = add(10,20,30,40)
print (result)
result = add(1,2,3)
print (result)
In the below example, The addr() function has an argument **kwargs which is able to accept any number of address elements like name, city, phno, pin, etc. Inside the function kwargs dictionary of kw:value pairs is traversed using items() method.
def addr(**kwargs):
for k,v in kwargs.items():
print ("{}:{}".format(k,v))
print ("pass two keyword args")
addr(Name="John", City="Mumbai")
print ("pass four keyword args")
# pass four keyword args
addr(Name="Raam", City="Mumbai", ph_no="9123134567", PIN="400001")
Python also accepts function recursion, which means a defined function can call itself.
Example
import os
print('Hello '+os.getlogin() + '! Executing python script')
print("\n")
def greetings( MyName ):
print("Hello ",MyName)
print('ID After passing: ',id(MyName))
return
Name = 'Sunil'
print('ID Before passin: ',id(Name))
greetings(Name)
# Arbitrary Arguments, *args
print("---Arbitrary Arguments, *args---")
def kids(*name):
print("2nd kid name is " + name[1])
kids("Child 1", "Child 2", "Child 3")
# Keyword Arguments
print("---Keyword arguments---")
def child_name(child1, child2, child3):
print("Young child is", child3)
child_name(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
# Arbitrary Keyword Arguments, **kwargs
print("---Arbitrary Keyword Arguments, **kwargs---")
def my_name(**name):
print("My last name: ",name["lname"])
my_name(fname = "Sunil", lname = "Marella")
# Default Parameter Value
print("---Default Parameter Value---")
def my_country(country = "Netherlands"):
print("I am living in ", country)
my_country("India")
my_country()
# position only Arguments
print("---position only Arguments---")
def my_position(x, /):
print(x)
my_position(10)
print("my_position(x=10) - Type error")
# Keyword-Only Arguments
print("---Keyword only Arguments---")
def my_keyword(*, y):
print(y)
my_keyword(y=5)
print("my_keyword(5) - Type error")
# Combine Positional-Only and Keyword-Only
print("---Combine Positional-Only and Keyword-Only---")
def my_comb_keyword(a, b, /, *, c, d):
print(a + b + c + d)
my_comb_keyword(5, 6, c = 7, d = 8)
# Recursion
def tri_recursion(k):
if(k > 0):
result = k + tri_recursion(k - 1)
print(result)
else:
result = 0
return result
print("\n\nRecursion Example Results")
tri_recursion(6)
Result:
Hello sunil! Executing python script
ID Before passin: 139911559775536
Hello Sunil
ID After passing: 139911559775536
---Arbitrary Arguments, *args---
2nd kid name is Child 2
---Keyword arguments---
Young child is Linus
---Arbitrary Keyword Arguments, **kwargs---
My last name: Marella
---Default Parameter Value---
I am living in India
I am living in Netherlands
---position only Arguments---
10
my_position(x=10) - Type error
---Keyword only Arguments---
5
my_keyword(5) - Type error
---Combine Positional-Only and Keyword-Only---
26
Recursion Example Results
1
3
6
10
15
21
Main Page | Next Chapter: Python - Modules |