Python Variables Explained

Understand what Python variables are, how they store and reference data, and why Python's variable model is different from other programming languages.

5 min read

Python variables are the names your program uses to hold on to data so it can use that data again later. When you calculate a total, ask for input, or read a file, you need somewhere to keep that result so the rest of the program can reach it. A variable gives that value a label, so you can reuse it without repeating the original calculation or typing the same value in several places.

A Python variable is a name that points to an object. That is a different model from languages like C or Java, where a variable is a fixed box that holds a value of one specific type. In Python, the variable itself carries no type. The object it points to has the type. This is why the same name can hold a number on one line and a list a few lines later, with no special syntax involved.

Every variable must be assigned a value before you can use it. Reading a name that was never assigned raises a NameError. There is no concept of a variable that exists but is empty. A name becomes real only once you give it a value with the assignment operator, the equal sign. The left side names the variable and the right side supplies the value.

How Python variables reference objects

When you write score = 100, Python creates an integer object holding 100, then makes the name score point to that object. The name is a label attached to the object, not the number itself. This becomes clear when one name is assigned to another:

pythonpython
score = 100
high_score = score
score = 200
print(high_score)   # 100

Assigning high_score = score makes both names point to the same integer. Because integers cannot change in place, reassigning score to 200 simply points score at a new object. It does not touch high_score, which still points at the original 100. Lists behave differently. If two names point to the same list and you modify it through one name, the change shows up through the other name too, because there was only ever one list. That distinction matters once you start passing lists into functions or copying them.

Python variables have no fixed type

Python is dynamically typed. You never declare what kind of value a name will hold, because the type belongs to the object, not the name:

pythonpython
value = 42
print(type(value))   # <class 'int'>
 
value = "hello"
print(type(value))   # <class 'str'>

The name value simply points to whatever it was last assigned. This keeps Python code short, since you spend less time writing type declarations, but it also means a typo or a wrong assumption about a value's type will not be caught until the line actually runs. Optional type hints, written as age: int = 30, let editors and tools flag mismatches ahead of time, but the interpreter itself ignores them while the program runs. This tradeoff is worth remembering as you write longer programs: dynamic typing buys speed while you are learning, and careful testing is what keeps that flexibility from turning into hard to trace bugs later on.

Creating a variable

Creating a variable takes one line: a name, an equal sign, and a value. The right side can be a plain value or a full expression, and Python always finishes evaluating the right side before it touches the left side:

pythonpython
username = "alex99"
attempts = 0
total = 10 + 20 * 3      # 70, multiplication runs first
greeting = "Hi, " + username

Each line produces one object and binds one name to it. Because Python resolves the right side completely first, an expression like attempts = attempts + 1 works correctly: Python reads the current value of attempts, adds one, and only then stores the result back under the same name. The right side can be as simple as a literal or as involved as a function call, and Python treats both the same way: finish evaluating everything on the right before the name on the left changes.

Scope: where a variable can be used

Where you create a variable decides where it can be read. A name created inside a function belongs to that function and disappears once the function finishes running. A name created at the top level of a file belongs to the whole file and can be read from anywhere below it. This is the version of scope you need for now: keep function-only names inside their function, and treat top-level names as available everywhere in that file. The fuller rules, including how nested functions share access to enclosing names, matter once you start writing functions of your own.

Memory cleanup happens automatically

Python manages memory through reference counting and a garbage collector, so you never call a cleanup function yourself. Every time a variable is created, Python allocates space for the object it points to. Once no variable anywhere in the program still points to that object, Python recognizes it as unreachable and reclaims the memory behind the scenes, including cases where two objects reference each other but nothing else references either of them.

This is why beginner Python programs can create variables freely without thinking about memory. It stays invisible until you write much larger programs, at which point understanding reference counting helps you reason about performance rather than correctness.

What to learn next

Variables are the foundation everything else in Python builds on. The next articles walk through creating and assigning variables in more depth, the naming rules Python enforces and the conventions the community follows, how to reassign a variable once its value needs to change, and how to assign several variables at once in a single line. Each one builds on the same idea introduced here: a variable is a name pointing at an object, and once that idea feels natural, the rest of Python's syntax around variables will make sense on sight instead of needing to be memorized separately.

Rune AI

Rune AI

Key Insights

  • Python variables are names that reference an object in memory, not boxes that store a value directly.
  • Python is dynamically typed, so a variable does not need a declared type.
  • A variable must be assigned before it can be used, or Python raises a NameError.
  • Assigning one name to another shares a reference to the same object until one name is reassigned.
  • Memory used by an object is reclaimed automatically once no variable points to it.
RunePowered by Rune AI

Frequently Asked Questions

Do I need to declare variable types in Python?

No. Python is dynamically typed, which means a variable can hold any type of value and you do not need to declare the type before using it. Python figures out the type when the program runs.

What happens if I use a variable before assigning a value?

Python raises a NameError. Every variable must be assigned a value before you can use it. There is no concept of an empty or undefined variable in Python.

Are Python variables the same as in C or Java?

No. In Python, variables are names that reference objects in memory. In languages like C, a variable is a fixed memory location that stores a value directly. Python's model is closer to reference variables in Java, but without type declarations.

Conclusion

A Python variable is a name that points to an object in memory. Understanding that variables are labels, not boxes, helps you avoid confusion when working with mutable types and multiple assignments.