Variables in Python: Complete Beginner's Guide
What Are Python Variables?
A Python variable is a name that refers to a value stored in the computer's memory. Instead of working with raw memory addresses, you work with readable names like age or username.
In Python, a variable is essentially a label attached to an object. When you write age = 25, Python creates an integer object with the value 25 and binds the name age to it. This is different from languages like C, where a variable is a fixed memory slot of a specific type — in Python, the name and the value are two separate things connected by a reference.
age = 20
name = "Krish"
is_student = TrueHere, age, name, and is_student are all python variable names pointing to an integer, a string, and a boolean respectively.
Why Are Variables Important in Python?
Variables let you store, reuse, and manipulate data without rewriting values every time. They make code readable, reusable, and easier to maintain.
Without variables, you'd have to hardcode every value directly into your logic, making programs rigid and repetitive. Variables allow you to:
- Store user input, calculations, or file data for later use
- Give meaningful names to values, improving code readability
- Update a single value in one place instead of many
- Pass data between functions, loops, and conditional statements
price = 99.99
quantity = 5
total = price * quantity
print(total) # 499.95Changing price or quantity automatically updates total wherever it's used — that's the core value of variables.
How to Declare Variables in Python?
You declare a variable in Python simply by assigning a value to a name using the = operator — there's no separate declaration step or type keyword required.
This is a key difference from languages like Java or C++, where you must state a type before the name (e.g., int age;). Python variable declaration happens in a single step:
city = "Delhi" # string
temperature = 32.5 # float
population = 33807403 # integerNote:
The = sign in Python is the assignment operator, not a mathematical equals sign. It means "assign the value on the right to the name on the left."
You can also assign a value later using input from a user or a calculation:
user_input = input("Enter your name: ")
result = 10 + 20What Is Dynamic Typing in Python?
Python dynamic typing means a variable's data type is determined automatically at runtime based on the assigned value, and the same variable can later be reassigned to a completely different type.
You never declare a type explicitly. Python checks the value on the right side of the = sign and assigns the appropriate type behind the scenes.
value = 10 # value is an int
print(type(value)) # <class 'int'>
value = "ten" # now value is a str
print(type(value)) # <class 'str'>
value = 10.5 # now value is a float
print(type(value)) # <class 'float'>This flexibility speeds up development but requires care — reassigning a variable to an unexpected type mid-program is a common source of bugs.
Python Variable Naming Rules and Best Practices
Python enforces strict syntax rules for variable names, called python identifiers, and violating them causes a SyntaxError.
The official rules are:
- A variable name must start with a letter (a–z, A–Z) or an underscore (_) — never a digit.
- After the first character, it can contain letters, digits (0–9), and underscores.
- Variable names are case-sensitive (age and Age are different variables).
- You cannot use a Python reserved keyword (like for, if, class, return) as a variable name.
- Spaces and special characters (-, @, %, !, etc.) are not allowed.
# Following the rules
first_name = "Ronnie"
_temp = 100
score1 = 95Multiple Variable Assignment in Python
Python multiple assignment lets you assign several different values to several different variables in a single line, separated by commas.
name, age, city = "Jonathan", 25, "Delhi"
print(name) # Jonathan
print(age) # 25
print(city) # DelhiThe number of variables on the left must match the number of values on the right, or Python raises a ValueError. This technique is commonly used to unpack values returned from functions, tuples, or lists.
def get_coordinates():
return 12.9, 77.6
latitude, longitude = get_coordinates()Python Constants: The Uppercase Convention
Python has no built-in keyword for true constants — instead, python constants are created by convention, using all-uppercase variable names to signal that the value should not be changed.
PI = 3.14159
MAX_USERS = 100
API_KEY = "abc123xyz"Nothing technically prevents reassignment of PI, but the uppercase naming tells other developers (and your future self) that the value is intended to stay fixed throughout the program. Constants are typically defined at the top of a file or in a separate configuration module.
The Python global Keyword
The python global keyword lets you modify a variable defined outside a function from within that function, instead of creating a new local variable.
By default, a variable assigned inside a function is treated as local to that function, even if a variable with the same name exists outside it. The global keyword overrides this behavior:
counter = 0
def increment():
global counter
counter += 1
increment()
increment()
print(counter) # 2Without the global keyword, the line counter += 1 inside the function would raise an UnboundLocalError, because Python would treat counter as a local variable being referenced before assignment.
Tip: Use global sparingly. Relying on it heavily makes code harder to test and debug — passing values as function arguments and return values is usually a cleaner approach.
Conclusion
Python variables are the foundation of every Python program, letting you store, reuse, and manipulate data through simple name-to-value bindings created with the = operator. Because Python uses dynamic typing, you never declare a type explicitly — the interpreter infers it from the assigned value and allows reassignment to any type. Mastering naming rules, scope (local vs. global), multiple assignment, and the constants convention gives you the groundwork needed to write clean, readable Python code. Get comfortable with Python variables early, since nearly every other concept in the language builds directly on top of them.