22 min read
Variables and Identifiers
Store values in variables, name them correctly, avoid keywords, and remember that Python is case-sensitive.
Variables
Creating and changing variablesPython
price = 280quantity = 3total = price * quantityprint(total) price = 300 # the value can changetotal = price * quantityprint(total)Expected output
840 900
= is the assignment operator: it stores the value on the right in the variable on the left. It does not mean "equals" as in maths — read it as "becomes". So total = total + 50 means "total becomes its old value plus 50".
Naming Rules (Identifiers)
| Python identifier rules | ||
|---|---|---|
Python has 35 keywords that are reserved for the language itself, including: False, None, True, and, as, break, class, continue, def, del, elif, else, except, for, from, if, import, in, is, not, or, pass, return, try, while, with.
Good Names
Rules decide what is allowed; good style decides what is clear:
- Use meaningful names:
total_price, nottporx. - Use snake_case — lower case with underscores — for variables and functions.
- Use CAPITALS for values that never change:
DISCOUNT_RATE = 0.1.
Case Sensitivity in Action
Two different variablesPython
Total = 500total = 750print(Total, total)Expected output
500 750
An undefined identifier — press Run to see the errorPython
total = 750print(Totl)A misspelt name is a NameError: Python has never seen Totl. This is Python's version of the "undeclared identifier" error you met in the Introduction to Programming course.
Practice
Choose all that apply.
More lessons in Python Programming · Next: Data Types · Previous: Your First Program
