Learn Python
>>> variables_data_types.variables

Variables in Python

#concept

A variable is a name that points to a value stored in memory. In Python you create one simply by assigning a value with = — no separate declaration step, no type keyword required first.

The name itself doesn't hold the value directly; it's a label pointing at an object. That's why the same name can be reassigned to a completely different type later — a variable is just a pointer, not a fixed-type box, as you already saw briefly in the very first example of this course.

#how it works
1

Choose a name

Pick an identifier following the rules from Keywords & Identifiers — letters, digits, underscores, starting with a letter or underscore.

2

Bind it with =

name = value creates the value first, then points the name at it.

3

Python builds the object first

The right-hand side is fully evaluated before the assignment happens, so score = score + 5 computes the new total using the OLD value of score.

4

Reading it later follows the pointer

Using the variable anywhere afterward just retrieves whatever value it currently points to.

5

Reassigning points elsewhere, it doesn’t edit

A new = doesn’t change the old object in place — it points the name at a new object entirely, of any type.

#example
score = 10
print(score)
 
score = score + 5
print(score)
 
score = "ten"
print(score)
Output
10
15
ten
1Line 1 binds score to the integer 10.
2Line 2 prints its current value.
3Line 4 computes score + 5 using the OLD value (10) first, then rebinds score to the result, 15 — the right side always evaluates before the assignment happens.
4Line 7 rebinds score to a completely different type, a string — perfectly legal, since a variable is just a name, not a fixed-type box.
#use cases
Storing user input for later use in a program
Keeping a running total or counter across loop iterations
Naming an intermediate calculation so the following code reads clearly
Holding a reference to any object — a number, a string, a list, even a function — under one convenient name

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.