6  Variables

In Principles of Programming, we talked about variables. We said these are essentially boxes that store information of a certain type so the program can refer to it later.

In many programming languages, we need to create a variable by declaring it. This means specifying its type alongside its name.

In Python, things are much more laid back. Variables are dynamic, so we simply assign a value to a name to create the variable, and the type will be automatically set based on the Value we provide. We assign values in Python using the assignment operator =. It basically says “Let this have a value of this”.

This can cause confusion if you’re new to coding. = does not mean ‘equals’.

What will be the variable type of each of these variables after assignment?

Because variables in Python are dynamic, their type will change if we give it a value of a different type :

6.0.1 Dealing with spaces in names

In programming languages, a space indicates a separation between instructions, values etc. So if we want to name something (like a variable) with multiple words then we can’t use spaces. There are two principle conventions for how we deal with this :

You can use whichever you prefer (though ensure you’re consistent) but snake_case is generally preferred for (and recommended by the developers of) Python.

6.0.2 Variable Types - Single Items

Let’s remind ourselves of some of the main types of variable, and see what they look like in Python.

6.0.2.1 Numbers

Integers (int) are whole numbers Floating point numbers (float) are numbers with up to 15 decimal places

6.0.2.2 Text

Strings (str) are sequences of characters denoted using “ or ‘ (Note - Python does not have a separate variable type for characters, they are just strings of length 1)

6.0.2.3 Truth

Booleans (bool) take one of two values to indicate whether they are True or False.

6.0.3 Variables that store Multiple Items

6.0.3.1 Lists

Lists (list) are sequences of items, where the order matters. Duplicates allowed.

6.0.3.2 Sets

Sets (set) are unordered sequences of unique items.

Tip

You won’t come across sets too often in Python - so don’t worry about them too much for now!

6.0.3.3 Tuples

Tuples (tuple) are ordered sequences (like a list) but are immutable (once created, they cannot be changed)

Tip

You won’t come across tuples too often in Python - so don’t worry about them too much for now!

6.0.3.4 Dictionaries

Dictionaries (dict) are unordered collections of key-value pairs