1. Introduction
A variable in Python is a name that refers to a value stored in memory. Unlike many other languages, Python does not require you to declare a variable's type before using it — you simply assign a value with the = operator and Python figures out the type automatically. This makes Python code concise and beginner-friendly, but it also means you need to understand how names are bound to objects to avoid subtle bugs.
Cricket analogy: A cricket team doesn't fix a player's batting position for life — captains reassign 'opener' to whoever suits the situation, just as Python lets a variable name be assigned any value without declaring its type upfront.
Variables matter because they let you store, reuse, and update data as a program runs — from simple counters in a loop to complex objects passed between functions. Mastering variable assignment is the first real step toward writing working Python programs.
Cricket analogy: A scoreboard's run tally is a variable that updates ball by ball throughout an innings and gets passed to the commentary team, just as Python variables store and update data as a program runs.
2. Syntax
x = 5
y = "hello"
z = 3.14
# multiple assignment
a, b, c = 1, 2, 3
# same value to multiple names
p = q = 1003. Explanation
In Python, a variable is really just a label pointing to an object in memory; the = sign creates or updates that binding rather than copying data into a fixed-type box. Because of this, the same variable name can be rebound to a completely different type of object later in the program — this is called dynamic typing.
Cricket analogy: A team's 'captain' armband is just a label passed to whichever player currently holds it — reassigning it to a new player doesn't copy the old captain's stats, just as Python's '=' rebinds a name to a new object.
Python also supports multiple assignment in a single line, either giving several names their own values (a, b, c = 1, 2, 3) or binding several names to the exact same object (p = q = 100). Understanding the distinction matters most for mutable objects like lists, where two names pointing to the same object will both see changes made through either name.
Cricket analogy: Assigning three batsmen their individual scores at once (a, b, c = 45, 30, 12) is like separate scorecards, but if two commentators share the same live-scoring app object, an update from one shows up for both instantly.
Variable names in Python are case-sensitive (age and Age are different variables), and by convention (PEP 8) should use lowercase snake_case such as user_name rather than camelCase.
Common misconception: reassigning a variable to a new type is not an error. x = 5 followed by x = "five" is perfectly legal — Python does not lock a variable to the type of its first value, because the variable is only a reference, not a typed storage slot.
4. Example
name = "Alice"
age = 30
height = 5.6
is_student = False
print(name, age, height, is_student)
print(type(name), type(age), type(height), type(is_student))
age = "thirty"
print(age, type(age))
a, b, c = 1, 2, 3
print(a, b, c)
x = y = 100
print(x, y)5. Output
Alice 30 5.6 False
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
thirty <class 'str'>
1 2 3
100 1006. Key Takeaways
- Variables are created by assignment (
=); no explicit type declaration is needed. - Python is dynamically typed — a variable can be reassigned to a value of a different type.
- Multiple assignment lets you assign several variables in one line:
a, b, c = 1, 2, 3. p = q = 100binds both names to the same object.- Variable names must start with a letter or underscore, contain only letters/digits/underscores, and are case-sensitive.
- PEP 8 recommends snake_case for variable names.
Practice what you learned
1. What is the correct way to create a variable holding the integer 5 in Python?
2. Which of the following is a valid Python variable name?
3. What happens after running `x = 5` followed by `x = "hello"`?
4. What does `a, b = b, a` do when `a = 1` and `b = 2`?
5. Which naming convention does PEP 8 recommend for ordinary Python variables?
6. After `x = y = 100` and then `y = 200`, what is the value of `x`?
Was this page helpful?
You May Also Like
Data Types in Python
A tour of Python's built-in data types — numeric, sequence, mapping, set, boolean, and NoneType — and how mutability affects each.
Keywords and Identifiers in Python
The difference between Python's reserved keywords and programmer-defined identifiers, plus the naming rules that make an identifier valid.
Operators in Python
An overview of Python's arithmetic, comparison, logical, assignment, bitwise, identity, and membership operators with precedence rules.
Scope and Namespaces in Python
Understanding Python's LEGB scope resolution order and how the global and nonlocal keywords let functions modify variables outside their local scope.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics