Last modified: May 25, 2026
Python Variables Examples Guide
Variables are the building blocks of any Python program. They store data that your code can use and manipulate. Understanding how variables work is essential for every Python developer.
In this guide, you will learn about Python variables with practical examples. We will cover assignment, naming rules, types, and scope. Each concept includes code snippets to help you learn faster.
What is a Python Variable?
A variable is a name that refers to a value stored in memory. Python is dynamically typed. This means you do not need to declare a variable's type before using it.
You can assign any value to a variable at any time. The interpreter handles the type automatically.
# Simple variable assignment
name = "Alice"
age = 30
price = 19.99
is_active = True
print(name)
print(age)
Alice
30
Variable Naming Rules
Python has strict rules for naming variables. Follow them to avoid errors.
- Variable names can contain letters, digits, and underscores.
- They cannot start with a digit.
- They are case-sensitive.
- They cannot be Python keywords.
Good variable names are descriptive. Use snake_case for multi-word names.
# Valid variable names
user_count = 10
total_price = 45.50
_first_name = "John"
# Invalid names (will cause errors)
# 2nd_place = "Silver" # starts with digit
# my-var = 5 # hyphen not allowed
# for = "loop" # 'for' is a keyword
Variable Assignment and Reassignment
You assign a value using the = operator. You can reassign a variable to a new value anytime.
Python also supports multiple assignments in one line.
# Multiple assignment
x, y, z = 1, 2, 3
print(x, y, z)
# Reassignment
x = 10
print(x)
# Swapping values
a = 5
b = 10
a, b = b, a
print(a, b)
1 2 3
10
10 5
Variable Types Examples
Python supports several built-in types. Here are common ones with examples.
# Integer
count = 100
print(type(count))
# Float
pi = 3.14159
print(type(pi))
# String
greeting = "Hello, Python!"
print(type(greeting))
# Boolean
is_valid = False
print(type(is_valid))
# List
fruits = ["apple", "banana", "cherry"]
print(type(fruits))
# Dictionary
person = {"name": "Bob", "age": 25}
print(type(person))
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'list'>
<class 'dict'>
For a deeper dive, read Understanding Python Variable Types. It explains each type in detail.
Variable Scope Examples
Scope defines where a variable is accessible. Python has local, enclosing, global, and built-in scopes.
A variable defined inside a function is local. A variable defined outside is global.
# Global variable
message = "Hello from global"
def greet():
# Local variable
message = "Hello from local"
print(message)
greet()
print(message)
Hello from local
Hello from global
To modify a global variable inside a function, use the global keyword.
counter = 0
def increment():
global counter
counter += 1
increment()
increment()
print(counter)
2
Learn more about scope in Mastering Python Variable Scoping: Best Practices and Techniques.
Constants and Naming Conventions
Python does not have built-in constant types. By convention, developers use uppercase names for constants.
This signals that the value should not change.
# Constants by convention
MAX_USERS = 100
PI = 3.14159
DEFAULT_NAME = "Guest"
print(MAX_USERS)
100
Type Hinting and Annotations
Python 3.5+ supports optional type hints. They improve code readability and help tools catch errors.
Type hints do not enforce types at runtime. They are for developers and linters.
# Variable with type hint
age: int = 25
name: str = "Alice"
prices: list[float] = [19.99, 24.50, 9.99]
def add(a: int, b: int) -> int:
return a + b
result = add(5, 3)
print(result)
8
For a complete guide, see Python Variable Annotations and Type Hinting: A Complete Guide.
Variable Unpacking Examples
Python allows unpacking sequences into multiple variables. This works with lists, tuples, and strings.
Use the * operator to capture remaining items.
# Basic unpacking
coordinates = (10, 20)
x, y = coordinates
print(x, y)
# Unpacking with *
first, *middle, last = [1, 2, 3, 4, 5]
print(first)
print(middle)
print(last)
# String unpacking
a, b, c = "ABC"
print(a, b, c)
10 20
1
[2, 3, 4]
5
A B C
Common Mistakes with Variables
Beginners often make these errors. Avoid them to write cleaner code.
- Using undefined variables.
- Misspelling variable names.
- Confusing
=(assignment) with==(comparison). - Modifying a global variable without the
globalkeyword.
# Common mistake: undefined variable
# print(undefined_var) # NameError
# Correct
undefined_var = "Now defined"
print(undefined_var)
# Mistake: using = instead of ==
x = 10
if x = 5: # SyntaxError
print("Equal")
Conclusion
Python variables are simple yet powerful. You learned assignment, naming rules, types, scope, and best practices. These examples give you a strong foundation.
Practice by writing small programs. Experiment with different variable types and scopes. This will build your confidence quickly.
Remember to use descriptive names and follow conventions. Your future self will thank you.