Python Variables
How to declare, assign, and name variables in Python
Overview
Variables in Python are names that refer to values stored in memory. Python is dynamically typed—you do not declare a type upfront. Assignment uses a single equals sign, and names should follow snake_case conventions for readability.
Syntax / Usage
# Basic assignment
name = "Ada"
age = 36
pi = 3.14159
is_active = True
# Multiple assignment
x, y, z = 1, 2, 3
# Reassignment changes the binding, not the original object
count = 10
count = count + 1 # now 11
# Constants by convention (not enforced)
MAX_RETRIES = 3
Variable names must start with a letter or underscore, cannot be Python keywords, and are case-sensitive (count ≠ Count).
Examples
Store user input and format a greeting:
first_name = input("First name: ")
last_name = "Lovelace"
greeting = f"Hello, {first_name} {last_name}!"
print(greeting)
Swap two values without a temporary variable:
a, b = 10, 20
a, b = b, a
print(a, b) # 20 10
Common Mistakes
- Using
=when you mean comparison (==) in conditionals - Shadowing built-in names like
list,str, orid - Assuming assignment copies data—mutable objects share references
- Using reserved keywords (
class,for,return) as variable names
See Also
python-data-types python-functions python-loops