Table of Contents
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:
- A labeled box where you store something.
- A name that points to a piece of data in the computer’s memory.
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 write
priceon a sticky note and put it on a product. - You write
ageon a sticky note and put it on a form. - You write
messageon a greeting card.
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:
- Choose a name.
- 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:
- Store information: like a user’s name, age, or a calculation result.
- Reuse information: use the same value in many places in your code.
- Change information over time: update a score, a balance, or a status.
- Make code readable:
total_priceis easier to understand than just seeing42.75scattered in the code. - Work with unknown values: you can write programs that work with any input by storing that input in variables.
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:
- The computer has a big storage area called memory.
- When you assign a value to a variable, Python stores that value somewhere in memory and remembers it.
- The variable name acts like a label that points to that stored value.
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:
- You start with a box labeled
temperaturecontaining20. - Later, you erase
20and write25in 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:
- A variable is a name.
- That name is associated with some value.
- The value has a type (explored in the rest of the Variables and Data Types chapter).
Variables as building blocks
Almost everything you do in Python will involve variables:
- Storing user input.
- Keeping track of scores, totals, and settings.
- Holding temporary results while performing calculations.
- Remembering configuration or options for your program.
Thinking clearly about what information your program needs and what to name the variables that store it is a core programming skill.
To summarize:
- A variable is a named reference to a value.
- It lets you store, read, and update information while your program runs.
- Good variable names make your code easier to understand and work with.