Kahibaro
Discord Login Register

What is a variable?

Understanding Variables

In Python (and in programming in general), a variable is a way to store a value in memory and give that value a name so you can use it later.

You can think of a variable as:

You decide the name; Python remembers the value for you.

Real-life analogy

Imagine you have sticky notes and you put them on different items:

You can now say, “What is the price?” or “What is the message?” without pointing directly to the object. The labels stand for the actual things.

Variables work the same way: the name (like price) stands for the value (like 9.99).

Variables as names for values

In Python, when you create a variable, you:

  1. Choose a name.
  2. Assign a value to that name.

The variable name becomes a reference to that value. You no longer have to remember the number or text itself; you just use the name.

For example, if you want to remember a user’s score in a game, you might have a variable called score. Everywhere in your program where you need the score, you use score instead of the actual number.

Why variables are useful

Variables allow you to:

Without variables, you would have to work only with raw numbers and text, which would be confusing and nearly impossible for anything beyond the simplest tasks.

Variables and memory (conceptual view)

You don’t need to manage memory yourself in Python, but it helps to have a simple picture:

You don’t see the memory addresses or low-level details; you only work with the names.

Values can change, names stay the same

The value stored in a variable can be changed, but the name stays the same.

Conceptually:

  1. You start with a box labeled temperature containing 20.
  2. Later, you erase 20 and write 25 in the same box.

The label temperature still exists, but it now refers to a different value.

This idea—that a name can refer to different values at different times—is part of what makes variables powerful.

Variables and data types

Every variable in Python refers to a value that has a type (such as a number, text, or boolean). The value’s type determines what you can do with it (for example, you can add numbers but you join pieces of text together in a different way).

In this chapter section, focus on the idea that:

Variables as building blocks

Almost everything you do in Python will involve variables:

Thinking clearly about what information your program needs and what to name the variables that store it is a core programming skill.

To summarize:

Views: 23

Comments

Please login to add a comment.

Don't have an account? Register now!