A variable is a name for a value
Programs compute in steps, and a step is useless if the next line cannot reach its result. Variables exist so a result can be kept and reused: a game keeps a score, a shop keeps a running total, a loop keeps its place. Without them, nothing in a program could accumulate or change over time.
A variable gives a value a name so you can use it later. The = sign means assign: take the value on the right, attach the name on the left.
score = 10Now the name score refers to the value 10. Use the name anywhere you would use the value. Assigning again replaces what the name points to:
score = 10 score = score + 5 # compute 10 + 5, then point score at the result
Naming rules: letters, digits, and underscores, and a name cannot start with a digit. By convention Python names are lowercase with underscores, like total_price.
A comment starts with #. Python ignores everything after it on that line. Comments explain why code exists, for the humans reading it.
A variable changing value between prints
The same name is printed twice with a reassignment in between, so the two output lines differ. The comment line contributes nothing at all.
score = 10 print(score) score = score + 5 print(score) # this line is a comment, Python skips it name = "Ada" print(name, score)
Output
10 15 Ada 15
The line score = score + 5 reads the current value, adds 5, and points the name at the result, which is why the second print shows 15. The comment produced no output because Python ignores everything after #. The final print passes two values separated by a comma, and Python joins them with a single space.
Assignment copies a value, it does not create a link
This behavior catches out most beginners. An assignment computes its right-hand side once, at the moment it runs, and stores the result. It does not remember the formula.
price = 40 tax = price * 0.1 total = price + tax print(total) price = 50 print(total)
Output
44.0 44.0
Both prints show 44.0, even though price changed to 50 in between. The value of total was worked out while price was still 40, giving 44.0, and that number is what the name now holds. Changing price afterwards has no way to reach back and recompute anything, so recalculating total would require running its assignment again.
After x = 4, then x = x * 2, then x = x + 1, the value of x is 9.
Each assignment replaces the old value with a newly computed one. x starts at 4, x * 2 makes it 8, and x + 1 makes it 9. Reassigning a name like this is completely ordinary, and it is exactly what makes a variable variable.
This snippet builds a value up in stages and then labels it on the way out. The second line follows the score = score + 5 pattern, reading steps and pointing the same name at the larger result.
steps = 8000 steps = steps + 2500 print("Steps today:", steps)
Output
Steps today: 10500The single print receives two values separated by a comma, a piece of text and a number, and Python inserts a space between them automatically. That is why the output reads Steps today: 10500 without a space needing to be written inside the quotes.