Python Data Types
1Python Data Types: overview
A data type tells Python what kind of value something is — a whole number, a decimal, text, True/False or nothing at all — and therefore what you can do with it.
Before this lesson: Python Variables
2Python Data Types: diagram
3Python Data Types: explained step by step
Every value in Python has a type. The type tells Python what kind of thing the value is, and therefore what you can do with it: you can divide two numbers but not two words, and you can make text uppercase but not a number.
The basic types
- int — whole numbers, positive or negative:
7,-3,2026. They can be as big as you like, and you may write1_000_000with underscores to make it easier to read. - float — numbers with a decimal point:
3.14,-0.5,2.0. Dividing with/always gives a float. - str — text, called a string, in single or double quotes:
"hello",'42'. - bool — a truth value:
TrueorFalse, always with a capital letter. Comparisons such as5 > 3produce a bool. - NoneType — the single value
None, meaning "nothing here yet" or "no result".
Python also has collection types such as list, tuple, dict and set, which hold many values at once. Each gets its own lesson.
Checking a type with type()
When you're not sure what kind of value you have, ask Python:
print(type(42))
print(type(3.5))
print(type("42"))
print(type(True))
print(type(None))Notice that "42" is a str, not an int. The quotes make it text that just happens to contain digits.
Why the type matters
The same operator can do different jobs depending on the types involved. + adds numbers but joins strings, and * multiplies numbers but repeats strings:
print(5 + 5)
print("5" + "5")
print("5" * 3)Mixing types carelessly leads to errors: "5" + 5 raises a TypeError, because Python refuses to guess whether you meant adding or joining.
Converting between types
To turn a value into another type, call the type's name like a function. Conversion creates a new value; the original is left unchanged.
age_text = "29"
age = int(age_text)
print(age + 1)
print(float("2.5") * 2)
print(str(99) + " bottles")
print(int(3.99))
print(bool(0), bool(""), bool("hi"))A few things to remember:
int()cuts off the decimal part instead of rounding, soint(3.99)is3. Useround()when you want rounding.int("3.5")fails with aValueError— the text must look like a whole number. Usefloat()for text with a decimal point.bool()givesFalsefor "empty" values —0,0.0,""andNone— andTruefor almost everything else.
Floats are approximate
Computers store floats in binary, and some decimals can't be stored exactly, so 0.1 + 0.2 comes out as 0.30000000000000004. Every programming language behaves like this. Round the result when you display it, and avoid comparing floats with ==.
Quick reference: Python Data Types
- Python Cheat Sheet (Python 3.13 Quick Reference) — This Python cheat sheet puts the most-used syntax for Python 3.13 into tables of tested one-liners: numbers, …
- Python List vs Tuple: Differences and When to Use Each — A **list** is a changeable (mutable) sequence written with `[]`, while a **tuple** is a fixed (immutable) seq…
- Python Mutable vs Immutable Objects Explained — **Mutable** objects (list, dict, set) can be changed in place after creation, while **immutable** objects (in…
- Python type() Function — `type(obj)` returns the class of an object, such as `<class 'int'>` or `<class 'str'>`, so you can see what k…
- Python isinstance() Function — `isinstance(obj, classinfo)` returns `True` if `obj` is an instance of the class (or of any class in a tuple …
4Python Data Types: syntax
type(value) # the type of a value, e.g. <class 'int'>
int("42") # text -> whole number 42
float("2.5") # text -> decimal number 2.5
str(99) # number -> text "99"
bool(0) # -> False ("empty" values are False)
isinstance(x, int) # True if x is an int
5Python Data Types: run the example
Press Run (or Ctrl + Enter). The output (step 6) appears directly below the code. Change anything and run it again.
product = "Notebook"
price = 3.5
quantity = 4
in_stock = True
discount = None
print(type(product), type(price))
print(type(quantity), type(in_stock), type(discount))
typed_amount = "6"
quantity = quantity + int(typed_amount)
total = price * quantity
print("Quantity:", quantity)
print("Total:", total)
print("Label: " + product + " x" + str(quantity))
print(type(total), type(str(total)))Expected output<class 'str'> <class 'float'>
<class 'int'> <class 'bool'> <class 'NoneType'>
Quantity: 10
Total: 35.0
Label: Notebook x10
<class 'float'> <class 'str'>
7Python Data Types: how the example works
Hover or tap a step to highlight its lines in the example above.
- Line 1-5Five variables, five types: text (str), a decimal (float), a whole number (int), a truth value (bool) and
None, meaning "no discount set". - Line 7-8
type()reports the type of each value, andprint()shows it as<class '...'>. - Line 10
"6"is in quotes, so it's text — exactly the kind of value you get when a user types a number — not an int. - Line 11
int("6")converts the text into the number 6, so it can be added: 4 + 6 = 10. - Line 12A float times an int gives a float: 3.5 × 10 =
35.0. The.0shows it is a float even though it's a whole amount. - Line 13-14The new quantity and the total are printed.
- Line 15
+between strings joins them.quantityis an int, sostr(quantity)turns it into the text"10"first. - Line 16
totalis still a float:str(total)made a new string value and lefttotalitself unchanged.
8Python Data Types: common mistakes
Adding a number to text
✗ Wrong
age = "30"
print(age + 1)✓ Right
age = "30"
print(int(age) + 1)age holds text, and Python won't add a number to text. Convert it with int() first.
Using int() on text with a decimal point
✗ Wrong
price = int("4.99")✓ Right
price = float("4.99")
print(price)int() only accepts text that looks like a whole number. Use float() for decimals — and int(float(...)) if you really want to drop the decimal part.
Writing true, false or none in lowercase
✗ Wrong
is_open = true✓ Right
is_open = True
print(is_open)True, False and None are special values that always start with a capital letter. In lowercase, Python treats them as unknown variable names.
9Python Data Types: exercises
Write your answer, then press Check. The checks run in your browser.
1. Fix the fruit total
Fix the bug Easy ✓ CompletedThe fruit counts arrive as text. This code should print Total fruit: 20, but it prints Total fruit: 128. Fix it so that total is the number 20.
apples = "12"
oranges = "8"
total = apples + oranges
print("Total fruit:", total)apples = "12"
oranges = "8"
total = int(apples) + int(oranges)
print("Total fruit:", total)
2. Predict the types
Predict the output Medium ✓ CompletedRead the code and type exactly what it prints. Then press Check to run it and compare.
print(type(7 / 2))
print(int(7.9))
print("7" * 2)
print(type("False"))
print(bool(0.0), bool(" "))<class 'float'>
7
77
<class 'str'>
False True
2 more exercises → 4 more examples
Questions learners ask
What is the difference between int and float in Python?
An int is a whole number with no decimal point, like 7. A float has a decimal point, like 7.0 or 7.5. Dividing with / always gives a float — even 8 / 2 is 4.0. Use int for counting things and float for measurements and averages.
Why does type() print <class 'int'> instead of just int?
In Python every type is a class, and type() hands you that class object, which prints as <class 'int'>. If you only want the name, use type(x).name, which gives 'int'.
What does None mean in Python?
None is a special value meaning "no value here". It's often used as a placeholder before the real value is known, and it's what a function gives back when it doesn't return anything. Check for it with x is None.
Further reading
10Next lesson
Up nextPython NumbersPython has three built-in number types, int for whole numbers, float for decimal numbers and complex for numbers with a…Related:
Python OperatorsPython StringsPython Input and OutputPython TypeError: What It Means and How to Fix It
← Python Variables · All Python Basics lessons · Practise in the IDE