Variables in Python
Variables are one of the first ideas you meet in Python. They let you store values and reuse them later — numbers, text, lists, and more.
Think of a variable as a labeled box. You put something in the box, give it a name, and use that name whenever you need the value.
Creating a variable
In Python you create a variable by writing a name, an equals sign, and a value:
age = 25
name = "Ada"
agestores the number25namestores the text"Ada"
You don’t need to declare a type. Python figures it out from the value.
Try it: store and print values
Edit the code if you like, then click Run:
Naming rules (keep these in mind)
Good variable names make your code easier to read.
Do:
- Use lowercase letters
- Separate words with underscores:
first_name,total_score - Choose names that describe the value
Don’t:
- Start with a number (
1nameis invalid) - Use spaces (
first nameis invalid) - Use Python keywords like
if,for,class
Examples:
| Valid | Invalid |
|---|---|
score |
2score |
user_name |
user-name |
total |
total amount |
Changing a variable
You can update a variable by assigning a new value:
count = 1
count = count + 1
Or more briefly:
count = 1
count += 1
Try it: update a value
Common types you’ll use early
| Type | Example | Meaning |
|---|---|---|
int |
age = 21 |
Whole number |
float |
price = 9.99 |
Decimal number |
str |
city = "Lagos" |
Text |
bool |
is_active = True |
True or False |
Check a type with type():
x = 42
print(type(x))
Try it: inspect types
Quick practice
Before you move on, try changing the examples above:
- Change
nameto your own name and run again - Start
scoreat0and add points in two steps - Create a variable for your favorite language and print it
What’s next
Once variables feel comfortable, the next step is working with strings and numbers — combining text, doing math, and formatting output cleanly.
For a broader learning path, see:
Whether you have a question, an idea, or feedback, we’d love to hear from you. Get in touch.