Variable Naming Conventions

We know there are rules around which variable names are legal. But there are still some ‘rules’ around how to choose good variable names. This is called a Naming Convention.

Here are the ‘rules’:

  • Variables should always start with a lower-case character
  • Use underscores instead of spaces to separate words
  • Don’t use upper-case characters
  • Starting with underscores is ok, but is used for a special purpose

Examples

ExampleConventional?Notes
my_variableyesUse underscores for spaces between words
MyvariablenoVariables shouldn’t start with an upper-case letter
myVariablenoVariables shouldn’t use upper-case characters
_my_variablepossiblyStarting with an underscore is ok, but it’s used for a special purpose

Do Variable Naming Conventions Matter?

Does it even matter if we follow convention? Not to Python! Python will not make us follow convention at all. It only cares if variable names are legal.

It’s not important to Python, but it is important to us, the programmer. It’s especially helpful for other programmers when they read our code.

Naming Conventions make our code easier to read

Know your variable naming conventions

Did You Know?

Other programming languages also have naming conventions. Each language has its own rules to follow

For example, JavaScript has a convention called camelCase