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, and will make it much easier for others to read your code as well.

In Python, most of your code will be lowercase, so make sure that when you're typing out functions you're keeping the first letter lowercase. 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". As long as your variables have names that tell you clearly what they do, you'll be fine. 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.

X = "Hello"

In this example, X is the variable that will represent our string, "Hello". Our string is stored within this variable. You can think of this as if we have a box that we have written "X" on and we've stored the word "Hello" within it. Or, you can think of X as a nickname for "Hello"- when we call for X, "Hello" will show up. But what if we wanted X to represent multiple strings, like this?:

X = "Hello"
X = "Goodbye"
(print(X))

If we were to run this code, we would only see "Goodbye" displayed on the screen. This is because each variable can only represent one thing at a time, so it will represent the most recent data that was assigned to it. But, if we did this:

X = Sam
print("Hello, " + X)
X = Bob
print("Hello, " + X)

We would get this output:

Hello, Sam
Hello, Bob

This is because we switched what X meant halfway through our code. So, think of "=" as meaning "contains/holds" or "is the nickname of", like this:

Y = "No" is like saying that Y holds/contains "No" (where Y is our box and "No" is what we're putting inside that box)

X = "Hi" is like saying that X is the nickname of "Hi"


You may be wondering, "what's the point in creating variables?". While the usefulness of variables can depend on the project, they're especially useful when you'll need to reuse data multiple times. Let's say we wanted to keep using someone's name in the below print function. We can do it like this:


print("Hello, Joanna")
print("How are you, Joanna?")
print("Joanna?")

But of course, the more complicated the string is that we want to reuse, the more impractical this would become. Instead, we can assign the name Joanna to a variable. Let's try this instead:

name = "Joanna"
print("Hello, " + name)
print("How are you, " + name + "?")
print(name + "?")

Both of these are valid methods that will produce the same results, but the second will become more practical the longer the string is or the more you need to keep reusing it. On top of this, it makes it much easier to update your code- if you decide to change this name to something else, there's no need to manually go back and edit every single instance of it; just change the string that the variable represents. So, you'll want to get comfortable with using variables.


Lesson Recap:

  • Variables can be identified by the equals sign next to them.
  • Variables hold data. The value on the left is like a nickname for our data on the right.
  • Variables can make working with long strings much faster. They also make it much easier to change the string later on.

In the next lesson, we'll learn about Snake case and properly naming variables.