Guide Python Beginner

4.8 Variables

Declaring variables without a separate declaration step, the assignment operator, multiple/chained assignment, and dynamic typing in Python.

2 min read

Declaring Variables

What Is It?

Unlike many languages, Python has no separate declaration step — a variable comes into existence the moment you assign a value to it.

Why Is It Used?

Less boilerplate, faster to write.

How Is It Used?

Just write name = value.

>>> username = "deploy_bot"
>>> username
'deploy_bot'

Assignment

The = operator binds a name to a value (technically, to an object in memory). Re-assigning simply points the name at a new object; it doesn’t modify the old one.

>>> retries = 3
>>> retries = retries + 1
>>> retries
4

Multiple Assignment

What Is It?

Assigning several variables in one statement, either to different values or the same one.

Why Is It Used?

It reduces repetition when initializing related variables together.

>>> a, b, c = 1, 2, 3    # unpack three values at once
>>> a, b, c
(1, 2, 3)

>>> x = y = z = 0        # all three names point at the same value
>>> x, y, z
(0, 0, 0)

Dynamic Typing

Covered in depth in Introduction to Python, Section 1.3 — a variable’s type is simply whatever its current value’s type is, and can change on reassignment:

>>> v = 10
>>> type(v)
<class 'int'>
>>> v = "text"
>>> type(v)
<class 'str'>

Quick Interview Answer

“A Python variable is just a name bound to an object — there’s no separate declaration step, no type annotation required. x = y = z = 0 binds all three names to the same object; a, b, c = 1, 2, 3 unpacks three values in one line. Because a name is just a label, reassigning it to a different type is perfectly legal — that’s what ‘dynamically typed’ means.”

Common Mistakes

  • Assuming x = y = [] creates two separate lists — it creates one list object that both names point to, so mutating it through either name affects both.
  • Forgetting that reassignment doesn’t mutate the old value — it just points the name at a new object, leaving the old one unchanged (and eventually garbage-collected if nothing else references it).

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form