Codectionary / Developer documentation / Python

Variables & Assignment

A variable is a name bound to an object. Assignment evaluates the expression on the right, then binds the name on the left to its result. Python determines types from objects at runtime; you do not need to declare a variable type before assigning it.

How it works

Use names to keep values you will reuse: a customer name, a running total or the result of a calculation. Reading a name before it has been assigned raises NameError. Names are case-sensitive, so score and Score are different names.

Assignment does not copy an object. If first and second refer to the same list, changing that list through either name is visible through both. Reassigning first to a different list changes that binding, not second. Numbers and strings are immutable; updating a numeric name binds it to a new result.

In x, y = 1, 2, the values are evaluated before the targets are assigned. This is why a, b = b, a swaps values. The number of values must match the targets unless you use a starred target. Augmented assignment such as += can mutate a mutable object in place; it is not universally identical to ordinary assignment.

Common mistakes and boundaries

Use descriptive snake_case names and avoid shadowing built-ins such as list or str. A name may contain digits but cannot start with one, and keywords such as for cannot be names.

ALL_CAPS is a convention for constants, not an enforcement mechanism. Rebinding a name to another type is allowed but can make the rest of a function difficult to understand.

Try it yourself

Create a list and give it two names. Append through the second name, then print both. Next reassign the second name to a new list and print both again. Explain which operation changed the shared object.

Syntax

variable_name = value

Examples

Basic Assignment

Creating variables of different types without any type declarations.

name = "John"
age = 21
gpa = 3.8
is_student = True

print(name, age, gpa, is_student)
  1. Each assignment binds a name to a different kind of object; print separates its arguments with spaces.
  2. Expected output: John 21 3.8 True

Multiple Assignment

Assigning several variables in a single line.

x, y, z = 1, 2, 3
print(x, y, z)  # 1 2 3

# Assign the same value to multiple variables
a = b = c = 0
print(a, b, c)  # 0 0 0
  1. Unpacking assigns one value per name. Chained assignment binds all three names to the same integer object; this is safe here because integers are immutable.
  2. Expected output: 1 2 3 0 0 0

Swapping Variables

Python's tuple assignment makes swapping values simple, with no temporary variable needed.

a = 5
b = 10

a, b = b, a

print(a, b)  # 10 5
  1. The original values on the right are read before either name on the left is rebound.
  2. Expected output: 10 5

Augmented Assignment Operators

Shorthand operators that combine an operation with assignment.

score = 10
score += 5   # score = score + 5  -> 15
score -= 3   # 12
score *= 2   # 24
score //= 5  # 4
print(score)

message = "Hello"
message += " World"
print(message)  # Hello World
  1. Each arithmetic update uses the previous score. The final string operation creates the combined text and rebinds message.
  2. Expected output: 4 Hello World

Best practices

  • Use snake_case for variable names (user_name, not userName or UserName) to follow Python conventions (PEP 8)
  • Choose descriptive names that explain what the variable holds, even if it makes them longer
  • Avoid reassigning a variable to a completely different type partway through a function - it makes code harder to follow
  • Use augmented assignment operators (+=, -=, etc.) for conciseness when updating a variable based on its own value
  • Constants (values that should never change) are conventionally written in ALL_CAPS, even though Python has no true constant enforcement

Runtime & versions

Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Python language reference

Specifications & further reading