Variable Naming Conventions

Anything (that isn't a Python keyword like if, else, not, or def) that starts with a letter or an underscore and contains only letters, numbers, and underscores is a legal variable name.

In [1]:
asdfahsldkfjh = 5 #legal variable name

Guidelines: make the variable names comprehensible -- prefer using words to abbreviations and initialisms.

In [2]:
num_coffee_cups = 2 #readable
ncc = 2 #a future reader won't be able to figure it out easily (not moral, like jaywalking)
x = 2   #very rarely justifibale (not really moral at all)

Naming style:

We generally use pothole case (or Snake Case) in Python: lowercase_words_separated_by_underscores. Rarely, CamelCase is used.

There is one exception: constants. One example of a constant is something like the mathematical constant π. Constants are generally written in all-uppercase.

In [3]:
PI = 3.14159265359

Another example of a constant is a value that is not supposed to change. For example, in a program that has to do with EngSci courses, we might set

In [4]:

Python will not prohibit us from changing values defined in all-uppercase:

In [5]:
PI = PI*1.01  #perfectly legal

However, a variable's name being all-uppercase suggests to the programmer that a line like the one above is likely a mistake.