Variable assignment
Variable assignment gives a value a name that can be used in later expressions.
Carlos writes two packing calculations in Python: 11 * 4 and 11 + 2. Both use his carton count, 11. If the count changes, he has to hunt down every 11. How could he name that count once and use the name instead?
Start the lesson
4 minutes. No account needed.
What you'll learn
- A variable is a name that refers to a value, so
cartons = 11lets later code writecartonsinstead of 11. - Assignment gives the name on the left the value worked out on the right, so you write
seats = 8, and8 = seatsis an error. - Python looks up a name only when it has no quotes, while the same word in quotes is just a string of text.
- Python runs lines from top to bottom, so a name must get its value before a later line uses it.