Skip to main content

Variables

Imagine you are playing a video game. The game needs to remember your name, your score, and how many lives you have left.

If the game cannot remember anything, it cannot work.

In programming, we need a way to tell the computer to remember information so we can use it later. This is why we use variables.


1. What is a Variable?

A variable is a name used to store a value.

You give the information a simple name. When you need that information again, you just call its name.

The Storage Box Analogy

Think of a variable like a storage box.

  • The name is the label sticker on the outside of the box.
  • The value is what you put inside the box.

2. Creating a Variable

Let's create our very first variable. We want to store a person's name.

name = "Alice"

Let's understand exactly what this line does:

  1. name is the label we chose for our variable.
  2. = tells the computer: "The value on the right is stored inside the variable on the left."
  3. "Alice" is the information we are putting inside the variable.

(Note: We put words inside quotes "". We will learn more about this in the next lesson!)

3. Using a Variable

To use a variable, we can simply print it out using print().

name = "Alice"

print(name)

Output:

Alice

Why did this happen? When Python sees print(name), it does not print the word "name". It opens the variable named name, finds "Alice" inside, and prints that value!

4. Changing a Variable

Variables are called "variables" because they can vary (change). You can put a new value into the same variable whenever you want.

score = 0
print(score)

score = 10
print(score)

Output:

0
10

When we write score = 10, the computer removes the old value 0 and stores the new value 10 in its place.

5. The Assignment Operator

Now that you know how it works, it is time to learn the official programming term.

In programming, the equals sign = is called the assignment operator.

It assigns the value on the right to the variable on the left. It does NOT mean "equal to" like in math class.

6. Naming Rules

When you create a variable name, Python has a few simple rules you must follow:

  1. Only use letters, numbers, and underscores (_).
  2. Do not start with a number. (1score is not allowed)
  3. Do not use spaces. (user name is not allowed)

7. Python Naming Convention (snake_case)

Sometimes, you need to use two words for a variable, like "player score". Since spaces are not allowed, how do we write it?

In Python, the standard way is to use all lowercase letters and separate words with an underscore (_).

This is called snake_case.

player_score = 100
user_age = 25

Why does Python use snake_case? Because player_score is much easier to read than playerscore. It makes your code clean and simple!

8. Short Recap

  • A variable is a name used to store a value.
  • Use = to assign a value to a variable.
  • Variables can change their values over time.
  • Always use snake_case for multi-word variables in Python.

What's Next?

Now that you know how to store information, let's learn about the different types of information you can store inside a variable in the next lesson!