Python Variables

Tutorial 4 of 65 · pythondeck.com Python course

A variable in Python is a name bound to an object. The object holds the type, not the name; the same name can be rebound to any kind of value. Names should be snake_case, start with a letter or underscore and may not collide with reserved keywords. Multiple assignment and tuple unpacking are first-class features.

Variables are names bound to objects in memory. Python is dynamically typed: the name does not carry a fixed type declaration; the object does. Assignment creates or rebinding, not a box that only holds ints.

Understanding reference semantics prevents surprises when two names share one list, or when reassigning a name does not mutate the object another name still references.

Assignment with = binds a name on the left to the object on the right.

Multiple assignment: a, b = 1, 2 and swapping with a, b = b, a.

Augmented assignment (+=) can differ from x = x + 1 for mutable types.

Naming conventions: snake_case for variables and functions, UPPER for constants.

The del statement removes a name binding; it may not destroy the object if other references exist.

Builtins like len can be shadowed accidentally by assigning to the same name.

Python has no variable declarations in the C sense. Local names are determined at compile time inside functions; assigning to a name makes it local unless you use global or nonlocal.

Immutable objects (int, str, tuple) are safe to share; mutating through one name on a list affects all names referencing that list. Copy with list.copy() or copy.deepcopy when needed.

Constants are a convention: ALL_CAPS signals do not reassign, but the language does not enforce it.

Using == to compare to None instead of is None when identity matters.

Expecting x += [1] on a default function argument to be safe—it mutates shared state.

Reusing names for unrelated concepts (list, dict, id).

Assuming assignment copies lists or dicts deeply.

Choose descriptive names; avoid single letters except in trivial loops.

Initialize variables before branches that read them.

Use unpacking and enumerate instead of manual index variables when possible.

Treat function defaults as immutable sentinels: use None and create mutable objects inside the function.

Re-read the examples below with these ideas in mind; change variable names and inputs to match your own project.

The program below demonstrates assignment. Read the comments on each line, run the code, then change names or values to see how the output shifts.

# Example: Assignment
# Run in the REPL or save as a .py file and execute with python.
x = 10
y = "hello"
z = [1, 2, 3]
print(type(x), type(y), type(z))

This sample walks through multiple assignment in a small, runnable script. Paste it into the REPL or save it as a .py file before you continue to the next block.

# Example: Multiple assignment
# Run in the REPL or save as a .py file and execute with python.
a, b, c = 1, 2, 3
x = y = 0
print(a, b, c, x, y)

Here is a hands-on illustration of swap values. Follow the inline comments first; only then execute the snippet and compare the result with what you expected.

# Example: Swap values
# Run in the REPL or save as a .py file and execute with python.
a, b = 5, 9
a, b = b, a
print(a, b)

The program below demonstrates reference sharing. Read the comments on each line, run the code, then change names or values to see how the output shifts.

# Names point at objects; assignment does not copy lists
original = [1, 2, 3]  # one list object in memory
alias = original  # second name references the same list
alias.append(4)  # mutation visible through both names
print(original, alias)  # both show four elements
rebound = original  # prepare to rebind only one name
rebound = [9, 9]  # new list; original unchanged
print(original)  # still [1,2,3,4]
print(rebound)  # [9, 9]
x = y = 0  # chained assignment binds two names to one int
y += 1  # ints are immutable; x stays 0, y becomes 1
print(x, y)  # 0 1

This sample walks through unpacking swap in a small, runnable script. Paste it into the REPL or save it as a .py file before you continue to the next block.

# Tuple unpacking assigns multiple names in one statement
first, second, third = 1, 2, 3  # parallel assignment from tuple
print(first, second, third)  # 1 2 3
first, second = second, first  # swap without a temp variable
print(first, second)  # 2 1
data = "a,b,c"  # comma-separated string
parts = data.split(",")  # list of three strings
a, b, c = parts  # unpack list into three variables
print(a, b, c)  # a b c
point = (10, 20)  # tuple coordinates
x, y = point  # unpack works on any iterable of right length
print(x + y)  # 30

« Python Syntax All tutorials Python Data Types »