Variables and Assignment

Variables and Assignment#

Assigning variables#

Variables are symbols, which represent data in memory. Data can be assigned to these symbols with the assignment operator =.

width = 20
height = 5 * 9
n = 4.

Assignments can be used for further calculations. The names, or symbols, defined in the assignment statements above, are used as placeholders for the data they are assigned to.

width * height
width * n

Variables can also be assigned expressions that may contain variables too.

area = width * height

Multiple assignments in one line are also possible

a = b = 3.
a, b, c = 1, 2, 3

To inspect the content of an assigned variable, we can use the print() function, to output the content of the variable.

print(area)
print(a, b)
900
1 2

In an interactive session, such as Jupyter Lab, simply putting the variable name on the last line will also output the content of the variable

c
3