Variables

As with most programming languages, Python has variables. Variables are essentially storage locations for things we wish to store.

Python is a high-level programming language. This means that unlike C (a low-level language), as programmers we’re less concerned with the more nuanced details of our code. An example.

In C, our program’s variables can be integers (int), characters (char), floating point decimals (float), etc. To declare a variable of one of these types, we first specify the type, give it a name, and can then assign a value. Like so:


int x;

int z = 5;

Observe that in Line 1, we created an integer variable (x), but left it unassigned. On Line 2, we created an integer (z) and assigned it the value 5

Now, let’s see how variables are created in Python.

z = 5

The first difference is obvious. In Python, we don’t specify the type of our variable. This is all taken care of; we simply assign a value to the name. The second difference is there’s no such thing as an unassigned variable. We can’t just write down “x” and leave it unassigned.

Moreover, the same variable can be assigned to different types with no specification. All of the following are valid.

z = 5 
z = 'a'
z = 3.7

Next, we’ll look at the basic constructs of Python that allow us to manipulate our variables and accompish tasks.