Functions in Python
What are Functions in python and how does it work?
A function is a block of code that only runs when it is called.
You can pass data, known as parameters, into a function.
A function can return data as a result.
Creating and Calling a Function
In Python a function is defined using the def keyword:
while Calling will be as below :
Call a function, use the function name followed by parenthesis:
Arguments
Information can be passed into functions as arguments.
Arguments are often shortened to args in Python documentations.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
The following example has a function with one argument (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:
Parameters or Arguments?
The terms parameter and argument can be used for the same thing: information that is passed into a function.
From a function's perspective:
A parameter is the variable listed inside the parentheses in the function definition.
An argument is a value that is sent to the function when it is called.
Arbitrary Arguments, *args
Arbitrary Arguments are often shortened to *args in Python documentations.
If you do not know how many arguments will be passed into your function, add a * before the parameter name in the function definition.
This way the function will receive a tuple of arguments, and can access the items accordingly:
Example
If the number of arguments is unknown, add a * before the parameter name:
Arbitrary Keyword Arguments, **kwargs
Arbitrary Kword Arguments are often shortened to **kwargs in Python documentations.
If you do not know how many keyword arguments will be passed into your function, add two asterisk: ** before the parameter name in the function definition.
This way the function will receive a dictionary of arguments, and can access the items accordingly:
Example
If the number of keyword arguments is unknown, add a double ** before the parameter name:
Return Values
To let a function return a value, use the return statement:
Example
Lists in Python
Lists are used to store multiple items in a single variable.
Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
Lists are created using square brackets:
Example
Create a List:
List Items
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0], the second item has index [1] etc.
Allow Duplicates
Since lists are indexed, lists can have items with the same value:
Example Lists allow duplicate values:
Python Collections (Arrays)
There are four collection data types in the Python programming language:
List is a collection that is ordered and changeable. Allows duplicate members.
Tuple is a collection that is ordered and unchangeable. Allows duplicate members.
Set is a collection that is unordered and unindexed. No duplicate members.
Dictionary is a collection that is ordered* and changeable. No duplicate members.
When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.
Passing a List as an Argument
You can send any data type of argument to a function (string, number, list, dictionary, etc.), and it will be treated as the same data type inside the function.
E.g. if you send a list as an argument, it will still be a List when it reaches the function:
Example:
REF: Geeks for Geeks