View on GitHub

Computational Techniques for Life Sciences

Part of the TACC Institute Series, Immersive Training in Advanced Computation

Python - Variables & Memory

Objectives:


This series of lessons is aimed at learning how to analyze datasets in Python. But before we discuss how to deal with many data points, we will show how to store a single value on the computer.

The line below assigns the value 55 to a variable weight_kg:

weight_kg = 55

A variable is just a name for a value, such as x, current_temperature, or subject_id. Python’s variables must begin with a letter and are case sensitive. We can create a new variable by assigning a value to it using =. When we are finished typing and press Shift+Enter, the notebook runs our command.

Once a variable has a value, we can print it to the screen:

print(weight_kg)

and do arithmetic with it:

print('weight in pounds:', 2.2 * weight_kg)

As the example above shows, we can print several things at once by separating them with commas.

We can also change a variable’s value by assigning it a new one:

weight_kg = 57.5
print('weight in kilograms is now:', weight_kg)

If we imagine the variable as a sticky note with a name written on it, assignment is like putting the sticky note on a particular value:

Variables as Sticky Notes

This means that assigning a value to one variable does not change the values of other variables. For example, let’s store the subject’s weight in pounds in a variable:

weight_lb = 2.2 * weight_kg
print('weight in kilograms:', weight_kg, 'and in pounds:', weight_lb)

Creating Another Variable

and then change weight_kg:

weight_kg = 100.0
print('weight in kilograms is now:', weight_kg, 'and weight in pounds is still:', weight_lb)

Updating a Variable

Since weight_lb doesn’t “remember” where its value came from, it isn’t automatically updated when weight_kg changes. This is different from the way spreadsheets work.


Who’s Who in Memory

You can use the %whos command at any time to see what variables you have created and what modules you have loaded into the computer’s memory. As this is an IPython command, it will only work if you are in an IPython terminal or the Jupyter Notebook.

%whos
Variable    Type       Data/Info
--------------------------------
weight_kg   float      100.0
weight_lb   float      126.5

Warning

When selecting a variable name, if that name was previously in use, it becomes only associated with the variable. For example, if you created a variable named “print”, you would no longer be able to use the print function in that script.


Exercise


Keypoints:


Previous: Getting Started with Jupyter Top: Python Overview Next: Python - Numpy and Arrays