LightMySky

Variables and Assignment in Text Code

Storing a value under a name in written code, then replacing it later. The name holds whatever was last assigned to it, which is how a program remembers anything.

No account needed. Progress saves in this browser.

What a learner can do afterwards

  • Assign a value to a name and print it back out
  • Predict what a name holds after two assignments in a row
  • Pick names that say what the value means instead of single letters

1 · Read

A variable is a named box holding one value. You create it with an assignment: the name on the left, the value on the right. In text code you write score = 0, and from then on score means 0. Here the single = does not ask whether two things are equal. It stores.

Try it together

Programs remember by reassigning. Start with score = 0, then later write score = 5 after a goal. The same name now holds 5, and the old 0 is gone for good. To show a value, print it back out.

Values come in kinds. Text sits in quotes, like name = "Lia", while numbers stand bare, like age = 14. In Python, True and False start with a capital letter and take no quotes. Each kind behaves differently when your program uses it.

Pick names that say what the value means. A name like player_score beats x every time, because future you will read this code. Short single letters hide meaning, while clear lowercase names reveal it.

Store a value under a clear name, print it to show it, and reassign to change it.

2 · Watch

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Then practise

8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.

Spotted a problem on this page? Tell us
Variables and Assignment in Text Code · Computing, ages 14 to 15 · LightMySky