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:
Variable names MUST begin with a letter or an underscore
_
.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
).Variable names may only use letters (a-z and A-Z), underscores (_), and numbers (0-9).
Case matters!
animal
andAnimal
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:
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 ofx
. Similarly,vowel
makes more sense thanv
.Stick to lowercase letters and underscores in the variable name, unless the value is a constant.
Use all UPPERCASE to name constants (e.g.
PI
orSPEED_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.
You can assign multiple values to multiple variables in the same line.
message, num, PI = "Python ROCKS!", 17, 3.14159
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.
- Student_Grade%
- Student_Grade_Percent
- Student Grade Percent
- student_grade%
- student_grade_percent
- student grade percent
- g
Question
Which of the following are legal AND recommended Python variable names? Select ALL that apply.
- Student_Grade%
- Student_Grade_Percent
- Student Grade Percent
- student_grade%
- student_grade_percent
- student grade percent
- g