Python Basics — Variables
Variables
A variable is a name that refers to a value stored in your computer’s memory. Think of it as a labeled box — you give the box a name, and you put something inside it.
In Python, you don’t need to declare the type of a variable. Just assign a value with the = operator, and Python handles the rest.
Creating Variables
name = "Alice"
age = 25
temperature = 36.6
is_student = True
No int, string, or var keyword needed — Python automatically determines the type based on the value you assign.
Assignment Operator (=)
The = sign in Python is the assignment operator. It means “store this value in this variable.” It does NOT mean “equals” in the mathematical sense.
x = 10 # Store 10 in variable x
x = x + 5 # Take current x (10), add 5, store result (15) back in x
print(x) # 15
Multiple Assignment
Python lets you assign values to multiple variables in one line:
# Assign different values to multiple variables
a, b, c = 1, 2, 3
print(a) # 1
print(b) # 2
print(c) # 3
You can also assign the same value to multiple variables:
# Chained assignment
x = y = z = 0
print(x, y, z) # 0 0 0
Swapping Variables
Python makes variable swapping elegant:
a = 10
b = 20
# Swap in one line (no temp variable needed!)
a, b = b, a
print(a) # 20
print(b) # 10
Dynamic Typing
Python is dynamically typed, meaning a variable can hold different types of data at different times:
x = 42 # x is an integer
print(type(x)) # <class 'int'>
x = "hello" # Now x is a string
print(type(x)) # <class 'str'>
x = [1, 2, 3] # Now x is a list
print(type(x)) # <class 'list'>
This is different from statically typed languages (like Java or C++) where a variable’s type is fixed at declaration.
| Feature | Python (Dynamic) | Java (Static) |
|---|---|---|
| Declaration | x = 10 |
int x = 10; |
| Type change | Allowed | Not allowed |
| Type checking | At runtime | At compile time |
The type() Function
Use type() to check what type a variable holds:
name = "Bob"
age = 30
height = 5.9
is_active = True
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
print(type(height)) # <class 'float'>
print(type(is_active)) # <class 'bool'>
The id() Function
Every object in Python has a unique identity — a number that represents its memory location. Use id() to see it:
x = 10
print(id(x)) # Something like 140234866423056
y = 10
print(id(y)) # Same number! Python reuses small integers
z = 10000
print(id(z)) # Different number
Why this matters: Python caches small integers (-5 to 256) for performance. Two variables holding 10 point to the same object in memory.
Variable Naming Best Practices
| Rule | Example | Notes |
|---|---|---|
Use snake_case |
user_name, total_price |
PEP 8 standard |
Start with letter or _ |
_count, name |
Cannot start with digit |
| Be descriptive | student_age not sa |
Self-documenting code |
| Avoid single letters | Exception: i, j, k in loops |
Conventions are OK |
| Avoid reserved words | Don’t use class, for, if |
Python keywords |
| Use meaningful names | is_valid not flag |
Booleans start with is_, has_, can_ |
# ✅ Good variable names
first_name = "Alice"
total_items = 42
is_logged_in = True
max_retries = 3
# ❌ Bad variable names
fn = "Alice" # Too short, not descriptive
x = 42 # What does x represent?
flag = True # Flag for what?
data = 3 # Too vague
Deleting Variables
Use the del keyword to delete a variable:
x = 10
print(x) # 10
del x
# print(x) # NameError: name 'x' is not defined
You’ll rarely need to delete variables in practice. Python’s garbage collector handles memory cleanup automatically.
Variables Are References
In Python, variables don’t “contain” values — they reference (point to) objects in memory.
a = [1, 2, 3]
b = a # b now references the SAME list as a
b.append(4) # Modifying through b...
print(a) # [1, 2, 3, 4] — a is also changed!
This is because a and b point to the same list object. We’ll explore this concept deeper when we cover lists and mutable vs immutable types.
Common Mistakes
| Mistake | Example | Fix |
|---|---|---|
| Using before assignment | print(x) (x not defined) |
Assign before using |
| Starting with digit | 2name = "Bob" |
Use name2 = "Bob" |
| Using keywords | class = "Math" |
Use class_name = "Math" |
| Case sensitivity | Name ≠ name |
Be consistent with casing |
| Spaces in names | my var = 10 |
Use my_var = 10 |
Summary
- Variables are names that reference values in memory
- Use
=to assign values — no type declaration needed - Python uses dynamic typing — variable types can change
- Use
type()to check a variable’s type andid()for its identity - Follow
snake_casenaming and PEP 8 conventions - Multiple assignment and swapping work in a single line
- Variables are references, not containers
Next up: Numbers →