Functional Arguments In Python

Python default arguments: Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value. Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the form keywordname=value.

Let’s understand this through a function student. The function student contains 3-arguments out of which 2 arguments are assigned with default values.

def student(firstname, lastname ='Mark', standard ='Fifth'):
	print(firstname, lastname, 'studies in', standard, 'Standard')

Python keyword argument: In Python, the terms parameter and argument are used interchangeably. However, there is a slight distinction between these two terms. Parameters are the input variables bounded by parentheses when defining a function, whereas arguments are the values assigned to these parameters when passed into a function (or method) during a function call. Keyword arguments (or named arguments) are values that, when passed into a function, are identifiable by specific parameter names. A keyword argument is preceded by a parameter and the assignment operator, = .

Keyword arguments can be likened to dictionaries in that they map a value to a keyword.

def team(name, project):
    print(name, "is working on an", project)

team(project = "CodeRed", name = 'John')

Python arbitrary argument

Python allows us to do this through certain special syntaxes that are collectively known as arbitrary arguments (or variable-length arguments). Here, unlike with fixed arguments, parameters are not specified by distinct individual names, but rather a general term to better encapsulate the shared attribute of the type of arguments being passed into the function. These syntaxes are of the forms:

*args: for non-keyworded/positional arguments

**kwargs: for keyworded arguments.

These labels, *args and **kwargs, represent the iterables that would be accessed during a function call.

A single asterisk signifies elements of a tuple, whereas double asterisks signify elements of a dictionary.

def newf(*kids):
    print("The cutest child is " + kids[2])
newf("Arun", "Varun", "Timothy", "Aaron")  # using a function with *args i.e variable length argument
def my_function(**kid):
    print("His last name is " + kid["lname"])
my_function(fname = "Timothy", lname = "Refsnes", age=18)  # using a function with **kwargs

Python Positional arguments

Positional arguments are values that are passed into a function based on the order in which the parameters were listed during the function definition. Here, the order is especially important as values passed into these functions are assigned to corresponding parameters based on their position.

def complex(i, j):
    print(i, "+", j, "j")
complex(1, 3)

Looking for the perfect medium way to polish your python programming skills? Codevita Live’s Bon-Voyage series got you covered!

Never stop learning,because life never stops teaching!