Variables#

Introduction to Variables#

Until now, all of the Python examples have used explicit data values typed directly into the code cells. This is okay for typing out a quick numerical calculation, but does not let us do anything any more sophisticated with our data. To do anything more complex your code will need to store and retrieve data, and to change the values of these data as you perform your calculations, which we do using variables.

What is a Variable?#

A variable is a Python is an object that stores some data.

This data can be a wide range of types, including int, float, or complex; a string; a bool; even a function, or a collection of other variables.

Assigning a Variable#

To create a new variable and assign it a value we use =.

days_in_october = 31

This creates a variable named days_in_october and assigns it the integer value 31.

The assignment of the value 31 to the variable named days_in_october.

The left-hand side of the assignment expression is the variable name.

  • Variable name must start with a letter or an underscore.

  • Variable names can be any length and can contain upper- and lower-case letters, numbers, and underscores.

  • Variable names are case sensitive. my_name and My_Name are two different variables.

  • Variable names cannot be special reserved keywords. You can see the full list of reserved keywords by running the command help("keywords") in a code cell.

Now we can use the variable days_in_october in place of the integer 31:

print("There are", days_in_october, "days in October")
There are 31 days in October

f-strings#

A neater way to do this is to use f-strings, which provide a way to embed expressions inside strings. An f-string is a string literal with an f character at the beginning, and curly brackets enclosing any expressions. When the f-string is created, any enclosed expressions are evaluated to give a single interpolated string:

f"There are {days_in_october} days in October"
'There are 31 days in October'

days_in_october can be used in any of the ways that we might use the value assigned to it. i.e. days_in_october behaves like an integer. If we call type with days_in_october we can confirm the assigned value is an int.

type(days_in_october)
int

Variables can also appear on the right-hand side of variable assignments.

a = 3
b = a
print(b)
3
  • In the first line, we create a variable a and assign it the value 3.

  • In the second line, we create a variable b and assign it to the same value as the variable a. This is the same as if we had written b = 3.

  • In the third line we print b.

The assignment of 3 to the variable a and the assigment of this to b.

Because b is assigned to the value stored in a, and not to a itself, if we reassign a, the value stored in b does not change.

a = 3
b = a
a = 2
print(f'a = {a}')
print(f'b = {b}')
a = 2
b = 3

The assignment of 3 to the variable a and the assigment of this to b, followed by the reassignment of a as 2.

Multiple Assignment#

Python allows multiple variables to be assigned in the same statement:

a = b = c = 21
print(a, b, c)
21 21 21

Empty Variables#

A variable may also be set to contain no value by assigning it None.

a = None
print(a)
None