Last modified: May 29, 2026
Python Variables and Assignment
Variables are a core concept in Python. They let you store data and reuse it later. Think of a variable as a labeled box. You put a value inside the box. Then you use the label to access that value.
This guide covers everything about Python variables and assignment. You will learn the rules, best practices, and common pitfalls. By the end, you will write clean and efficient Python code.
What Is a Variable in Python?
A variable is a name that refers to a value. Python uses dynamic typing. This means you do not need to declare the type beforehand. The type is inferred from the value you assign.
For example:
# Assign an integer to a variable
age = 25
# Assign a string to a variable
name = "Alice"
# Assign a float to a variable
pi = 3.14159
In this code, age, name, and pi are variables. Each holds a different type of data. Python figures out the type automatically.
Assignment in Python
Assignment is the process of giving a value to a variable. You use the equals sign (=) for this. The variable name goes on the left. The value goes on the right.
Basic assignment syntax:
# Simple assignment
x = 10
y = "Hello"
z = 3.14
You can also assign multiple variables in one line. This is called multiple assignment.
# Multiple assignment
a, b, c = 1, 2, 3
print(a) # Output: 1
print(b) # Output: 2
print(c) # Output: 3
Another useful trick is assigning the same value to multiple variables.
# Same value to multiple variables
x = y = z = 0
print(x) # Output: 0
print(y) # Output: 0
print(z) # Output: 0
Variable Naming Rules
Python has strict rules for variable names. Follow these to avoid errors.
Rule 1: Names can only contain letters, numbers, and underscores. No spaces or special characters.
Rule 2: Names cannot start with a number. 1var is invalid. var1 is valid.
Rule 3: Names are case-sensitive. myVar and myvar are different variables.
Rule 4: Avoid using Python keywords. Keywords like if, for, and while are reserved.
Here are examples of valid and invalid names:
# Valid names
my_variable = 5
userName = "Bob"
_count = 10
# Invalid names (will cause errors)
# 1st_variable = 5 # Starts with a number
# my-variable = 5 # Contains a hyphen
# for = 5 # Reserved keyword
Python Data Types
Variables can hold many types of data. Common types include integers, floats, strings, and booleans. You can check the type with the type() function.
# Check data types
x = 10
print(type(x)) # Output:
y = 3.14
print(type(y)) # Output:
z = "Hello"
print(type(z)) # Output:
flag = True
print(type(flag)) # Output: Python is dynamically typed. So a variable can change its type over time. This is flexible but can lead to bugs if you are not careful.
# Dynamic typing example
value = 42
print(type(value)) # Output:
value = "Now I am a string"
print(type(value)) # Output: Common Assignment Operations
You often need to update a variable based on its current value. Python provides augmented assignment operators for this.
For example, x += 5 is shorthand for x = x + 5. This works for other operators too.
# Augmented assignment
count = 10
count += 5 # Equivalent to count = count + 5
print(count) # Output: 15
count -= 3 # Equivalent to count = count - 3
print(count) # Output: 12
count *= 2 # Equivalent to count = count * 2
print(count) # Output: 24
count /= 4 # Equivalent to count = count / 4
print(count) # Output: 6.0
These operators make your code shorter and clearer. Use them when updating a variable with its own value.
Variable Scope
Scope determines where a variable is accessible. In Python, variables can be local or global.
A variable defined inside a function is local. It only exists within that function. A variable defined outside any function is global. It is accessible everywhere.
# Global variable
global_var = "I am global"
def my_function():
# Local variable
local_var = "I am local"
print(local_var) # Works fine
print(global_var) # Works fine
my_function()
# print(local_var) # This would cause an error
To modify a global variable inside a function, use the global keyword.
counter = 0
def increment():
global counter
counter += 1
increment()
print(counter) # Output: 1
Best Practices for Variables
Good variable names make your code readable. Follow these tips.
Use descriptive names. Instead of x, use user_age or total_price.
Use snake_case. Python convention uses lowercase letters with underscores. Example: my_variable_name.
Avoid single-letter names. Except for loop counters like i or j.
Use constants in uppercase. For values that never change, use uppercase with underscores. Example: MAX_SIZE = 100.
Here is a good example:
# Good variable names
student_name = "Alice"
student_age = 22
final_grade = 95.5
# Constants
PI = 3.14159
GRAVITY = 9.81
Common Mistakes and How to Avoid Them
Beginners often make these mistakes with variables.
Misspelling variable names. Python will create a new variable if you misspell. This can cause unexpected behavior.
# Typo example
user_name = "Alice"
print(usre_name) # NameError: name 'usre_name' is not defined
Using reserved keywords. Avoid names like if, else, while.
Forgetting to initialize. Always assign a value before using a variable.
# Uninitialized variable
# print(my_var) # NameError: name 'my_var' is not defined
my_var = 10
print(my_var) # Works fine
Confusing assignment with equality.= is assignment. == is comparison. Do not mix them.
If you are coming from JavaScript, you might find our guide on JavaScript Variables Guide helpful for comparison. Python's approach is simpler but has its own nuances.
Reassigning Variables
You can change a variable's value anytime. Just assign a new value to the same name.
# Reassignment
temperature = 25
print(temperature) # Output: 25
temperature = 30
print(temperature) # Output: 30
temperature = "Hot"
print(temperature) # Output: Hot
Notice that the type changed from int to string. This is allowed in Python but can be confusing. Try to keep a variable's type consistent.
Swapping Variables
Python makes swapping variables easy. You do not need a temporary variable.
# Swapping variables
a = 5
b = 10
a, b = b, a
print(a) # Output: 10
print(b) # Output: 5
This is a clean and Pythonic way to swap values.
Conclusion
Variables and assignment are the building blocks of Python programming. You learned how to create variables, assign values, and follow naming rules. You also explored data types, scope, and best practices.
Remember to use descriptive names and keep your code clean. Practice with small examples to build confidence. For more on working with different data structures, check out our article on Types of JavaScript Variables to see how other languages handle similar concepts.
Now you are ready to write Python code that is readable and efficient. Happy coding!