Zero to Hero in Python in 30 days: Day 10: Functions Part II

Manthan B Y
Python in Plain English
4 min readApr 13, 2020

--

Before reading this article, please ensure that you have read all my previous articles from the series Zero to Hero in Python in 30 days: https://medium.com/python-in-plain-english/zero-to-hero-in-python-in-30-days-day-9-functions-part-i-c68ed0569f2d

Everyone knows that any scripting language shootout that doesn’t show Python as the best language is faulty by design. — Max M

In the previous article, we have discussed the basic introduction of functions. In this article, let us dive deeper into the concepts of functions in python.

Arguments we pass based on the position of the parameters in the function definition are called Positional Arguments. In the previous article what we passed to the function are nothing but, positional arguments.

Keyword Arguments

Functions can also be called by passing the name of the arguments. These are called keyword arguments. Keyword arguments should always follow non-keyword arguments. Matching always from left to right. Non-keyword arguments are also called positional arguments, as the name suggests based on the position they are matched.

In the above program, I am directly calling the function using the parameter name itself. Above call can also be done like add(2, b = 1) (keyword should always follow non-keyword)

Default Arguments

Python functions can also have default arguments i.e, we can have an optional argument which can or cannot be passed, if not passed the default given during the function definition is taken. During the definition of the function, default parameters(arguments) should always follow non-default parameters.

In the above program, in the function greet, msg is a default argument. In line 4, only one positional argument “Manthan” is passed. In line 5, two positional arguments are passed. In line 6, one is a positional argument, another is keyword argument. In line 7, both are keyword arguments.

Arbitrary Arguments(*args)

What if we have a function for which we do not know how many arguments are passed by the client. Python allows us to handle these kinds of the situation very easily. We have to just add *(asterisk) before the parameter name to make our function accept any number of arguments we wish to. Normally we use args as parameter name to capture the arbitrary number of arguments. But it is not mandatory, we can use any name we want.

Let us write a function and pass them arbitrary arguments and introspect the parameter,

You can observe that all the passed arguments are captured inside a parameter which is a tuple. Tuples are the concept of another upcoming article. Let’s just think like all the passed arguments are in one place under one name. Let’s now write a function to add those arbitrary integers.

In the above program observe that we are using the for loop to iterate through the items passed to the function. This is another variation of for loop, where you can iterate through the items in a container(e.g, tuple as seen above), i is a loop variable, it holds the value of the item being iterated.

We can also have a combination of positional, keyword and an arbitrary number of arguments and the next one we cover too.

**kwargs

Let us see what happens when we pass a non-existing parameter name as a keyword argument

We get an error which says unexpected keyword arguments.

To avoid this and to capture the extra keyword arguments we use **(double-asterisk) before the parameter name. Normally to hold these extra keyword arguments we use kwargs as parameter name, but this is not mandatory, we can have our own name for the parameter.

Let’s see this in action,

Observe all the extra keyword arguments are captured in kwargs. If we examine the type of kwargs using type(), we get <class ‘dict’>, it is a dictionary. We will learn more about dictionaries and how to use them in upcoming articles.

This completes my eleventh article in the series Zero to Hero in Python in 30 days. Hope you enjoyed reading this article. If you have any further doubts please drop them in the response section. Also, you can feel free to contact me.

Happy Coding…

Update:
Links for all the articles:
https://drive.google.com/file/d/1eUXaMy064uCEg3z3F_oL_gF-OmBkhVIl/view?usp=sharing

--

--