Variables
Variables are an important topic in programming. They allow us to keep track of data, storing information in a place for us to reference, use, or update as we progress in our code. These variables require us to be set a value equal to it. In order to create a variable in Python, we must set the variable equal to a value initially. This value can be a default value or a value that will be used later. We can think of a variable as storing a value to use later in the program.
Create a Variable
In order to create a variable, it follows this blueprint1:
<variable> = <value>
When we create variables, we must follow a couple rules for the name of the variable. A variable in Python must start with a letter or an underscore. It can contain numbers, but the first character of the variable must be a letter or an underscore. It can have uppercase, lowercase, or digit characters in the variable name. For example, name = "Edward" is a variable with the string "Edward" stored inside it. A bad example of a variable would be 3names = "John". This breaks our rule of the first character has to be a letter or underscore as the first character of the variable is a 3. However, a variable cannot have a space in the name. Variables must be one word. This means there are different methods, or conventions, for us to create meaningful and helpful variable names.
Naming Conventions
When we create variables, we often follow a set naming convention that goes beyond just the rules of variable names. There are a few different naming conventions. These are: snake case, camel case, pascal case, lowercase, and uppercase.
Snake Case
Snake case follows the use of underscores in lieu of spaces between words. An example of snake case is: user_name = "jdoe". Generally, the other characters are in lowercase form. A longer example of snake case could be: a_really_long_variable = True. While this example isn't the most practical, it demonstrates the use of snake case well.
Camel Case
Camel case maintains the single word convention by capitalizing the first letter of every word after the first. An example of camel case is: userName = "jdoe". Notice how the first letter of the first word is lowercase still. Following the longer snake case example, the longer example of camel case would be: aReallyLongVariable = True.
Pascal Case
Pascal case is similar to the Camel case convention. However, instead of keeping the first letter of the first word lowercase, every first letter of each word is capitalized. For example, UserName = "jdoe" uses Pascal Case. The longer example looks like AReallyLongVariable = True.
Lowercase
Lowercase is self explanatory. Every character in the variable name is kept in lowercase. Using the same examples as above, username = "jdoe" and areallylongvariable = True use the lowercase naming convention.
Uppercase
Uppercase is similar to lowercase and is also self explanatory. Every character in the variable name is kept in uppercase. The examples would follow: USERNAME = "jdoe" and AREALLYLONGVARIABLE = True.
When to use each convention
Python's standard library, the functions built in to the language, use the lowercase convention. Constant variables often use the uppercase convention. Classes follow the pascal case convention. Between Camel case and Snake case, those can be used in functions and variables. According to the Python Style Guide2, functions and variables should use snake case. Camel Case can be used when the convention is already being used to maintain consistency.
For consistency, throughout this book, variables and functions created will use snake case. In your own programs, you can use whichever you choose, but be consistent and stick with one.
Throughout this book, we will use this blueprint format for placeholder values. In this instance, <variable> is a placeholder for a variable name. The <value> is a placeholder for a value. This format will be used throughout the book.
The Python Style Guide at https://peps.python.org/pep-0008/#naming-conventions dictate basic rules around how code should be styled.