A variable name is the label you choose for a stored value. Python has naming rules, and programmers use simple habits to make names readable.
A name may contain letters, numbers, and underscores. It cannot begin with a number. It cannot contain spaces or hyphens.
favorite_color = "green"Line-by-line explanation
favorite_coloruses letters and an underscore, so it is valid.- The underscore joins two words because spaces are not allowed.
"green"is stored under that name.
student_age = 12Line-by-line explanation
student_ageclearly describes the information.=stores a value.12is the stored value.
Tiny Practice: Improve a Name
Replace x with book_title everywhere.
Starter template
x = "The Hobbit"
print(x)Line-by-line explanation
- The first line stores a title under an unclear name.
- The second line reads that same variable.
Hint
- Change the name on both lines.
- Use an underscore.
- Do not change the text.
Show Solution
book_title = "The Hobbit"
print(book_title)Line-by-line explanation
- The first line uses a descriptive name.
- The second line uses the exact same spelling.
- The output remains
The Hobbit.