Functions in Python
To define functions in Python we have the instruction def
After this reserved word goes the function name along with a list of arguments delimited by parentheses and separated by commas, ending with the symbol :
Defining a function does not execute the body of the function
It will only be executed when the function is called
Its execution binds the function name with the current local namespace to an objecto function (a wrapper around the executable code for the function)
This object function contains a reference to the global local namespace as the global namespace to be used when the function is called
Optionally you can add an expression that will be returned by the function
The expression can be a valid Python type preceded by the statement return
The DOCSTRING is a descriptive text of the function enclosed in three double quotes of opening and closing, before the first statement
It's optional, so if we don't want to add a description, we can omit it
But it is recommended to use it so that when we review after a while our code, we remember what our function did
Arguments
The argument step is optional and in some cases there may be functions that lack them
Type of arguments
Supports types str, int, float, complex,bool, list, tuple, dic and class
Arguments with value by position
When we send arguments to a function, they are received in order in the defined parameters
Value arguments by name
It is possible to evade the order of the parameters if we indicate during the call the value that each parameter has from its name
No arguments
When calling a function which has a few parameters defined, if you do not pass the arguments correctly it will cause an exception TypeError
Default value arguments
Sometimes we need an argument to have a value, to avoid an error (which would cause the exception to be thrown TypeError), even if the user of the function does not enter it
Note that default arguments become optional and that all arguments to the left of the first default value are required
The example used the null value (None) to express that no arguments had been passed
But we could use any type we need as the default
Indeterminate arguments
Sometimes we won't know in advance how many elements we need to send to a function
For these cases we can use the indeterminate parameters by position or by name
Arguments by position
As an argument we will use a tuple type preceding it with the symbol *
In this way all the arguments will be received by position, being able to navigate them comfortably with a for
Arguments by name
As an argument we will use a type to dictate it before the symbols **
In this way all the arguments will be received by the name of the dictionary key, being able to navigate them comfortably with a for
Arguments by position and name
Sometimes you may need to use both types of parameters simultaneously in a function, so you must create both dynamic collections
First the indeterminate arguments by value and then by name
All examples have used the names args and kwargs but they're not mandatory
Many frameworks and libraries use them so it's a good practice to call them that, by convention
The pass statement
It's a null operation, when we execute it, nothing seems to happen
It is useful as a container when a statement is required syntactically, but you don't need to execute code
The return statement
The statement return it is the one that allows you to return the result of a function
Supports types str, int, float, complex,bool, list, tuple, dic and class
The statement return optional, but if omitted, by default it will return the null value (None)
Multiple return
An interesting feature of the functions in Python, is the possibility to return multiple values separated by commas
Functions are predefined by the language
Language defined functions are sorted by modules
A module is a set of classes and functions stored in a file that can be used later
We can create our own modules and import them into the new programs we make
However, it is advisable to reuse existing modules
To import a module we will use the statement import
In this way we will be driving a namespace, similar to how C++ does (using namespaces) or Java
It has the following syntax
To call a specific function of the module we will use number.funcion() or alias.funcion in case we've given the module an alias
If you don't want to use all the functions of the module but if you do, we'll use the statement from
It has the following syntax
Sys module
This module is responsible for providing variables and functionalities related to the interpreter
The variables of the sys module are:
- sys.executable
Returns the absolute directory of the Python interpreter executable binary file
- sys.platform
Returns the platform on which the interpreter is running
- sys.version
Return the Python version number with additional information
The most prominent methods of the sys module are as follows:
- sys.exit()
Force the interpreter out
- sys.getdefaultencoding()
Returns the default character encoding
- sys.getfilesystemencoding()
Returns the character encoding used to convert unicode file names to system file names
- sys.getsizeof()
Returns the size of the passed object as a parameter
os module
This module allows us to access OS dependent features
Above all, those that give us information about the environment of the same and allow us to manipulate the directory structure
Some of the most commonly used methods are:
- os.getcwd()
It tells us what the current directory is
- os.chdir(route)
Change working directory to last path as an argument
- os.chroot()
Switch to the root directory
- os.mkdir(directory)
Creates the passed directory as an argument
- os.rmdir(directory)
Deletes the passed directory as an argument
- os.remove(file)
Delete the file passed as an argument
- os.rename(current, new)
Renames the current file by the name of the new file, passed as arguments
math module
This module provides us with mathematical functions and constants
We can find all the functions and attributes in your documentation
Some of the most commonly used variables are:
- math.pi
Represents the value of the variable \pi (Pi number)
- math.e
Represents the value of variable e (Euler number or Napier constant)
- math.tau
Represents the value of the variable \tau = 2\cdot\pi (constant Tau, proposed by Bob Palais, Peter Harremoes, Hermann Laurent, Fred Hoyle, Michael Hartl, Nicolas Atanes, among others)
Some of the most commonly used methods are:
- math.ceil(x)
Returns the integer less than or equal to x
- math.floor(x)
Returns the largest integer less than or equal to x
- math.exp(x)
Returns the number e raised to the power x
- math.log2(x)
Returns the base 2 logarithm of x
- math.sin(x)
Returns the sine of x radians
- math.cos(x)
Returns the cosine of x radians
- math.tan(x)
Returns the tangent of x radians
- math.degrees(x)
Converts the x-angle of radians to degrees
Random module
The random module is used for everything related to random numbers
Some of the most commonly used methods are:
- shuffle(col)
Randomly reorders items in a collection
- sample(col, k)
Take n items without a collection replacement
- random()
Generates a random number in the range [0, 1)
- uniform(a, b)
Generates a random number in the range [a, b]
- randint(a, b)
Generates an integer random number in the range [a, b]
- gauss(mean, sigma)
Generates a random number following a gaussian distribution with given mean and sigma
NumPy module
Numpy provides us with a lot of mathematical functions and the type of variable NumPy array
Usually the NumPy package is usually imported with the alias np
This module allows us to work with mathematical structures in a very comfortable way (product of matrices, random numbers, operations on matrices...)
It is a module that is not installed by default
To do this open a terminal and run:
We can find all the functions and attributes in your documentation
Some of the most commonly used methods are:
- random.rand()
It returns random values to us with the desired shape
- empty()
Creates an empty array with the right shape
- zeros()
Creates an array of zeros with the right shape
- dot()
Calculates the matrix product between two arrays
- sum()
Calculates the sum of the elements of an array
Pandas module
The package pandas is widely used in the Data Scientists
It's one of the packages we should focus on to learn about data analysis
Through pandas we can get acquainted with our data set by cleaning it, transforming and analyzing it
For example, with pandas we can read a CSV file from our computer, pass it to a DataFrame (a table, in essence) and do things like:
- Calculate statistics and answer questions about the data such as getting the maximum, minimum and average of each column, knowing how correlated columns A and B are, or knowing the statistical distribution of column C
- Clean up data doing things like removing missing values or filtering rows and columns according to some criteria
- Visualize the data with the help of matplotlib
- Save the data already cleaned back in a CSV or database
- Take one or more functions as input
- Return a function as output
Pandas is based on NumPy, so we should have that module pre-installed
Jupyter Notebooks it's an app that complements very well with Pandas, since we can run the code by cells instead of completely, which is useful if we work with large datasets
The primary components of Pandas are series and DataFrame, the first type being a column and the second being a table
Usually the pandas package is usually imported with the alias np
It is a module that is not installed by default
To do this open a terminal and run:
We can find all the functions and attributes in your documentation
Now let's create a simple example to show the use of pandas using a series that will contain the members of the Spanish football team that won the 2010 World Cup
To create the series we use the method pd.Series you will receive a list of player names and a list with the number of players
Then we'll show it by screen
Now we're not going to explicitly point indexes on it, then it will generate the indexes automatically starting from the zero value
Now let's introduce a dictionary into the series instead of a list and add one more player
Now let's create a simple example to show the use of pandas using a DataFrame that will contain the members of the Spanish football team that won the 2010 World Cup
To create the DataFrame we use the method pd.DataFrame you will receive a dictionary with player data, a list with the column name and a list with the number of players
We'll add one more player using the method loc dataFrame object
Then we'll show it by screen
Display functions
The function print serves to send the output of your arguments to the browser in a format
Operador %
The operator % can also be used for string format
Interprets the left argument much like a style format string of the function printf C++, which applies to the right argument
In Python, there is no function printf but it has been endowed with its functionality print
For this purpose, the operator % is overloaded for strings to show the format of the string
It is often called string module operator (or, sometimes, even, module)
It has the format:
To format a text you have to enter the following formatting characters preceded by the symbol %:
Format characters for text | |
Character | Description |
c |
The argument is treated as a string value and collects a single character |
d |
The argument is treated as a value of type int and presented as an integer |
e |
The argument is treated with scientific notation (for example 1.2e+2) |
E |
As %e but uses the uppercase letter (for example 1.2E+2) |
f |
The argument is treated as a float value and presented as a floating point number (considering the locale) |
F |
The argument is treated as a float value and presented as a floating point number (without considering the locale) |
g |
Like %e and %f |
G |
Like %E and %f |
o |
The argument is treated as an integer value and presented as an octal number |
s |
The argument is treated as a string value and collects a string |
u |
The argument is treated as a value of type int and presented as an unsigned decimal number |
x |
The argument is treated as an integer value and presented as a hexadecimal number |
X |
The argument is treated as an integer value and presented as a hexadecimal number in uppercase |
Format method
The method format added to string types in Python 2.6
Allows the use of characters {} to mark where a variable will be replaced and uses detailed formatting policies used to format
This method allows us to concatenate elements within an output through the positional format
Brackets and characters within them (called format fields) are replaced with objects passed to the format method
A number can be used in parentheses to refer to the position of the argument passed to the format method
Keep in mind that the first argument starts at zero
A key identifier can also be used when the argument is passed by name
This option will be useful when working with dictionaries or when we want to reduce the code something
Optionally you can put the symbol : after the number or name, and specify the type of the object:
Format characters for text | |
Character | Description |
s |
The argument is treated as a string value and collects a string |
d |
The argument is treated as a value of type int and presented as a decimal number (signed) |
f |
The argument is treated as a float value and presented as a floating point number (considering the locale) |
Higher order functions
In mathematics and computer science, higher order functions are functions that meet at least one of the following conditions:
Lambda functions
Lambda functions are anonymous functions, that is, unnamed functions
They are only used where they were created (although they can be named and used later)
They are usually used in conjunction with map, filter and reduced functions
They are usually used to create functions that are passed as arguments to other functions
In Python the syntax of a lamdba function is:
Where the expression can be any operation we would do when making an assignment
map function
It allows us to apply a function on each of the elements of an iterable
It returns a map object, which we can easily transform into a list
filter function
Allows us to filter items in a collection (for example, a list)
It is a widely used function in the treatment of collections
reduce function
Take a function and a collection by reducing it to a single value
It would be equivalent to an aggregation function
It belongs to the module functools, so it must be imported before it can use reduce
The function we pass must take at least two parameters and an optional one
The first is the aggregate parameter that accumulates in each iteration over the items in the collection
The second element corresponds to the step of the iteration
The third is in case an initial value is needed