Introduction
When you write a program, you almost always need to store information — a name, a number, a score, a result. Variables are how you do that. They are one of the very first concepts every Python programmer learns, and understanding them well sets the foundation for everything else that follows.
Think of a variable as a labelled box. You put something inside the box, give the box a name, and whenever you need what is inside, you simply call the name.
What Is a Variable?
A variable in Python is a named location in memory that stores a value. You create a variable by giving it a name and assigning a value to it using the equals sign ( = ).
- name stores a string
- age stores an integer
- score stores a float
- is_student stores a boolean
The moment you write name = "Adedeji", Python creates a space in memory, labels it "name", and stores the value "Adedeji" inside it.
Variable Assignment
Assigning a variable means giving it a value. In Python, the = sign is the assignment operator — it does not mean "equal to" the way it does in mathematics. It means "store this value in this variable."
Python reads the right side first, evaluates it, then stores the result in the variable on the left.
Variable Naming Rules
Python has strict rules about what you can and cannot name a variable. Breaking these rules causes a syntax error.
Rules you must follow:
- Variable names can only contain letters, numbers, and underscores ( _ )
- Variable names must start with a letter or an underscore — never a number
- Variable names are case-sensitive — name, Name, and NAME are three different variables
- Variable names cannot be Python reserved keywords (see below)
Python reserved keywords — these cannot be used as variable names: 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
Variable Naming Conventions
Beyond the rules, Python programmers follow widely agreed conventions to make code readable and professional.
Snake case — the standard convention for variables in Python. Words are lowercase and separated by underscores.
Camel case — more common in other languages but sometimes seen in Python.
Descriptive names — always name variables clearly so anyone reading your code understands what the variable holds.
Data Types Stored in Variables
A variable in Python can store different types of data. Python automatically detects the type — you do not need to declare it.
Integer (int)
Whole numbers, positive or negative, with no decimal point.
Float (float)
Numbers with a decimal point.
String (str)
Text — any sequence of characters enclosed in single or double quotes.
Boolean (bool)
A value that is either True or False — nothing else.
Checking the Type of a Variable
Use the built-in type() function to check what type a variable is.
Multiple Assignment
Python allows you to assign values to multiple variables in a single line.
Reassigning Variables
Unlike some other programming languages, Python variables are not fixed to one data type. You can reassign a variable to a completely different type at any point.
Python handles this dynamically — this is called dynamic typing.
Constants in Python
Python does not have a built-in constant type the way some languages do. By convention, variables intended to be treated as constants are written in all capital letters. This signals to other programmers that the value should not be changed.
These are technically still variables in Python (nothing prevents them from being changed) but the naming convention communicates the intention clearly.
Global and Local Variables
Local Variables
A variable created inside a function exists only within that function. It cannot be accessed from outside.
Global Variables
A variable created outside all functions is a global variable. It can be accessed from anywhere in the program.
The global Keyword
If you need to modify a global variable from inside a function, use the global keyword.