Functions#
A function in Python is a reusable block of code that performs a specific task. It can be executed with different sets of arguments, allowing for code abstraction and modularity by encapsulating operations into a single, callable unit.
Parameters#
A named entity in a function (or method) definition that specifies an argument (or in some cases, arguments) that the function can accept.
Find out more:
Parameters page.
There are such types of parameters in python:
Name |
Description |
Syntax |
---|---|---|
Positional-only |
Parameters that can be specified only positionally. |
Parameters before the symbol |
Positional-or-keyword |
Can be specified positionally as well as through keyword arguments. |
Default parameters of the function |
Var-positional |
Parameter that takes values of all positional arguments that don’t correspond to the positional-only or positional-or-keyword parameters. |
Parameter name begins with the |
Keyword-only |
Can be specified only through keyword arguments. |
Parameters that follow the Var-positional argument or just |
Var-keyword |
Parameter that takes values of all keyword arguments that don’t correspond to the positional-or-keyword or keyword-only parameters. |
Parameter name begins with |
You can define default values for parameters just by using syntax <parameter>=<default value>
. But of you have defined default values for positional-only or for positional-or-keyword
parameter all following parameters of these groups should have default values.
The following cell shows the definition of a function that contains all types of parameters, with default values defined for some of them, arranged in a way that forms a valid Python expression.
def some_function(
positional_only1,
positional_only2,
/,
positional1,
positional2=None,
*args,
keyword_only1=None,
keyword_only2,
**kwargs
):
pass
Built in functions#
Python includes a set of built-in functions that are available directly in the interpreter without requiring any imports. This section provides an overview of these functions.
Sorted#
Function for sorting in python. Which takes an object and returns a new object with the elements sorted. You can pass any sortable object, such as a list, and receive the sorted result.
The following cell demonstrates how to sort a Python list.
sorted([8, 2, 3, 1, 4, 2])
[1, 2, 2, 3, 4, 8]
Key argument#
The key
argument allows you to specify a callable that, for each element of the original array, returns a value used to determine the order of the elements during sorting.
Consider the list [4, 5, 7, 12, 15]
. If we want to treat odd numbers as halves when sorting, the code to achieve this is shown below:
ans = [4, 5, 7, 12, 15]
sorted(ans, key=lambda x: (x/2 if x%2 else x))
[5, 7, 4, 15, 12]
Consider the following example: for each element of the original array, if the number is odd, it is transformed to half of its value; if it is even, it remains unchanged. For the list [4, 5, 7, 12, 15]
, this results in the pairs: 4 → 4
, 5 → 2.5
, 7 → 3.5
, 12 → 12
, and 15 → 7.5
. Sorting based on these transformed values gives the order: 5 → 2.5
, 7 → 3.5
, 4 → 4
, 15 → 7.5
, and 12 → 12
, which corresponds to the original list order of [5, 7, 4, 15, 12]
.
One useful example is sorting dictionary keys based on their corresponding values. By passing a function that retrieves the value for each key, you can obtain a list of keys sorted according to their values.
sort_dict = {
"mazda": 32,
"audi": 50,
"mercedes": 43,
"bmw": 38,
"toyota": 45,
"ford": 29
}
sorted(sort_dict, key=lambda key: sort_dict[key])
['ford', 'mazda', 'bmw', 'mercedes', 'toyota', 'audi']