Skip to content

Python Variables

How to create and use variables in Python — dynamic typing, naming, and assignment.

What you'll learn

  • How to assign values to variables (no declaration keyword needed)
  • Python's dynamic typing and how to check types
  • Variable naming rules and conventions (PEP 8)
  • Multiple assignment and swapping variables

Concept

Creating Variables

In Python, you create a variable simply by assigning a value. There is no let, const, or var keyword — the assignment = creates the variable automatically:

name = "Ada"
age = 25
pi = 3.14159
is_admin = True

Dynamic Typing

Python is dynamically typed — a variable can hold any type, and the type can change at runtime. You do not declare the type; Python figures it out:

x = 10          # x is an int
x = "hello"     # now x is a str
x = [1, 2, 3]   # now x is a list

Use type() to check what a variable currently holds, and isinstance() to test against a type:

print(type(age))         # <class 'int'>
print(isinstance(age, int))  # True

Naming Rules

Variable names must:

  • Start with a letter or underscore (not a digit)
  • Contain only letters, digits, and underscores
  • Not be a reserved keyword (if, for, class, etc.)
  • Be case-sensitive (age and Age are different)

PEP 8 conventions:

  • Use snake_case for variables and functions: user_count, total_price
  • Use UPPER_SNAKE_CASE for constants: MAX_CONNECTIONS, PI
  • Use PascalCase for classes: MyClass
  • Names starting with _ are "private" by convention: _internal_value

Multiple Assignment

Python lets you assign multiple variables at once, which is both concise and readable:

# Assign the same value to multiple variables
x = y = z = 0

# Assign different values in one line
name, age, role = "Ada", 25, "admin"

# Swap two variables without a temp variable
a, b = 1, 2
a, b = b, a  # now a=2, b=1

Variables Are References

When you assign an object (like a list) to a variable, the variable is a reference to that object. This matters for mutability:

a = [1, 2, 3]
b = a           # b points to the SAME list
b.append(4)
print(a)        # [1, 2, 3, 4] — a changed too!

To create a copy, use b = a.copy() or b = list(a).

Example

              # Basic assignment — no declaration keyword
name = "Ada Lovelace"
age = 25
height = 1.68
is_student = True

print(f"{name} is {age} years old and {height}m tall")

# Dynamic typing — the same variable can change type
x = 10
print(type(x))   # <class 'int'>
x = "now a string"
print(type(x))   # <class 'str'>

# Multiple assignment
a, b, c = 1, 2, 3
print(f"a={a}, b={b}, c={c}")

# Swap without a temp variable
a, b = b, a
print(f"After swap: a={a}, b={b}")

# Variables are references — be careful with mutable objects
original = [1, 2, 3]
reference = original
reference.append(4)
print(f"original is also changed: {original}")  # [1, 2, 3, 4]

# Make a real copy instead
copy = original.copy()
copy.append(5)
print(f"original is NOT changed: {original}")   # still [1, 2, 3, 4]
            

Shows basic assignment, dynamic typing with type(), multiple assignment, the swap trick, the shared-reference pitfall with lists, and how .copy() avoids it.

Try it

  • Case Converter

    Python naming conventions (snake_case) — convert text live.

  • Math Evaluator

    Evaluate expressions like those you would assign to variables.

Common mistakes

The mistake

Using a single = (assignment) instead of == (comparison) in conditions

The fix

In Python, = assigns a value and == compares. if x = 5 is a SyntaxError; use if x == 5. Python catches this at parse time, unlike some other languages.

The mistake

Expecting b = a to copy a list, then being surprised when both change

The fix

b = a creates a reference to the same object, not a copy. Use b = a.copy() for a shallow copy, or import copy; b = copy.deepcopy(a) for nested structures.

Related Cheatsheets

Practice Python Variables

2 exercises

Practice Python Variables

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors