Python Lesson 5: Snake case

Snake case (or snake_case) is a naming convention in Python where you write variable names in lowercase and seperate words with an underscore instead of a space. For example, instead of naming a variable "First Name", you would name it "first_name". This keeps everything neat and uniform, which will make it much easier for others (and you!) to read your code.

In Python, your functions will be kept lowercase, so make sure that when you're typing them out you're keeping the first letter lowercase as well. For example, if you were to run the following function:

Print(1)

You'd get the following error code:

Traceback (most recent call last):
File "", line 1, in
Print(1)
NameError: name 'Print' is not defined. Did you mean: 'print'?

This error code was caused by us capitilizing the first letter in the print function, which made Python unable to read it. Keep it lowercase, like this:

print(1)

Another important thing to note when naming your variables is to make sure you're choosing short and descriptive names. Below is an example of what not to do:

NOC = 7

The name we've chosen isn't very helpful. If anyone else reads or edits our code, they won't be able to decipher what it stands for. Plus, we'll probably forget what it stands for, too! Instead, we should name it like this:

number_of_cats = 7

Now it's easy for everyone to tell what the number 7 represents. If we wanted, we could shorten the name even further to something like num_of_cats. Variable names can only contain numbers, letters, and underscores. So, if you're getting an error, double check that you're not using any special characters in the name.


Lesson Recap:

  • Variable names should be short and descriptive.
  • Variable names can only contain numbers, letters, and underscores.
  • Keep functions lowercase to prevent errors.

In the next lesson, we'll look at how Python handles mathematic equations.