4.4. More on Variables

The previous section covered the big picture for variables–creating, evaluating, and reassigning. This section deals with some of the finer points for variables.

4.4.1. Naming Python Variables

Just like the print function must follow specific syntax rules, variable names in Python have their own set of rules:

  1. Variable names MUST begin with a letter or an underscore _.

  2. Variable names CANNOT contain spaces. If you have more than one word in a name, connect the words with underscores (e.g. price_of_eggs).

  3. Variable names may only use letters (a-z and A-Z), underscores (_), and numbers (0-9).

  4. Case matters! animal and Animal are different variable names.

In addition to these rules, there are also some conventions. These are habits that Python programmers follow to keep their code consistent with others’ across the world. You do not have to follow these suggestions, but you really SHOULD:

  1. Use names that clearly describe their values. For example, if you need to store the price of eggs, call your variable price_of_eggs instead of x. Similarly, vowel makes more sense than v.

  2. Stick to lowercase letters and underscores in the variable name, unless the value is a constant.

  3. Use all UPPERCASE to name constants (e.g. PI or SPEED_OF_LIGHT).

4.4.1.1. Compare

Writing good code is about more than just solving the task at hand. It also includes making the code easy to read, update, and maintain.

Consider these examples of variable names:

Example

Weaker variable names:

1
2
3
4
x = 5
y = 3.14
z = y * x ** 2
print(z)

What is this program doing? Hard to say. The variable names x, y, and z don’t tell us anything about how they are used.

Let’s look at an improved version of this program.

Stronger variable names:

1
2
3
4
radius_of_circle = 5
PI = 3.14
area_of_circle = PI * radius_of_circle ** 2
print(area_of_circle)

With improved variable names, it becomes clear that the program is calculating the area of a circle of radius 5. Here’s another benefit of descriptive variable names. Although we have not yet learned what the symbols * and ** do on line 3, the variable names give us an idea of what the symbols mean.

4.4.2. Keywords

The following code produces an error:

print = "Hello, World!"

The problem here is that print is a Python keyword—a word reserved by the language for its own use. print does something very specific, so we cannot use it to store a value.

Tip

Most code editors will highlight keywords in a different color than variables or other parts of your code. This gives you a visual clue about which words are reserved for use as keywords.

There are a little over 30 keywords in Python. You can find a complete list of them here.

4.4.3. Cool Tricks

Finally, here are a couple of nice details that save you a few lines of code.

  1. You can assign multiple values to multiple variables in the same line.

    message, num, PI = "Python ROCKS!", 17, 3.14159
    
  2. You can assign the same value to multiple variables all at once.

    word_1 = word_2 = "same"
    

4.4.4. Check Your Understanding

Question

Which of the following are legal Python variable names? Select ALL that apply.

  1. Student_Grade%
  2. Student_Grade_Percent
  3. Student Grade Percent
  4. student_grade%
  5. student_grade_percent
  6. student grade percent
  7. g

Question

Which of the following are legal AND recommended Python variable names? Select ALL that apply.

  1. Student_Grade%
  2. Student_Grade_Percent
  3. Student Grade Percent
  4. student_grade%
  5. student_grade_percent
  6. student grade percent
  7. g