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 "
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"
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
We would get this output:
Hello, Sam
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"
name = "Joanna"
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: In the next lesson, we'll learn about Snake case and properly naming variables.
X = "Goodbye"
(print(X))
print("Hello, " + X)
X = Bob
print("Hello, " + X)
Hello, Bob
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?")
print("Hello, " + name)
print("How are you, " + name + "?")
print(name + "?")