Python Variables: The Complete Guide Every Developer Needs (With Real-World Examples)

If you’ve ever run into a NameError, got confused about why a variable changed unexpectedly inside a function, or wondered why your variable “disappeared” the moment your code exited a loop, this guide is for you.

Python variables are deceptively simple on the surface. You can learn the basic syntax in five minutes. But the real depth, scope, mutability, type conversion, unpacking, and naming pitfalls trip up beginners and even intermediate developers all the time.

In this tutorial, I’m going to walk you through everything you need to know about Python variables. I’ll keep it practical, show you real code I’ve personally tested, and flag the most common mistakes I’ve seen people make.

Let’s get into it.

Table of Contents:

What Is a Variable in Python? (Simple Explanation)

A variable is a name that points to a value stored in memory. That’s it. You give a name to some data so you can use it, change it, and refer to it throughout your program.

Here’s a simple way to think about it: imagine a variable as a sticky label on a storage box. The label (the variable name) tells you what’s inside. The box (computer memory) holds the actual value. You can change what’s in the box anytime, and you can even put a completely different type of thing in it; that’s Python’s dynamic typing in action.

# Creating a variable
name = "Michael"
age = 28
price = 9.99

print(name) # Michael
print(age) # 28
print(price) # 9.99

You can refer to the screenshot below to see the output.

Python Variable

In Python, you don’t need to declare the type upfront. The moment you assign a value, the variable is created. No separate int x; declaration like in Java or C++.

And here’s something worth knowing early on, Python is dynamically typed, which means the same variable can hold different types of data at different points in your code:

data = 42           # data is an integer
data = "hello" # now data is a string
data = [1, 2, 3] # now data is a list

print(type(data)) # <class 'list'>

This is very flexible — but it also means you need to be careful, because it’s easy to accidentally overwrite a variable with the wrong type and get unexpected behavior.

Python Variable Types Explained (With a Quick-Reference Table)

Python variables can hold many types of data. Here’s a clean reference table so you can see everything at a glance before we dive into each one:

Variable TypeExampleMutable?Common Use Case
Integer (int)age = 30NoCounting, indexing
Float (float)price = 9.99NoDecimal calculations
String (str)name = "Alice"NoText, labels, messages
Boolean (bool)is_active = TrueNoFlags, conditions
List (list)items = [1, 2, 3]✅ YesOrdered, changeable collections
Tuple (tuple)coords = (10, 20)NoFixed data, unpacking
Dictionary (dict)user = {"id": 1}✅ YesKey-value pairs
Set (set)unique = {1, 2, 3}✅ YesDeduplication, membership checks

Now let’s look at each one with practical examples.

Integer

An integer is a whole number — no decimal point.

product_count = 150
year = 2026
temperature = -5

print(type(product_count)) # <class 'int'>

You’ll use integers everywhere — loop counters, IDs, indexes, age values, scores.

Float

A float is a number with a decimal point.

item_price = 499.99
discount_rate = 0.15
pi = 3.14159

print(type(item_price)) # <class 'float'>

# Practical example: calculate discount
final_price = item_price - (item_price * discount_rate)
print(f"Final price: ${final_price:.2f}") # Final price: $424.99

You can refer to the screenshot below to see the output.

Variables in Python

Watch out: floating-point numbers can sometimes give you tiny precision issues (like 0.1 + 0.2 = 0.30000000000000004). For financial calculations, use Python’s decimal module instead.

String

A string is text — anything wrapped in single or double quotes.

first_name = "Sarah"
city = 'Austin'
message = "Welcome to Python!"

# String operations
print(first_name.upper()) # SARAH
print(len(city)) # 6
print(f"Hello, {first_name}!") # Hello, Sarah!

Strings in Python are immutable — you can’t change individual characters in place. But you can always create a new string from the old one.

Boolean

A boolean holds either True or False. That’s it — just two values.

is_logged_in = True
has_permission = False
is_admin = True

if is_logged_in and is_admin:
print("Access granted")
else:
print("Access denied")

Booleans are what drive all your if statements, while loops, and conditional logic. They’re used constantly in real applications.

List

A list is an ordered, mutable collection. You can add, remove, and change items.

products = ["Laptop", "Mouse", "Keyboard"]

# Add an item
products.append("Monitor")
print(products) # ['Laptop', 'Mouse', 'Keyboard', 'Monitor']

# Remove an item
products.remove("Mouse")
print(products) # ['Laptop', 'Keyboard', 'Monitor']

# Sort the list
products.sort()
print(products) # ['Keyboard', 'Laptop', 'Monitor']

# Access by index
print(products[0]) # Keyboard

Lists are one of the most used data structures in Python. If you’re building any kind of collection that might change, use a list.

Tuple

A tuple looks like a list but uses parentheses — and once created, you can’t change it.

coordinates = (40.7128, -74.0060)  # New York City's lat/long
rgb_color = (255, 128, 0)

print(coordinates[0]) # 40.7128
print(type(coordinates)) # <class 'tuple'>

# Tuples are great for returning multiple values from a function
def get_user():
return "Jake", 32, "Chicago"

name, age, city = get_user()
print(name, age, city) # Jake 32 Chicago

Use tuples when data shouldn’t change — like coordinates, RGB values, or config settings you want to stay fixed.

Dictionary

A dictionary stores data as key-value pairs. Think of it like a real dictionary — you look up a key, you get the value.

user = {
"name": "Emily",
"age": 27,
"city": "Seattle",
"is_active": True
}

# Access values
print(user["name"]) # Emily
print(user.get("age")) # 27

# Add a new key
user["email"] = "emily@example.com"

# Update a value
user["age"] = 28

print(user)

You can refer to the screenshot below to see the output.

Variables Python

Dictionaries are heavily used when you’re working with APIs, JSON data, databases, or anything that has labeled fields.

Set

A set is an unordered collection with no duplicate values.

visitors = {"Alice", "Bob", "Charlie", "Alice", "Bob"}
print(visitors) # {'Alice', 'Bob', 'Charlie'} — duplicates removed

# Check membership
print("Alice" in visitors) # True

# Set operations
team_a = {"Ryan", "Jessica", "Marcus"}
team_b = {"Jessica", "Marcus", "Lauren"}

print(team_a & team_b) # Intersection: {'Jessica', 'Marcus'}
print(team_a | team_b) # Union: {'Ryan', 'Jessica', 'Marcus', 'Lauren'}

Sets are perfect when you need to eliminate duplicates or do membership testing fast.

Python Variable Naming Rules — And What Happens When You Break Them