// python · beginner
Python Variables and Data Types Explained for Beginners
Learn what variables are, how to name them, the four basic Python data types, and how to convert between them without getting a TypeError.
Every program needs to remember things: a player’s score, a username, the price of an item. In Python you remember things with variables. This guide explains how variables work, which kinds of data they can hold, and how to avoid the most common beginner mistake of mixing up text and numbers.
What is a variable?
A variable is a name that points to a value. You create one with a single equals sign:
score = 0
player = "Ava"
Read = as “is set to”, not “equals”. The line score = 0 means “set score to 0”. You can change a variable whenever you like:
score = 0
score = score + 10
print(score) # 10
Python works out the right-hand side first (0 + 10), then stores the result back in score. There’s a shortcut for this that you’ll see everywhere:
score += 10 # same as score = score + 10
Naming rules
Variable names can contain letters, digits and underscores, but:
- they can’t start with a digit (
2nd_placeis not allowed); - they can’t contain spaces or hyphens (
my-scorereads as my minus score); - they can’t be a Python keyword such as
if,for,classorTrue.
Names are case-sensitive, so Score and score are two different variables. The Python convention is snake_case: lowercase words joined by underscores, such as high_score or first_name. Pick names that say what the value means; total_price is far easier to understand later than tp.
The four basic data types
| Type | What it holds | Examples |
|---|---|---|
int |
Whole numbers | 7, -3, 2026 |
float |
Numbers with a decimal point | 3.14, 0.5, 10.0 |
str |
Text (a string of characters) | "hello", 'Python', "42" |
bool |
True or false | True, False |
You can ask Python what type something is with type():
print(type(7)) # <class 'int'>
print(type(7.0)) # <class 'float'>
print(type("7")) # <class 'str'>
print(type(True)) # <class 'bool'>
Notice that "7" with quotes is a string, not a number. That difference matters a lot, as you’re about to see.
The classic mistake: text plus a number
age = input("How old are you? ")
print(age + 1)
Run this, type 20, and Python stops with:
TypeError: can only concatenate str (not "int") to str
input() always gives you a string, even if the user types digits. Python refuses to add the text "20" to the number 1 because it isn’t sure what you mean. The fix is to convert the string to a number first:
age = int(input("How old are you? "))
print(age + 1) # 21
Converting between types
| Function | Converts to | Example | Result |
|---|---|---|---|
int() |
whole number | int("42") |
42 |
float() |
decimal number | float("3.5") |
3.5 |
str() |
text | str(99) |
"99" |
bool() |
True/False | bool(0) |
False |
Be careful: int("hello") or even int("3.5") will raise a ValueError, because that text isn’t a whole number. You’ll learn how to handle that gracefully with try and except later on.
Putting values into text with f-strings
Joining strings with + gets messy quickly. f-strings are the modern, tidy way: put an f before the quotes and wrap variables in curly braces.
name = "Ava"
score = 1250
print(f"{name} has {score} points")
# Ava has 1250 points
f-strings convert numbers to text for you, so there’s no TypeError to worry about. You can even do maths inside the braces:
price = 4.5
quantity = 3
print(f"Total: £{price * quantity:.2f}")
# Total: £13.50
The :.2f part means “show two decimal places”, which is handy for money.
Mini project: a tip calculator
bill = float(input("Bill amount: £"))
tip_percent = int(input("Tip percentage: "))
tip = bill * tip_percent / 100
total = bill + tip
print(f"Tip: £{tip:.2f}")
print(f"Total to pay: £{total:.2f}")
Try running it with different numbers. Then extend it: ask how many people are sharing the bill and print how much each person pays.
Key points
- A variable is a name pointing to a value;
=means “set to”. - The four basic types are
int,float,strandbool. input()always returns a string, so convert it withint()orfloat()before doing maths.- Use f-strings to build text that includes variables.
Next up: learn every way Python can calculate with numbers in Python Operators and Maths.