Last modified: May 29, 2026
10 Python Variables for Beginners
Python variables are containers for storing data. They are simple to use but powerful. This article covers 10 key concepts every beginner needs. Each concept includes a short explanation and code example. You will learn how to create, use, and manage variables in Python.
Variables let you store values like numbers or text. You can reuse them in your code. Python is a dynamically typed language. That means you do not need to declare the type. The interpreter decides it for you. This makes Python very beginner-friendly.
If you already know JavaScript Variables Guide, you will find Python variables even simpler. They follow similar logic but with less syntax. Let us dive into the 10 key variable concepts.
1. Variable Assignment
You assign a value to a variable using the = sign. The variable name goes on the left. The value goes on the right. Python creates the variable automatically.
# Assigning a number to a variable
age = 25
print(age) # Output: 25
25
Tip: Variable names are case-sensitive. Age and age are different. Always use lowercase for consistency.
2. Multiple Assignment
You can assign multiple variables in one line. This saves space and makes code cleaner. Separate variables and values with commas.
# Multiple assignment in one line
x, y, z = 10, 20, 30
print(x, y, z) # Output: 10 20 30
10 20 30
You can also assign the same value to multiple variables. Use the = sign repeatedly.
# Same value for multiple variables
a = b = c = 100
print(a, b, c) # Output: 100 100 100
100 100 100
3. Variable Naming Rules
Variable names must follow rules. They can contain letters, numbers, and underscores. But they cannot start with a number. They also cannot use Python keywords like if or for.
# Valid variable names
my_var = 5
var2 = 10
_private = 20
# Invalid names will cause errors
# 2var = 10 # SyntaxError
# for = 5 # SyntaxError (keyword)
Best practice: Use descriptive names. student_name is better than sn. This makes your code self-documenting.
4. Dynamic Typing
Python variables can change type. You can assign a number first, then a string later. The variable type adapts automatically.
# Dynamic typing example
value = 42 # value is an integer
print(value) # Output: 42
value = "Hello" # value is now a string
print(value) # Output: Hello
42
Hello
This is different from statically typed languages. In Java or C, you must declare the type first. Python gives you more flexibility.
5. Variable Scope
Scope determines where a variable is accessible. Variables inside a function are local. Variables outside are global. Use global variables sparingly.
# Global variable
global_var = "I am global"
def my_function():
# Local variable
local_var = "I am local"
print(local_var) # Works
print(global_var) # Works
my_function()
# print(local_var) # Error: local_var not defined outside
I am local
I am global
Important: 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
1
6. Constants
Python does not have true constants. But by convention, use uppercase names for values that should not change. This tells other programmers the value is fixed.
# Constant convention
PI = 3.14159
MAX_USERS = 100
print(PI) # Output: 3.14159
print(MAX_USERS) # Output: 100
3.14159
100
You can still change the value. But the uppercase name warns others. Use this convention for readability.
7. Type Checking
You can check a variable's type with the type() function. This helps debug and understand your data. Use it when you are unsure what type a variable holds.
# Checking variable types
name = "Alice"
age = 30
height = 5.7
print(type(name)) # Output: <class 'str'>
print(type(age)) # Output: <class 'int'>
print(type(height)) # Output: <class 'float'>
<class 'str'>
<class 'int'>
<class 'float'>
For type conversion, use int(), str(), or float(). This is useful when reading user input.
# Type conversion
user_input = "25"
age = int(user_input) # Convert string to integer
print(age + 5) # Output: 30
30
8. Deleting Variables
You can delete a variable with the del keyword. This removes it from memory. Use it when you want to free up resources or reset data.
# Deleting a variable
temp = 100
print(temp) # Output: 100
del temp
# print(temp) # Error: name 'temp' is not defined
Note: Deleting is rarely needed in Python. The garbage collector handles memory automatically. But it is good to know for advanced use.
For more on variable management, see JavaScript Variables Examples for comparison.
9. Variable Swapping
Python lets you swap two variables in one line. No temporary variable needed. This is a Pythonic trick that makes code cleaner.
# Swapping variables
a = 5
b = 10
a, b = b, a # Swap values
print(a, b) # Output: 10 5
10 5
In other languages, you need a third variable. Python's tuple unpacking makes it elegant. Use this in sorting or reordering tasks.
10. Variable in Loops
Variables often change in loops. The loop variable updates each iteration. Be careful not to modify it accidentally inside the loop.
# Loop variable example
for i in range(5):
result = i * 2
print(result) # Output: 0 2 4 6 8
0
2
4
6
8
You can also use list comprehension for concise variable assignment. This is a powerful Python feature.
# List comprehension with variable
squares = [x**2 for x in range(5)]
print(squares) # Output: [0, 1, 4, 9, 16]
[0, 1, 4, 9, 16]
Variables in loops are temporary. They exist only inside the loop block. Use them for iteration but avoid relying on them outside.
Conclusion
Python variables are easy to learn but have many useful features. You now know assignment, naming, scope, and more. Practice these 10 concepts to write cleaner code. Start with simple scripts and gradually use advanced tricks like swapping and list comprehension. Python variables will become second nature.
For a deeper dive, check Types of JavaScript Variables to see how Python compares. Keep coding and experimenting. Every line of code builds your skill.