Variables and Data Types in Python with Examples

Learn what variables and data types are in Python, how to declare them, what types exist, and how to avoid common mistakes when working with them.
What exactly is a variable?
Imagine you're moving. You have a lot of boxes. To avoid going crazy, you stick a label on one box that says "Books" and you put your books inside.
In Python, the box is a space in your computer's RAM, the label is the variable name, and the content is the data.
Golden Rule: In Python, variables don't "store" data like physical boxes, but rather "point" to it like arrows. This is called a reference.
Declaration and Assignment
Unlike languages such as Java or C++, in Python you don't need to say "I'm going to store a number." Python is dynamic: it looks at the data and deduces what it is.
# This is how simple it is to create a variable
username = "Gabriel"
age = 28
height = 1.75
is_developer = True
Naming Rules
You can't name your variables just any old way. Python has rules and conventions:
-
Allowed: Letters, numbers, and underscores
_. -
Forbidden: Starting with numbers or using spaces.
-
Case Sensitive:
scoreandScoreare two different variables.
The Snake Case Style 🐍
In Python, by convention (PEP 8), we use snake_case (lowercase words separated by underscores).
✅ last_login
❌ lastLogin (This is CamelCase, used in JavaScript).
Reserved words (keywords)
Python has words you cannot use as variable names because they already have a special meaning. Some of them are:
False, True, None, and, or, not, if, else, elif, for, while, break, continue, def, return, import, from, class, try, except, finally, with, as, pass, raise, in, is, lambda.
# ❌ Error: 'if' is a reserved word
if = 10
# ✅ Correct: use a valid name
if_condition = 10
Primitive Data Types
To be an expert, you must know the 4 basic types that you will see 90% of the time:
| Type | Name | Example | Use |
|---|---|---|---|
String | str | "Hello" | Text, messages, IDs. |
Integer | int | 42 | Whole numbers (no decimals). |
Float | float | 3.14 | Numbers with decimals (precision). |
Boolean | bool | True | Logical values (True/False). |
The NoneType type
Python has a special type called None that represents the absence of value. It is different from 0, "" (empty string), or False. It is used to indicate that a variable doesn't have a defined value yet.
result = None # We don't know the result yet
# Later...
result = 42 # Now it has a value
Dynamic Typing and its Risks
Python allows you to change the type of a variable on the fly. This is very flexible but dangerous in large projects.
points = 10 # It's an int
points = "Ten" # Now it's a str! Python doesn't complain.
Pro Tip: To avoid confusion in advanced courses, we use Type Hints:
# We tell other devs that 'age' should be an integer
age: int = 25
name: str = "Anna"
Operations with Variables
Variables aren't just there to look pretty; they interact with each other.
product_price = 100
tax = 0.21
final_price = product_price * (1 + tax)
print(f"The total to pay is: ${final_price}")
The type() function
Python includes a useful function called type() that tells you the type of any variable. It's ideal for debugging and understanding what data type you're working with.
name = "Anna"
age = 25
height = 1.75
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
print(type(height)) # <class 'float'>
String Manipulation
first_name = "Gabriel"
last_name = "Maza"
# The modern way: f-strings
full_name = f"{first_name} {last_name}"
print(full_name.upper()) # Result: GABRIEL MAZA
# You can also multiply strings
separator = "-" * 20
print(separator) # --------------------
Type Conversion (casting)
Sometimes you need to convert one data type to another. Python provides functions for this:
# Convert string to number
age_text = "25"
age_number = int(age_text)
print(age_number + 5) # 30
# Convert number to string
score = 95
message = "Your score is: " + str(score)
print(message) # Your score is: 95
# Convert to float
price = "19.99"
price_float = float(price)
print(price_float + 0.01) # 20.0
Common Mistakes (To Avoid Frustration)
- NameError: Trying to use a variable you haven't created yet.
print(total_points) # Error: name 'total_points' is not defined
- TypeError: Trying to combine things that don't belong together.
age = 25
text = "My age is " + age # Error: cannot concatenate str and int
# ✅ Correct: convert to string first
text = "My age is " + str(age)
Data type summary table
| Type | Python Name | Mutable? | Examples |
|---|---|---|---|
str | String | No | "Hello", 'Python', "123" |
int | Integer | No | 42, -5, 1000000 |
float | Float | No | 3.14, -0.5, 1.0 |
bool | Boolean | No | True, False |
NoneType | Null | No | None |
Practical challenge: "Tip Calculator"
Write a program that calculates how much each person should pay for a shared dinner.
Test variables:
total_bill = 450.00
num_people = 4
tip_percentage = 15 # 15%
# Your logic here
# tip = ...
# total_with_tip = ...
# total_per_person = ...
Hint: First calculate the tip as a percentage of the total, then add the tip to the total, and finally divide by the number of people.
To calculate a percentage, multiply the total by the percentage divided by 100: tip = total_bill * (tip_percentage / 100).
-
More about variables in Python: J2Logo - Variables in Python.
-
More about data types in Python: The Pythonist - Types in Python.
Ready to continue? Now that you know how to store data, learn how to manipulate it with operators in Python.
Want to practice with interactive exercises? Use DivZone AI to create and test your own exercises.