Variables#
Learning Objectives#
- Create a variable in Python code.
- Change the value of an existing variable.
- Given some code, identify which lines of code create variables and which lines change the values of existing variables.
- State the rules for naming variables, and identify valid and invalid variable names.
- Given some code, be able write the state of the environment on a piece of paper.
What Are Variables?#
Programming languages use variables to store data. Variables are containers for your data. That’s it! Conceptually, variables are that simple.
You might use a variable to store the result of a computation, or to load data from a file and store that data in your computer’s memory. Here is an example:
y = 5 + 7
If you run the above cell, it will evaluate the mathematical expression 5+7
and store the result, 12, in a variable named y
.
Creating Variables#
How to Create a New Variable#
Python makes it very easy to create a variable. Simply type the name of your new variable, an equals sign, and a valid value. For example, if you type:
z = 14
into a code cell and run it, Python will create a new variable named z
and store the value 14 in it.
Changing an Existing Variable#
What if you have already created the variable z
earlier in your code? Then running the command z = 14
will tell Python to overwrite the old value of z
with the new value of 14. We will discuss this at length in the next subsection on environment and state.
Rules for Naming Your Variables#
Python is pretty flexible about variable names. You can name your variable anything you want, subject to the following restrictions:
Variable names can contain:
Uppercase letters (A-Z)
Lowercase letters (a-z)
Digits (0-9)
The underscore character ( _ ). Note: this is different than the hyphen, or dash (-), which is not allowed in a variable name.
Variable names cannot contain spaces.
A variable name cannot begin with a digit.
Thus, some valid variable names are accy570
, Accy570
, ACCY570
, and ACCY_570
.
Finally, note that Python is case-sensitive! That means that all four of the example variable names in the previous sentence are treated as different.
Environment and State#
One of the fundamental ideas in programming is that your code executes inside an environment. An environment is a container for all of your variables. The state of your environment is simply a list of all your variables and their values. When you first opened this notebook, there were no user-defined variables in your environment so you had an “empty” environment. If you execute any of the example code below, Python will create variables and save them in your environment. These variables, and the entire environment, will be deleted when you close this notebook.
Let’s watch the environment in action. First, open JupyterLab on your computer and create a new notebook. Then, open the JupyterLab debugger by clicking on the bug button in the top right of your JupyterLab window (it is on the right side of the toolbar that has the save button, and should be to the left of the words Python 3 (ipykernel)). A new pane will open on the right side of your JupyterLab window. At the top of the pane, next to the word Globals, there are two buttons. If you hover over one, it will say “Tree View”. If you hover over the other, it will say “Table View”. Make sure the Table View button is selected. Now to the left of the word Globals, you will see the word VARIABLES. Click the arrow to the left of VARIABLES to expand the list of variables. In the box beneath this button, you will see the values of system variables that Python created. Now, type the code from the following cells into new cells in your notebook. Run each, one at a time, and watch the changes in your debugger.
testvar1 = 19
testvar1 = 'Cow'
testvar2 = 'Sheep'
testvar3 = 3.1415926535
You probably noticed a number of things.
The debugger shows you information about each variable’s name, type, size, shape, and content. We haven’t explained these yet and will do so later in the course. For now, just look at the name and content.
testvar1
was initially assigned the value 19. When you ran the cell that createdtestvar1
, you changed the state of your environment.The second cell changed the value of
testvar1
to ‘Cow’. This command also changed the state of your environment.The third and fourth cells created
testvar2
andtestvar3
and thereby changed the state of your environment.
If you want to run the above cells again and watch the debugger, run the following cell first. It will delete testvar1
, testvar2
, AND testvar3
. Alternatively, you can restart the kernel; if you do, you will need to restart the debugger by clicking on the bug button again.
del testvar1, testvar2, testvar3