Variables
Give data meaningful names and trace how a value changes over time.
On this page
The concept
A variable is a name bound to a value. The assignment operator = evaluates the expression on its right and binds the name on its left to the result. In age = age + 1, Python reads the old age first, calculates a new integer, then updates the binding. This is not a mathematical equation.
Choose names that describe the role of data, such as student_name and unit_price. Python names are case-sensitive. The conventional spelling for ordinary variables is snake_case. An uppercase name such as MAX_ATTEMPTS signals a value intended to be treated as constant, but Python does not prohibit reassignment.
Worked example
student_name = "Avan"
age = 19
age = age + 1
city, country = ("Erbil", "Iraq")
MAX_ATTEMPTS = 3
print(student_name, age)
print(city, country)
print("Maximum attempts:", MAX_ATTEMPTS)Trace the result
- The student name and age are independent bindings; their values need not have the same type.
- The age starts at 19 and becomes 20 after reassignment. The earlier value is not changed retroactively in output already printed.
- The paired assignment binds city and country to two values. MAX_ATTEMPTS is a naming convention rather than a special declaration.
Avan 20
Erbil Iraq
Maximum attempts: 3Line-by-line walkthrough
Line 1: student_name = "Avan"
Evaluate 'Avan', then bind or update student_name with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Line 2: age = 19
Evaluate 19, then bind or update age with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Line 3: age = age + 1
Evaluate age + 1, then bind or update age with the result. Assignment itself does not imply a deep copy.
Line 4: city, country = ("Erbil", "Iraq")
Evaluate ('Erbil', 'Iraq'), then bind or update (city, country) with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 5: MAX_ATTEMPTS = 3
Evaluate 3, then bind or update MAX_ATTEMPTS with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Line 6: print(student_name, age)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 7: print(city, country)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 8: print("Maximum attempts:", MAX_ATTEMPTS)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
Do not name a variable print, list, str, or input: doing so hides a useful built-in name and can cause confusing errors later.