Difference between positional arguments and keyword arguments

(Comments)

Difference between positional arguments and keyword arguments.

If you consider the below example.

def fn (a, b, c = 1): # a/b required, c optional.
return a * b + c

Now, the above function can be called in the below ways.

print fn (1, 2) # returns 3,

here arguments(1,2) are positional and we did not give the third argument, so it is defaulted to value 1. print fn (1, 2, 3) # returns 5, here the arguments(1,2,3) are positional.

print fn (c = 5, b = 2, a = 2) # returns 9,

here the arguments(c = 5, b = 2, a = 2) are keyword arguments or named arguments.

print fn (b = 2, a = 2) # returns 5,

here the arguments(b = 2, a = 2) are keyword or named arugments and c is defaulted to value 1 .

print fn (5, c = 2, b = 1) # returns 7,

here 5 is positional argument and c = 2, b = 1 are named or keyword arguments.

print fn (8, b = 0) # returns 1,

here 8 is positional, b=0 is named and c is defaulted to value 1.

Comments

Recent Posts

Archive

2022
2021
2020
2019
2018
2017
2016
2015
2014

Tags

Authors

Feeds

RSS / Atom