Skip to main content

Variables

What are variables?

Variables are names that refer to values. They let you store data and give it meaningful labels, making your code readable and maintainable. Python doesn't use type declarations; a name is bound to an object at runtime and can later be rebound to another.

message = "hello"   # name -> string object
count = 3 # name -> int object
pi = 3.14159 # name -> float object

Why this matters

Variables are essential for writing useful programs. Without variables, you'd have to repeat values throughout your code, making it hard to maintain and update. Variables let you store user input, intermediate calculations, and results that you'll use later. They also make your code self-documenting. A well-named variable like user_age is much clearer than just using the number 25 scattered throughout your code. Understanding how variables work, especially how they reference objects and how mutability affects them, is crucial for avoiding bugs and writing clean Python code.

Naming rules and style

  • Letters, digits, and underscores. Cannot start with a digit.
  • Case-sensitive: name and Name are different.
  • Avoid keywords like for, if, class.
  • Follow PEP 8: use lower_snake_case for variables.
user_name = "ada"
max_items = 10

Assignment and rebinding

= binds a name to a value. Reassigning changes the binding, not the original object.

x = 1
x = x + 2 # x now refers to 3

Multiple assignment is supported:

a, b = 1, 2
a, b = b, a # swap

Underscore is often used as a throwaway name:

first, _, third = [10, 20, 30]

Immutability vs mutability

Rebinding works the same for all objects, but mutating an object changes it in place.

nums = [1, 2, 3]
alias = nums
alias.append(4)
print(nums) # [1, 2, 3, 4] (same list, two names)

For immutable objects, you can only rebind:

name = "hi"
name = name + " there" # creates a new string; old one unchanged

Identity vs equality

  • == checks if values are equal.
  • is checks if two names refer to the same object.
a = [1, 2, 3]
b = [1, 2, 3]
c = a
a == b # True (same contents)
a is b # False (different lists)
a is c # True (same list)

Tips for beginners

  • Choose descriptive names: total, user_age, items.
  • Keep a name’s meaning consistent; avoid reusing the same name for unrelated data.
  • Print and inspect with print(x) or type(x) to see what a name currently refers to.