Variables

0. Learning objectives


1. What is a variable?

So far, we've been using Python like a calculator, where we had to keep typing the numbers or string we want to use in an expression. This can be pretty annoying, and not good for abstraction. In fact, explicitly typing out these values in a computer program is called hard-coding. It's a much better idea to use variables.

A variable is like a "box" that can hold one thing.

This might sound vague, but it's the basic idea of variables. In Python, we can "put" anything we want "into" a variable.

What do we mean by "put into"?

You might be familiar with mathematical expressions like $y = x + 2$. In Math, this represents an equality. As a matter of fact, we can evaluate the value of $x$ if we are given the value of $y$. In most computer languages, things are a little different. An expression like y = x + 2 should be interpreted as $y \leftarrow x + 2$. This is not an equality, it is an assignment. This means that Python will first evaluate the value of the expression to the right of the = sign and then assign the resulting value to the variable on the left. In other words, Python "takes the value stored in the variable x, adds 2, and stores it in y". This, of course, assumes that we had previously stored something in x (e.g., x = 23). If you try to read a value from a variable to which you have not assigned any value, Python will complain with a NameError telling you that your variable has not yet been defined. In Brython try to do something like y = happyHippo + 5, and look at what happens. Then assign a numeric value to happyHippo and try again.

To further underline the difference with Math, consider the following expression: x = x + 1. Python is going to interpret this as "take the current value of x, add 1 and then store it back in x". This means that if x started out as 2, then after executing x = x + 1, the variable x would now store a value of 3 and the original value of x is effectively been replaced by the result of the computation.

Let's look at some examples:

We have already seen it but, as a reminder, in Python # introduces a single line comment. Python will ignore (not evaluate) anything that comes after the #. You can you can create multi-line comments by starting all the lines with a #, or you can enclose your lines between triple quotes either double (""") or single ('''), as long as they match at the beginning and end of the comment block.

"""
For example!
This is a multi-line
comment block
Started and ended by matching triple double-quotes
"""

Soon, when we talk about functions, we will see that the a multi-line comment block right after the function definition is called a docstring and has a special meaning... but no spoilers for now!


2. Variables naming rules & conventions

In the example above, we used letters (x, y, z and b) for our variable names. We can use as many letters and also mix in numbers to define variable names, but there are few rules we need to follow:

Finally, there are different naming conventions that are used for variables. We'll use a mix of these so you can get exposed to and learn to recognize the different styles. However, when you work for a company, typically they will ensure you're writing code that uses the same conventions as everyone else in the company. To complicate things, different languages use different conventions. Here are the different variable naming conventions:

  • snake case: consists of entirely lower case letters with some underscores to separate words. For example: this_is_a_snake_case_variable.This is actually the preferred style according to the official PEP 8 Style Guide for Python.
  • camel case: variables start with an uppercase letter, followed by lower case letters until a new word which again starts with an uppercase letter. For example: ThisIsACamelCaseVariable.
  • drinking camel case or mixed case: a variant of camel case in which the first letter in the variable is lower case. For example: thisIsADrinkingCamelCaseVariable.

It's usually good practice to be consistent with your naming conventions because it makes it easier for someone else to read your code. It's also good practice to name variables according to what they represent. For example, you might use x for "position along x-axis" or T to store "temperature".