3. Variables

Variables in a programming language are used to temporarily store information. This means that they can save a value at one time, and then change that value at another time.

For example, in a program for calculating average grades, we could have a variable called "sum_notes" that stores the sum of all grades entered by the user and another variable called "number_notes" that stores the number of grades entered. . Finally, the variable "average" will store the result of calculating the average grade.

suma_notas = 0
numero_notas = 0

while True:
   nota = input("Introduce nota:")
   if nota == "fin":
      break
   suma_notas = suma_notas + int(nota)
   numero_notas = numero_notas + 1

promedio = suma_notas / numero_notas
print("Promedio = ", promedio)

Variables also allow us to give a value a meaningful name, making the code easier to read and understand. Instead of having to write the number 3.141592653589793 every time, we can store it in a variable called "pi", so that the code is more readable and easier to write.

Constant values ​​that are used multiple times in the program are also stored in variables. In this way it is easier to understand the program and also, if one day we want to change the value of the constant, it will only have to be changed in one line of code.

In summary, 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 variable name 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 like "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, ñ, umlauts, etc) and spaces can cause problems in some programming languages. It is better to get used to never using them.
Use snake_case notation
This is a writing style in which words in the variable name are separated by the underscore character "_". Examples of this notation are "number_of_students", "length_of_pool", etc.
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 macro for each proposed exercise. Save the macros with the names proposed in the exercises.

  1. Write 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 to float() all numbers entered from the keyboard.

    Remember to name your variables in snake_case style.

    pi = 3.1415927
    
    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 exercise_02_perimetro.py

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

    Print the result on the screen.

    Save the program with the name exercise_02_salary.py

  3. Develop a program to calculate the volume of a cube. The program should ask the user to enter 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 exercise_02_cube.py

  4. Write 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. Then calculate the change to be returned by performing a subtraction.

    Print the result on the screen.

    Save the program with the name exercise_02_change.py

  5. Write 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. Calculate the discounted price below with the following formula:

    final_price = starting_price * (100 - discount) / 100

    Print the result on the screen.

    Save the program with the name exercise_02_discount.py