Skip to content

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 = 11 lets later code write cartons instead of 11.
  • Assignment gives the name on the left the value worked out on the right, so you write seats = 8, and 8 = seats is 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.
Lessons in Simple Python programs7 lessons · 29 min

Keep going with Python

We build you a study plan for each day. It fits your time and starts at this chapter.

Build my plan

Start free, no account needed. See prices