In previous article We had discussed the reasons why to learn the Python programming language and how to have the latest version. Now is the time to learn the components of the language with some examples.
Table of Contents
More about Python. The tools to get started
Python has many modules that save code writing time because they bring preprogrammed functions that we need. Although some of them are pre-installed, others must be added as we need them.
There are two easy ways to add them; from the repositories of your distribution (as if they were a normal program) or using a packet manager called PIP. The second method ensures that we have the most current versions.
Remember that the command to launch programs is
python3 nombre_del_programa and not
Let's check if we have PIP installed
You should receive a message similar to this
pip 20.2.3 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
In case you don't have Pip installed you can do it from the package manager of your distribution. You need the package
Entering and displaying data
We are going to write our first program. It is likely that if you copy and paste the programs, they will not work. I recommend that you type them. Make sure you respect the spaces or they won't work.
nombre=input("¿Cómo te llamas? ")
print("¡Bienvenido/a!", nombre, ".Gracias por leer Linux Adictos")
In the first line of the program we print the greeting message. In the second, we define a variable called "name" and assign it as a value the answer to the question, What's your name? Note that Input has two functions, that of displaying a message and assigning the value of the response to the variable.
In the third line, the print command returns, printing two types of content. The predefined greeting (What is always in quotes) and the value of the variable (Variables are always without quotes. Variable and non-variable content is separated with a comma (,).
Let's try a modification
nombre=input("¿Cómo te llamas? " )
saludo="Bienvenido "+ nombre +". Gracias por leer Linux Adictos"
In the third line we assign to the variable «greeting» a combination of predefined text (between quotes) and the value of the variable (without quotes)
Having to write a series of instructions in code each time it is needed is a waste of time and space. Especially if it is an extensive program. That is why it is best to use an integrated development environment and write a long program (script in the jargon) to be executed later by the interpreter.
As the program lengthens, it is advisable to divide it into several files to facilitate its maintenance and reuse. This is what modules are used for.
Modules are files that contain definitions and declarations in Python. The file name is the name of the module ending in .py. For example, we will create a module called Welcome.py. Within a module, the module name (as a string) is available as the value of the global variable __name__.
Keep in mind what I said above about the spaces. Note that the second, third, and fourth lines of the program start just below the space between the word def and the word Welcome. This is required by Python and we will explain it in the following articles.
In the first line we define a function called Welcome. The next three lines are the instructions that have to be executed each time the function is called.
In line 5 we define the content of the publication variable that the last instruction of the function needs to print the welcome message.
With the last line of the program, we execute the function.
Let's try something else. Create two files. One called Welcome.py and the other test. py.
In Welcome.py write these lines:
nombre=input("¿Cómo te llamas ")
saludo="Bienvenido "+nombre+" Gracias por leer "+publicacion
In test.py write:
from Bienvenida import Bienvenida
What we are doing is separating the function and the function call into two separate files.