Skip to content
Happy Programming Guide
Start learning
Python

Python Variables Explained

How variables work in Python: creating them, naming rules, multiple assignment, and the mutable-versus-immutable behaviour that surprises beginners.

A Python variable is a name pointing at a value. You create one by assigning to it — there is no declaration step and no type to write.

Python
city = "Karachi"
population = 15000000
average_rating = 4.6
is_coastal = True

Assignment happens right to left#

Python
total = 10 + 5    # the right side is worked out first, then stored
print(total)      # 15

This is why count = count + 1 makes sense in code even though it is nonsense in algebra. Python has a shorthand for it:

Python
count = 0
count += 1     # same as count = count + 1
count -= 1
count *= 3

Naming rules and conventions#

Rules that Python enforces:

  • Start with a letter or underscore, never a digit
  • Only letters, digits and underscores — no spaces or dashes
  • Case sensitive: Total and total are different
  • Cannot be a keyword such as class, for, lambda or None

Conventions the community follows:

  • snake_case for variables and functions: user_email
  • CAPITALS for values treated as constants: TAX_RATE = 0.17
  • A leading underscore for internal details: _cache

Assigning several at once#

Python
x, y = 3, 4
a = b = c = 0

# swapping without a temporary variable
x, y = y, x

That swap line is a small piece of Python that people genuinely enjoy.

Deleting and checking#

Python
score = 10
print(type(score))     # <class 'int'>
del score
print(score)           # NameError

You rarely need del. type() on the other hand is one of the most useful debugging tools you have — see Python data types explained.

The behaviour that surprises people#

For numbers and text, assigning makes an independent copy in every way that matters:

Python
a = 5
b = a
b = 10
print(a)   # 5 — unaffected

For lists and dictionaries, both names point at the same object:

Python
a = [1, 2, 3]
b = a
b.append(4)
print(a)   # [1, 2, 3, 4] — a changed too

To get a real copy:

Python
b = a.copy()      # or list(a), or a[:]

Global and local#

A variable created inside a function belongs to that function.

Python
counter = 0

def increment():
    counter = 5      # a new local variable, not the outer one

increment()
print(counter)       # still 0

Rather than reaching for the global keyword, return the value instead. Functions that only depend on their inputs are far easier to test and reason about. See Python functions explained.

Questions people ask#

Does Python have constants?

Not enforced ones. Writing a name in capitals is a convention that tells other programmers not to change it, but Python will not stop you.

What is None?

Python’s way of saying “no value”. Functions without a return hand back None. Check for it with if value is None: rather than == None.

Can I annotate types?

Yes: age: int = 30. Python does not enforce it, but editors use annotations to catch mistakes and improve autocomplete. Useful in bigger projects, optional when learning.

Where to go next#

Next lessonPython data types explained

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *