SypherTec
…
PART 1 · 02 OF 074 MIN READUPDATED 19 NOV 2026

Variables: Giving Your Values Names

Your tracker prints a title and a month, typed straight into each print. This lesson stores them under names first. Once a value has a name, your program can reuse it and change it.

The concept

Here is your tracker as the last lesson left it. Run it:

WHERE YOU LEFT OFF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

The "where you left off" boxes show the starting version. Your own tracker, with your name in it, is safe in the exercise at the end of this page.

Now run this:

ONE VALUE, PRINTED TWICE · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

You typed November 2026 once, and it printed twice. The first line stored the string under the name month. Each print(month) looked that name up and printed what it held. A name that holds a value is called a variable.

A variable can be given a new value. Run this:

A NAME GETS A NEW VALUE · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Two different lines from the same print(status). The third line replaced the value stored under status. Python runs top to bottom, so the first print ran before the change and the second ran after it.

The syntax

month = "November 2026"

The name goes on the left, the value goes on the right, and = sits between them. Storing a value this way is called assignment.

In Python, = does not mean "is equal to". It is an instruction: store the value on the right under the name on the left. That is why the order matters. "November 2026" = month is an error, because a string cannot be used as a name.

To use a variable, write its name without quotes. print(month) prints the value stored in month. print("month") prints the word month, because the quotes make it a string.

Names follow three rules:

  • Use letters, digits and underscores only. No spaces, no dashes.
  • Do not start with a digit. month2 works. 2month does not.
  • Capitals count. month and Month are two different names.

Python programmers write names in lowercase, with underscores between words: bus_fare, data_bundle. This style is called snake case. Python does not require it, but every example in this track uses it.

Blank lines between groups of code change nothing. They are there to make the code easier to read.

A minimal working example

main.py
title = "Ada's Money Tracker"
month = "November 2026"
 
print(title)
print(month)

Two variables, assigned at the top, used by the prints below them. The output is the same as typing the text into each print. The difference is that the text now has a name.

Common mistakes

You use a name before the line that assigns it. Run this:

A NAME USED BEFORE IT EXISTS · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
NameError: name 'month' is not defined

Python runs line 1 first. At that moment nothing has been stored under month yet. "Not defined" means "no value has been assigned to this name so far". Move the assignment above the line that uses it.

You put a space or a dash in a name. my month = "November 2026" gives SyntaxError: invalid syntax. this-month = "November 2026" gives SyntaxError: cannot assign to expression here, because Python reads the dash as a minus sign. Use an underscore: this_month.

You start a name with a digit. 2026month = "November" gives SyntaxError: invalid decimal literal. Python saw digits and expected a number. Put the digits later: month_2026.

You change the case of one letter. Run this:

ONE CAPITAL LETTER · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output
NameError: name 'Month' is not defined. Did you mean: 'month'?

To Python, Month is a different name, and nothing was assigned to it. Read the end of the error: Python spotted the near match and suggested it.

Try it yourself

You will move the text in your tracker into variables. The output should stay exactly the same while you do it. That is how you know the variables hold the right values.

  1. Run the starting code below. Confirm the console shows your title, the month, a blank line, then Nothing spent yet.
  2. Add a new first line: title = followed by your title in quotes, exactly as it appears in your first print. Change that print to print(title). Run it. Confirm the output has not changed.
  3. Under the title line, add month = "November 2026", or your own month. Change the second print to print(month). Run it. Confirm the output has not changed.
  4. Under that, add status = "Nothing spent yet". Change the last print to print(status). Leave a blank line between your three variables and your prints. Run it. Confirm the output has not changed.
  5. Replace a value: on the line after status = "Nothing spent yet", add status = "Spending started". Run it. Confirm the last line now reads Spending started. Then delete that line and Run again to bring Nothing spent yet back.
  6. Break it on purpose: change print(month) to print(Month) with a capital M. Run it. Confirm your title printed, then a NameError that suggests 'month'. Nothing after the error printed.
  7. Fix it: make the M lowercase again. Run it. Confirm all four lines return.
TRY IT YOURSELF · PYTHON
Tab indents. Press Escape, then Tab, to leave the editor.
CONSOLE
no output

Your tracker's text now lives in variables. Every value in it is still text, though. The next lesson adds the first numbers to your tracker: what you spent.

QUIZ · 3 QUESTIONSCheck what stuckStart the quiz
PART 1 · 02 OF 07❮❯