More about Python. The components of language

More about Python
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.

More about Python. The tools to get started

Pip

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.

Important
Remember that the command to launch programs is python3 nombre_del_programa and not python nombre_del_programa.

Let's check if we have PIP installed
pip3 -V

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
python3-pip

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.

print("¡Hola!")
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

print("¡Hola!")
nombre=input("¿Cómo te llamas? " )
saludo="Bienvenido "+ nombre +". Gracias por leer Linux Adictos"
print(saludo)

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)

Modules

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:
def Bienvenida():
   publicacion="Linux adictos"
   nombre=input("¿Cómo te llamas ")
   saludo="Bienvenido "+nombre+" Gracias por leer "+publicacion
   print(saludo)

In test.py write:
from Bienvenida import Bienvenida
Bienvenida()
What we are doing is separating the function and the function call into two separate files.

 


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

Be the first to comment

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.