What Are Variables?
Exit

What Are Variables?

Learn how variables store and name data in your program

Right now, every piece of data in your quiz — the question text, the options — lives buried inside print() calls. If you want to change the question, you have to hunt through the code to find the exact string. If you want to use the question text in two different places, you have to type it twice.

Variables solve this. A variable gives a value a name. You define the name once, assign it a value, and then use that name anywhere in your code.

Assigning a variable

The = sign assigns a value to a name:

city = "Tokyo"
population = 14000000
temperature = 36.6

The name is on the left. The value is on the right. This is not a math equation — it means "store this value under this name."

Three types you'll use

Python figures out the type from the value you assign — you don't declare it separately.

  • str (string): text in quotes — city = "Tokyo"
  • int (integer): whole numbers — population = 14000000
  • float: decimal numbers — temperature = 36.6

Naming rules

Variable names must follow these rules:

  • Start with a letter or underscore
  • No spaces — use underscores for multi-word names: correct_answer, not correct answer
  • Case-sensitive: Score and score are different variables

Use descriptive names. question is better than q. option_1 is better than a.

In the next chapter, you'll move the quiz question out of print() and into a variable.

Next Chapter →