LESSON 02

Comments & Variables

Leave notes for humans. Give information a place to live.

USES: print( ), quotation marks.

01

Two new tools

Programs need to remember things, and humans need notes. Comments are notes for people reading your code. Variables are how your code remembers information. Two small tools, and you will use both in every program you ever write.

BLUEPRINT · TAP OR HOVER EACH PIECE

Blueprint 1 of 2: the comment. Tap each piece.

02

Comments: notes for humans

A comment is a note written inside your code for people, not for Python. Think of a note in the margin of a textbook: it helps you understand the page, but it is not part of the book. When Python meets a #, it ignores the rest of that line. Future you will be grateful for good comments. Future you may also wonder what past you was thinking. Both are normal.

INPUT# This is a comment print("Hello World!")
OUTPUTHello World!
TIPA # only silences the rest of ITS OWN line. The next line is normal code again, so a comment on a new line needs its own #.
CRASHES# a comment print("runs!") This line crashes
SAFE# a comment print("runs!") # this line is safe

BLUEPRINT · TAP OR HOVER EACH PIECE

Blueprint 2 of 2: the variable. Tap each piece.

03

Variables: labelled boxes

A variable is a labelled box. The label stays; the contents can change. That is literally why they are called variables: their value can vary. Assign a second time and the old contents are replaced, not added. Press the button below to watch the box change.

INPUTname = "Alex" print(name) name = "Sam" print(name)
OUTPUTAlex Sam
name(empty box)
INPUT(nothing run yet)

04

Boxes can hold more than text

So far our box holds text. Boxes can also hold whole numbers and decimal numbers. You do not need the technical names yet; those come in Lesson 3. For now, just see it: same label-and-value idea, different kinds of contents.

INPUTname = "Alex" age = 20 height = 1.75 print(name) print(age) print(height)
OUTPUTAlex 20 1.75

05

Naming your boxes

Rules: a name cannot start with a number; no spaces or hyphens (use underscores); upper and lower case are different boxes (name and Name are two boxes); and you cannot use Python's own special words like if or for as names. Advice: name boxes after what they hold. x, y, z tell nobody anything, while age, height and name tell the story at a glance.

INPUTfavorite_color = "Blue" # good 2name = "bad" # starts with a number my-name = "bad" # hyphen is not allowed

06

Quick quiz: which name is valid?

Only one of these is a legal variable name. Decide first, then check.

07

Putting them together

Comments explain, variables store, print shows. Each part has one job. This exact trio is how real programs are written, just with more boxes and more notes.

INPUT# Store the student's name name = "Alex" # Store the student's age age = 20 print(name) print(age)
OUTPUTAlex 20
Report a mistake or suggest a lesson