In the previous section, we saw something similar to the following come up repeatedly:

a = 4

This is what is known as a variable assignment.

What is a Variable?

A variable is a name given to some space in memory in Python, that can store any value. Python is a "dynamic" language, which means that a variable can store any type of data within itself. For example,

a = 4
a = 4.0
a = "four"

All of these are valid assignments of the variable a. The name "variable" should hopefully be slightly insightful as well. While our numbers like 4 and 8 are fixed, something like a in the above case can change its value over the course of our program β€” it can vary, and is therefore vary-able.

Assignment Statements, and How They Are Executed

In Python, assignment statements are everywhere.

a = 3 + 5 - 7

What Python does during these statements is first analyze the right hand side of the equals sign, and reduce it to a value. It then sets the variable name on the left-hand side to that value. Here, it first calculates the right hand side to be equal to 1, then sets a to 1.

Variable Naming Convention

You can use numbers, letters or underscores in your variable name. Your variable name must not start with a number, however β€” only with letter or underscore. For example, store_data_1 is a valid variable name. So is _buffer. But 1Cat is not. Moreover, in Python we use the convention for separating out the words of our variables with underscores.

Naming variables is tricky business. Most programmers will attest to spending much time in the grey zone between variables names that don't mean anything (a, for example) and variable names that explain too much (store_for_various_types_of_data). Don't stress yourself out too much about these! Just keep an eye out for names that feel correct, and over the course of this class, we'll end up developing some good intuition for this together.

Last updated