Variables and Expressions in Python

From Sustainability Methods
Revision as of 21:27, 17 September 2024 by Gustavo (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

What is a variable?

Similar to an administration, a computer processes information and this information has to be stored somewhere. An administration has filing cabinets to store data, in programming we have variables. So, you can think of a variable as a container or filing cabinet where you can store different things. For example, you have just got a score of 30 in your favourite computer game and want to store it in the variable current_score. A variable has a name (e.g. current_score) and can store a value (e.g. 30). This value can be different types of data, which in our case it is an integer, (i.e. a whole number) but it is also possible to store in variables data suchb as text, a whole dataset, a pdf or a mini version of ChatGPT.

Creating variables

To create a variable we invent a variable name (following the naming conventions below) and assign it a value using the assignment operator =. The moment we assign the value, the variable begins to exist. Python automatically recognizes what type of data is stored in the variable based on certain characteristics (see Data Types for more on this).

Note: There are a few simple rules for naming variables:
- The name must start with a letter or an underscore.
- The name must not start with a digit.
- The name may only consist of alphanumeric characters and underscores (A-z, 0-9, _).
- The name must not contain spaces. Use underscores instead.
- Tip: it is advisable to give variables self-explanatory names.

To create the variables current_score and highscore, we assign the values 30 and 50 to the variables current_score and highscore, respectively.

current_score = 30
highscore = 50

Note: The variable that is created/changed is always on the left, followed by the assignment operator `=`, followed by the new value on the right.

After executing the above code cell, these variables are now temporarily created in the working memory of our coding environment. They exist as long as the notebook is open, even if you delete the cell, where the variable was created. The variables will be deleted as soon as you close this notebook.

As with a filing cabinet, we can now access the stored values by simply using the names of the variables. For example, we can print the values of the variables current_score and highscore.

print(current_score)
print(highscore)

Notice the order of execution of the code in the output below.

30
50

Now, what happens when you make a tiny mistake in writing the variable expression?

current score? = 30
print(current score?)

If you run the code cell above, you will get the following error, which Python specifies what kind of error it is.

  Input In [1]
      current score? = 30
                   ^
  SyntaxError: invalid syntax

Changing the values stored in variables

However, the value of a variable is not necessarily fixed. We can use the assignment operator to overwrite the values stored in the variable.

Taking our above example, imagine that you played your computer game again and got a score of 60. We can overwrite the values of current_score and highscore accordingly.

current_score = 60
highscore = current_score

Now you print the variables:

print(current_score)
print(highscore)

As you can see, it is also possible to overwrite the value of one variable directly with the value of another variable.

60
60

Removing variables

In Python, variables can be removed using the del statement. The del statement allows the deletion of references to objects. Once a variable is deleted, it cannot be accessed unless reassigned. For example, if you want to remove the variable current_score:

del current_score

if you try to print that variable again, you will get an error:

print(current_score)
 NameError: name 'current_score' is not defined

Python expressions

When you are writing code in Python, you are typically writing expressions, which are a combination of variables, operators, and values that produces a result or an output. When running the code cell, Python evaluates expressions to compute a value, which can be used in assignment statements, functions, or other operations. For example, imagine that you are want to calculate the total score of the days you played your computer game.

score_friday = 50
score_saturday = 40
score_sunday = 70
total_score = score_friday + score_saturday + score_sunday
print(total_score)

Now, the output showing your total score would be:

160

Summary

  1. Variables act as containers to store and reuse data.
  2. Variables are created by assigning a value with the assignment operator =.
  3. Values stored in variables can be accessed via the variable name. They can also be overwritten by new values.
  4. Doing operations with variables is basically writing Python expressions.

The authors of this entry are Wanja Tolksdorf and Gustavo Rodriguez.