Skip to content

Variables

Learning Objectives

At the end of this sub-unit, students should

  • understand what is a variable.
  • be able to write meaningful variable names.

Abstraction

One of the major concept in computational thinking is the idea of abstraction. Abstraction carries different meaning depending on the topic at hand. In fact, we may even be using a slightly different notion of abstraction later on. But the underlying principle is the same, we want to look at a particular "construct" as an abstract entity where we do not care about the underlying details.

Variable is one such abstraction. So far, we have worked with values and we should know how to operate on values. These values are fixed in the code.

Looking back at the example of interest rate, there is no difference between writing

>>> 10000 * ((1 + (0.05 / 12)) ** (12 * 10))
16470.0949769028

and simply writing

>>> 16470.0949769028
16470.0949769028

as both will produce the value 16470.0949769028 all the time regardless of where the program is run.

Of course we can say that by writing it as a formula, we can use different values of \(P\), \(r\), \(n\), and \(t\). But once the program is written, it will always produce the same result all the time. We cannot change the behavior while the program is running. To do that, we need variables.

A variable is simply an abstraction of values. So instead of considering only a single value, we can say that a variable P can have many possible values. These different values will depend on how the program is evaluated. But at any given instant, a variable will hold only one value.

More precisely, a variable is an abstraction that is characterized by three components:

  1. The name of the variable.
  2. The value of the variable.
  3. The address (i.e., location in memory) of the variable.

The last one is assigned by the computer, so we can often ignore that. We can even represent these visually as a box and arrow1.

Variable

Since the address is managed by the computer, we do not have to worry where the variable is actually located in memory. This simplifies our task. If we want to find the value, we simply use the name. The computer will then retrieve the value for us. As such, we will always ignore the address when visualizing a variable.

Variable

Variable Names

Now we know that variables can be used to abstract values. The most important thing about variables is the name because that is how we can use a variable. Remember, we do not need to know the address and the value is the construct we are abstracting. So we are left only with the name.

There is a strict rule governing a valid name. It can be captured as follows.

Variable Naming Rule

A variable name in Python must satisfy all of the following rule.

  • It must start with uppercase (i.e., A to Z), lowercase (i.e., a to z), or underscore (i.e., _).
  • It must contain only uppercase (i.e., A to Z), lowercase (i.e., a to z), numeric (i.e., 0 to 9), or underscore (i.e., _).
  • It must not be one of the reserved keywords.

Although your program can run --even run correctly-- with cryptic names, it is always useful to have meaningful variable names. By meaningful, the variable names should describe the "responsibility" of the variable. For instance, it is meaningful to have a variable called name to store a name and not the age.

Using meaningful and easy to read names will lift some of the mental burden when reading code. It is often said that our working memory can only process seven elements at the same time. With conventions, variable names need not be one of these seven. Hopefully, that means we can process more.

Python Naming Convention

We should use the following good practice in naming conventions. This comes from Python convention documented as PEP-0008. There are conventions for constants, variables/functions, and classes names.

  1. Name should be in uppercase (i.e., A-Z) words and numeric (i.e., 0-9) separated by underscore (i.e., _).
    • For instance, MILES_TO_KMS.
  2. Name should be indicative of the responsibility.

This is known as UPPER_SNAKE_CASE.

  1. Name should be in lowercase (i.e., a-z) words and numeric (i.e., 0-9) separated by underscore (i.e., _).
    • For instance, students_in_cs1010s.
  2. Name should be indicative of the responsibility.

This is known as lower_snake_case.

  1. Name should be in camel case words (i.e., first letter in uppercase, the rest in lowercase).
    • For instance, ElectricVehicle.
  2. Name should be indicative of the responsibility.

This is known as UpperCamelCase.


There are also general guidelines.

  1. If not clear from the context, the name should inform the type.
    • For instance, student_age_as_str.
  2. Avoid built-in keywords.
  3. Avoid single characters name.
    • An exception is for iteration variable as we typically use single character i, j, or k.
      • If you need more, you may want to modify your code instead.
    • Another exception is that we may use _ for iteration variable that we do not even use.

Case Sensitivity

Variable names are case sensitive. This means that the variable x is different from the variable X. The former is lowercase x while the latter is uppercase x. As such, be careful when writing variable names and try to avoid names that can look ambiguous.

Substitution

Because variable is an abstraction of values, it can be thought of as a mapping from the variable name to the value it contains. The process of retrieving the value is called substitution. Substitution is done by simply replacing the variable name with its mapped value. However, this can only be done if the variable name exist. Otherwise, we will get an error.

So far, we have not learnt how to instantiate (i.e., declare) variables. We will learn about that later. But for now, assume that we have the following variables declared.

Var3

Substitution

1
2
3
4
>>> x + X  # ⇝  2 + 2.0  ⇝  4.0
4.0
>>> y * x  # ⇝  'A' * 2  ⇝  'AA'
'AA'

Review

Question 1

Select ALL valid Python name?

Question 2

Select the meaningful name following Python convention for constant?

Question 3

Select the meaningful name following Python convention for variables?


  1. If you are wondering where the arrow is, it will be discussed in future topics.