Variables
Learn what variables are, how Python assigns and manages them dynamically, and the rules and conventions for naming them well.
Introduction
Every useful program needs a way to remember information — a user's name, a running total, a score, a temperature reading. In Python, that information is stored in variables. A variable is simply a name that points to a value stored in memory, and it is one of the very first building blocks you need to write real programs.
In this lesson, you will learn what a variable actually is, how Python's dynamic typing makes creating them effortless, the rules and conventions for naming them well, and a few handy tricks like assigning several variables at once.
- What a variable is and how assignment works.
- Why Python does not require you to declare a type.
- The rules and conventions for naming variables.
- How to assign multiple variables in a single line.
- How to check what type of value a variable currently holds.
What is a Variable?
A variable is a named label attached to a value stored in your computer's memory. Instead of remembering the exact memory address where "25" is stored, you give it a friendly name like age, and Python takes care of the rest.
age = 25
print(age)25A Named Label
A variable is just a name that refers to a value stored somewhere in memory.
A Container Analogy
Think of a variable as a labeled box that holds a value you can look at or replace.
Reusable
Once created, a variable can be read, updated, or used in calculations anywhere in your code.
Creating Variables
You create a variable in Python simply by writing its name, followed by the assignment operator =, followed by the value you want to store. There is no special keyword like var or let required.
name = "Priya"
score = 92
is_passed = True
print(name)
print(score)
print(is_passed)Priya
92
TrueThe = sign here does not mean "equals" like in math class — it means "assign the value on the right to the name on the left." Read score = 92 as "score is assigned the value 92."
Dynamic Typing
Many programming languages require you to state a variable's type up front, such as int age or String name. Python does not. It uses dynamic typing, which means Python figures out the type of a variable automatically, based on the value you assign to it.
x = 10 # Python knows this is an int
y = 3.14 # Python knows this is a float
z = "hello" # Python knows this is a str- You never write out a type when creating a variable.
- Python inspects the value and assigns the type behind the scenes.
- This makes Python code shorter and faster to write than statically typed languages.
Naming Rules
Python enforces a small set of strict rules for what counts as a valid variable name. Breaking any of these rules causes a SyntaxError.
Letters, Digits, Underscore
Names can contain letters, digits, and underscores only — no spaces or symbols like -, @, or %.
No Leading Digit
A name cannot start with a digit. 2names is invalid, but names2 is fine.
Case-Sensitive
age, Age, and AGE are three completely different variables to Python.
No Reserved Keywords
You cannot use words Python reserves for itself, like if, for, class, or True.
# Valid
user_name = "Alex"
_score = 100
total2 = 50
# Invalid — would raise SyntaxError
# 2total = 50
# user-name = "Alex"
# class = "10th"You can check every keyword Python reserves using the built-in keyword module.
import keyword
print(keyword.kwlist)['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']Naming Conventions
Beyond the strict rules, Python developers follow shared style conventions so that code is easy for others to read. The official convention for variable names is snake_case — all lowercase words separated by underscores.
snake_case
first_name, total_price, is_active — the standard style for Python variables.
camelCase
firstName works in Python but is not the conventional style — it is common in JavaScript and Java instead.
Descriptive Names
Prefer total_price over tp — clear names make code easier to understand later.
# Recommended
student_age = 16
total_price = 499.99
# Works, but not conventional Python style
studentAge = 16Multiple Assignment
Python lets you assign several variables in a single line, which keeps related assignments compact and readable.
The first style assigns different values to different variables, matched by position.
a, b = 1, 2
print(a)
print(b)1
2The second style assigns the exact same value to several variables at once, chained with equal signs.
x = y = z = 0
print(x, y, z)0 0 0Multiple assignment even lets you swap two variables without a temporary variable: a, b = b, a.
Reassigning Variables
Because Python is dynamically typed, a variable is not locked to the type of its first value. You can reassign it to a completely different type at any time, and Python will simply update what the name points to.
value = 10
print(value, type(value))
value = "now a string"
print(value, type(value))10 <class 'int'>
now a string <class 'str'>This flexibility is convenient, but reassigning a variable to an unrelated type midway through a program can make code confusing to follow — it is best used intentionally, not by accident.
Checking a Variable's Type
Python's built-in type() function tells you exactly what kind of value a variable currently holds. This is especially useful while learning, or while debugging unexpected behavior.
name = "Riya"
age = 16
height = 5.4
is_student = True
print(type(name))
print(type(age))
print(type(height))
print(type(is_student))<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>Constants in Python
Some languages have a true const keyword that prevents a value from ever changing. Python does not — technically, every variable in Python can be reassigned at any time. Instead, Python programmers signal "this value should not be changed" purely through convention: writing the name in ALL_CAPS with underscores.
PI = 3.14159
MAX_USERS = 100
APP_NAME = "PrograMinds"
print(PI)3.14159Writing PI = 3.14159 does not stop later code from reassigning PI = 4. ALL_CAPS is a signal to other developers that a value is meant to be treated as constant — Python itself does not enforce it.
Common Mistakes
- Starting a variable name with a digit, like 1st_place.
- Using a reserved keyword as a variable name, like class = "A".
- Mixing naming styles, such as userName in one place and user_age elsewhere.
- Using unclear single-letter names like x or d for values with real meaning.
- Assuming Python "declares" a type — it only infers one from the assigned value.
Best Practices
- Use descriptive names: total_price instead of tp.
- Follow snake_case for all variable names.
- Reserve ALL_CAPS names for values meant to stay constant.
- Avoid reassigning a variable to an unrelated type unless there is a clear reason.
- Use type() while learning to confirm what type a value actually is.
Frequently Asked Questions
Do I need to declare a variable's type before using it?
No. Python uses dynamic typing, so the type is determined automatically from the value you assign.
Can a variable name start with an underscore?
Yes. _score is a valid name, though a leading underscore is usually reserved for special conventions used in larger programs.
Are variable names case-sensitive?
Yes. total, Total, and TOTAL are treated as three separate variables.
Does Python have true constants?
No. Python has no keyword that prevents reassignment. ALL_CAPS names are only a convention that signals "please do not change this."
What happens if I use a keyword like for as a variable name?
Python raises a SyntaxError immediately, because keywords are reserved for the language's own syntax.
Key Takeaways
- A variable is a name that refers to a value stored in memory.
- Python uses dynamic typing — you never declare a type explicitly.
- Variable names must start with a letter or underscore, and can only contain letters, digits, and underscores.
- snake_case is the standard naming convention for Python variables.
- You can assign multiple variables in one line using a, b = 1, 2 or x = y = z = 0.
- A variable can be reassigned to any type at any time.
- type() reveals the current type of any variable.
- Python has no true constants — ALL_CAPS names are just a convention.
Summary
Variables are the foundation of every Python program — they let you store, label, and reuse values as your code runs. Because Python is dynamically typed, creating a variable is as simple as writing a name, an equal sign, and a value.
In this lesson, you learned how variables work, the rules and conventions for naming them, how to assign several at once, and how to inspect a variable's type using type(). Next, you will explore Python's built-in data types in much more depth.
- You understand what a variable is and how assignment works.
- You know the rules and conventions for naming variables.
- You can assign multiple variables in a single line.
- You are ready to explore Python's data types.