On previous chapter In this Python programming course using Linux we talk about lFunctions are small programs for specific tasks that we can reuse both in the application we are writing and in others that need to perform that task.
One thing we must keep in mind when working with functions is how they interact with other program components such as variables, lists, tuples, and dictionaries.
Python programming course using Linux
What we must keep in mind when working with functions is that there are variables, lists, and dictionaries that will be used by the entire program and others that are used only by the function for which they are defined.
Variables
This is the difference between a globally defined variable and a locally defined one.
The `distro` variable is defined at the beginning of the program and the `mostrar` function uses it. It could also be used by another function created to add it to a list.
Since the variable is defined inside the function, if we try to call it from another part of the code, we will get an error message.
It is possible to modify a global variable for the entire program using the global command.
This program assigns Ubuntu to the distro variable and then creates a function that tells it to modify a global variable by changing its name to Debian. It then calls the function to perform the name change.
Subscriber lists
We can add or remove items from a globally created list using a function. This program, for example, adds an item to the distros list.
Reassignment is the process of creating a list within a function that has the same name as the global list. Let's look at this example:
As we can see, when we invoke the function, it prints the list defined inside the function, while outside the function it prints the list originally created.
Tuples
Tuples are immutable, so we can only do two things
1) Create a local tuple with the same name as a global one.
2) Replace a tuple.
Inside the function we add the global instruction that tells it that we are going to replace the tuple that we defined at the beginning.
Dictionaries
Dictionaries are mutable, therefore we can easily modify one that is defined globally.
If you look closely, you will notice that within the function we use square brackets to indicate the element being modified and the modification, instead of the braces that dictionaries use to define their content.
Functions with or without a parameter
When we call a function, it is possible to assign it a specific parameter.
This is an example of a parameterless function
And this is an example of a function that we tell to act on a specific parameter
We can set different types of parameters:
- def function(): The function does not require any parameters.
- def function(distro): The content of the distro variable is assigned as a parameter.
- def function(distro=»Ubuntu»): The function is assigned the parameter Ubuntu but it can be replaced by another user-defined parameter.
- def function(*args): We can use several parameters.
We already gave examples of the first two paragraphs - Let's look at an example of the other two.
This program assigns the Ubuntu parameter to the function by default. When we call the function without assigning a parameter, it performs the check for Ubuntu. The next call performs the check for Debian, which is on the list, and the third for Arch Linux, which is not.
What if we want to test several parameters with a single call?
This way we put the list of parameters into a tuple.
It is also possible to use lists, tuples, and dictionaries as parameters.
Subscriber lists
Tuples
Dictionary
Item is an internal Python method that allows us to display all key/value pairs.
The return statement
Normally, the results of code execution within a function are not available to other parts of the program. This can be corrected with the `return` statement, which also marks the end of the function's execution and the continuation of the program's execution. Let's look at the following example:
This program asks the user to enter the name of a distribution and saves the result of the comparison in a variable for later use.
Comments on the code
When we write extensive code and need to review it again, or when we need someone else to do so, we must document it; that is, include explanations of what each part of the code does. It is possible to write simple comments preceded by the # symbol.
# El comando print muestra en pantalla un texto o el contenido de una variable
Python will understand that the text following the # sign is not part of the code and will not attempt to execute it.
Although technically they are not comments but text strings that are not assigned to a variable and are not recommended to be used, it is possible to write information between 3 quotation marks that the program will ignore.
«» »
The print command prints text to the screen.
Lists are groups of elements,
Functions are small programs that perform a task and can be reused.
Tuples are immutable.
«» »
But, as we said, the recommended practice is to write the number sign at the beginning of each line.
# The print command prints text to the screen.
# Lists are groups of elements,
Functions are small programs that perform a task and can be reused.
# Tuples are immutable.
The characteristics of the comments are:
- They are used to explain specific parts of the code.
- They are ignored by the program once they have been interpreted.
- They cannot be read from the code itself at runtime.
In the case of functions, objects, classes, methods, and modules (we'll discuss modules in future articles), it's possible to use docstrings. These are text strings placed at the beginning of a function that briefly explain what the function does, what parameters it receives, and what values it stores. For Python to recognize it as a docstring, the text must be enclosed in three quotation marks at the beginning and end.
In the next article we will explain how to use docstrings.