3. Variables

Variables in a programming language are containers that serve to store information with a descriptive name. The way to assign a value to a variable is to write the variable name, an equal, and the assigned value:

pi = 3.1415927
altura = 176
peso = 72

The value of a variable can stay the same throughout the program or can change:

suma = 0
suma = suma + 2
suma = suma + 4
suma = suma + 6

Magic numbers

The most correct thing to do when writing a program is to store all the numbers inside variables and then use only the variables. This avoids using so-called magic numbers, which are undesirable.

Numbers that appear in a program and are not inside a variable are called magic numbers. The programmer understands these numbers when writing the code, but other people often cannot understand their meaning. Even the programmer himself, over time, can forget the meaning of a specific number that he wrote in the code.

Variables allow us to give a meaningful name to a number, making the code easier to read and understand, thus avoiding magic numbers.

For example, instead of having to write the number π (3.1416) each time, we should store it in a variable called 'pi', so that the code is more understandable.

If one day we want to change the value of this constant, for example, to give it more digits of precision (3.141592653589793), we will only have to change one line of the entire code, in the definition of pi.

In short, variables are an essential tool in programming since they allow us to store and access information in an organized and easy-to-understand way.

Name variables

When looking for a name for variables it is important to follow some rules to ensure that the code is easy to understand and easy to modify later.

Use meaningful names
The name of a variable should reflect its content or its purpose in the code. For example, instead of using a variable named "x" or "temp", it is better to use a name such as "grades" or "average".
Use short but descriptive names
Although it is important to use meaningful names, it is also important to avoid names that are too long or complex. A short but descriptive name is easier to read and write.
Do not use special characters
Special characters (accents, eñe, umlauts, etc.) and spaces can cause problems in some programming languages ​​or with other programmers. It is better to get used to never using them.
Use snake_case notation

This is a writing style in which the words in the variable name are separated by the underscore character "_". Examples of this notation are "number_of_students", "length_of_pool", etc.

If we are going to create several related variables, it is preferable that their first word match in all of them:

persona_altura = 175
persona_peso = 68
persona_nombre = 'Juan'
persona_apellido = 'Gómez'

piscina_longitud = 30
piscina_anchura = 15
piscina_profundidad = 2.5
Do not use reserved words
Language reserved words such as "print", "input", "for", "while", "break", "continue", "True", " False", etc. They should not be used as variable names.

By following these rules, you will be able to name your variables in a clear and organized way, which will help you write code that is easier to understand and maintain.

Exercises

Write a program or macro for each proposed exercise. Save the programs with the names proposed in the exercises.

  1. Create a program to calculate the perimeter of a circle. The program should ask the user to enter the radius of the circle, and then calculate the perimeter using the formula "2 * pi * radius". Name the variables to store the radius, the number pi, and the perimeter of the circle.

    Print the result on the screen.

    Remember to convert all numbers entered by the keyboard to float().

    Remember to name the variables with style snake_case.

    Solution to the exercise:

    pi = 3.141592653589793
    
    radio = float( input("Introduce el radio del círculo: ") )
    
    perimetro = 2.0 * pi * radio
    
    print("Perímetro del círculo = ", perimetro)
    

    Save the program with the name tema_03_ejercicio_01.py

    Note that the number 2.0 that appears in the program is a magic number. It has remained this way because it is part of the perimeter formula, known to everyone.

  2. Write a program to calculate the salary of an employee. The program should ask the user to enter the hours worked. The hourly wage will be 15 Euros and must be stored in a variable. Calculate the total salary by multiplying the hours by the hourly salary.

    Print the result on the screen.

    Hint: Use the previous program as a template, changing the name of the variables and the numerical values.

    Save the program with the name tema_03_ejercicio_02.py

  3. Write a program to calculate the volume of a cube. The program should ask the user to type the length of one side. The volume of the cube will be calculated by raising the length of the side to the third power (** 3).

    Print the result on the screen.

    Save the program with the name tema_03_ejercicio_03.py

  4. Create a program to calculate the change of a purchase. The program should ask the user to enter the total cost of the purchase and the money received. Next calculate the change that must be returned by performing a subtraction.

    Print the result on the screen.

    Save the program with the name tema_03_ejercicio_04.py

  5. Create a program to calculate the final price of a product with a 25% discount.

    The discount percentage must be stored in a variable.

    The program should ask the user to enter the cost of the product. Then calculate the discounted price with the following formula:

    precio_final = precio_inicial * (100 - descuento) / 100
    

    Print the result on the screen.

    Save the program with the name tema_03_ejercicio_05.py