← Python Crash Course

PCC·02 · Chapter record

Variables & Simple Data Types

Chapter 2 — the data you'll work with in every program

Done

Five sessions covering the data types you'll use constantly: variables, strings, numbers, and the comments that explain them. Each one explains the concept, lists the key terms, then gives you an exercise — try it yourself before revealing the code.

2.1

Variables & the Traceback

  • A variable is a name connected to a value.
  • It's often described as a box you store a value in, but it's more accurate to think of it as a label attached to a value — you can move the label to a new value at any time.
  • Variable names can only contain letters, numbers, and underscores, and can't start with a number.
  • Spaces aren't allowed — use underscores instead — and you should avoid Python's reserved keywords and built-in function names like print.
  • Misspell a variable name Python hasn't seen before, and you get a NameError, shown as a traceback pointing at the exact line.
variable
a name connected to a value
NameError
raised when Python can't find a variable or function by the name you used
traceback
the report Python prints showing where and why an error happened
Activity 1

Assign a greeting to a variable, then print that message.

Activity 2

Print that same variable, then reassign it to a new message and print it again.

▶ Variable Name Checker

▶ Traceback Reader