Last modified: May 25, 2026
Create a Variable in Python: Quick Guide
Python makes creating variables very easy. You do not need to declare a type. You just assign a value. This guide will show you how to do it. You will learn the rules and best practices.
What is a Variable?
A variable is a container for data. It holds a value in memory. You can use it later in your code. Python variables are dynamic. Their type can change as you run the program.
How to Create a Variable
To create a variable, use the equal sign =. Put the variable name on the left. Put the value on the right. Python will create the variable for you.
# Create a variable named 'age'
age = 25
print(age) # Output: 25
# Create a variable named 'name'
name = "Alice"
print(name) # Output: Alice
# Create a variable with a number
price = 19.99
print(price) # Output: 19.99
25
Alice
19.99
That is all you need to do. Python handles the rest. You can now use age, name, and price in your code.
Variable Naming Rules
Names must follow simple rules. They can only contain letters, digits, and underscores. They cannot start with a digit. They are case-sensitive. This means Age and age are different.
For a full list of rules, check out our Python Variable Naming Rules guide.
# Valid names
my_var = 10
var2 = "hello"
_private = True
# Invalid names (will cause errors)
# 2var = 5 # SyntaxError
# my-var = 3 # SyntaxError
# class = 7 # SyntaxError (reserved word)
Dynamic Typing in Python
Python is dynamically typed. You can change the type of a variable anytime. This is flexible but needs care.
# Same variable, different types
x = 10 # x is an integer
print(x) # Output: 10
x = "hello" # x is now a string
print(x) # Output: hello
x = 3.14 # x is now a float
print(x) # Output: 3.14
10
hello
3.14
This is powerful. But it can lead to bugs if you are not careful. Use clear names to avoid confusion.
Assigning Multiple Variables
You can create many variables in one line. This makes code cleaner.
# Multiple assignment
a, b, c = 1, 2, 3
print(a, b, c) # Output: 1 2 3
# Same value to multiple variables
x = y = z = 0
print(x, y, z) # Output: 0 0 0
1 2 3
0 0 0
Understanding Variable Meaning
A variable in Python is like a label. It points to an object in memory. When you assign a value, Python creates an object and attaches a name to it. For more detail, read our Python Variable Meaning Explained article.
Common Mistakes to Avoid
Beginners often make these errors. Avoid them to write better code.
- Using reserved words like
if,for, orclassas variable names. - Starting with a digit like
1var. - Forgetting case sensitivity –
Nameandnameare not the same. - Not using meaningful names –
xis okay, butuser_ageis better.
Best Practices for Creating Variables
Follow these tips to write clean code.
- Use snake_case for variable names:
my_variable. - Make names descriptive:
total_priceinstead oftp. - Avoid single-letter names except in loops.
- Keep variable scope small.
Examples of Variable Creation
Let us look at more examples. These show different data types.
# Integer
count = 10
# Float
temperature = 36.6
# String
message = "Hello, World!"
# Boolean
is_active = True
# List
fruits = ["apple", "banana", "cherry"]
# Dictionary
person = {"name": "Bob", "age": 30}
print(count, temperature, message, is_active, fruits, person)
10 36.6 Hello, World! True ['apple', 'banana', 'cherry'] {'name': 'Bob', 'age': 30}
For more examples, visit our Python Variables Examples Guide.
Conclusion
Creating a variable in Python is simple. Use the = operator. Follow naming rules. Use descriptive names. Avoid reserved words. Python is flexible, so you can change variable types. Practice with different data types. This will build your confidence. Now you can start coding with variables.