Variables
0. Learning objectives
- Store values in variables in Python
- Perform operations on variables, including mathematical, logical operations and comparisons,
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.
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:
- Variable names must start with either a letter (
a-z,A-Z) or an underscore (_) - Variable names can only consist of the following characters: upper/lower case letters, numbers and underscores. That's it! No special characters like @ or !
- Variable names cannot be any of the special reserved keywords in Python (e.g.
True,False,and,not,or, plus some more soon). You will be able to tell if you are using a reserved keyword because usually IDEs (e.g., Thonny) will make those words colored once you type it into the editor. Worse case, Python is definitely going to tell you that is not ok. (Try the following in Brython:True = 5.
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:
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 |
|