Every value in Python has a type. The type decides what you can do with the value: you can divide two numbers, but not two names; you can make text upper-case, but not a number. Knowing the basic Python data types helps you understand error messages and avoid surprises such as "2" + "2" giving "22". This lesson builds on Python Syntax and Variables.
Python data types at a glance
| Type | What it holds | Example |
|---|---|---|
int | Whole numbers, positive or negative, of any size | 42, -7, 1_00_000 |
float | Numbers with a decimal point | 98.6, 0.5 |
str | Text (a string of characters) | "Kochi", 'A' |
bool | Truth values | True, False |
NoneType | The “no value” marker | None |
list | An ordered, changeable collection | ["idli", "vada"] |
tuple | An ordered collection that cannot change | (11.0, 79.8) |
dict | Key–value pairs | {"city": "Hosur"} |
set | Unique items with no order | {1, 2, 3} |
This lesson focuses on the first five, the “simple” types. The collections get lessons of their own: lists and tuples and dictionaries and sets.
Checking a type with type() and isinstance()
The built-in type() function tells you the type of any value. isinstance(value, type) answers yes or no, which is handier inside an if.
population = 1_250_000
temperature = 31.5
city = "Thanjavur"
is_capital = False
mayor = None
print(type(population))
print(type(temperature))
print(type(city))
print(type(is_capital))
print(type(mayor))
print(isinstance(temperature, float))<class 'int'> <class 'float'> <class 'str'> <class 'bool'> <class 'NoneType'> True
The underscores in 1_250_000 are only for readability; Python ignores them. They are handy for large rupee amounts, and you can group digits the Indian way too, as in 1_00_000 for one lakh.
Numbers: int and float
An int has no size limit, so Python can work with very large numbers without any special code. A float stores decimals, but in binary, so some simple decimals cannot be stored exactly. That is why 0.1 + 0.2 is not exactly 0.3. This happens in almost every programming language, not only Python.
print(2 ** 100)
print(7 / 2)
print(0.1 + 0.2)
print(round(0.1 + 0.2, 2))
from decimal import Decimal
print(Decimal("0.10") + Decimal("0.20"))1267650600228229401496703205376 3.5 0.30000000000000004 0.3 0.30
- Dividing with
/always gives afloat, even6 / 3, which is2.0. - Use
round(value, 2)when you only need to display two decimals. - For money calculations where every paisa must be right, use
Decimalfrom the standard library and create it from a string, as shown above.
Text: str
A string is text inside single or double quotes; both mean the same thing. Pick one style and stick with it. Strings are covered fully in Python Strings. For now, remember that "25" with quotes is text, while 25 without quotes is a number.
True, False and None
A bool has only two values, True and False, always with a capital first letter. Comparisons such as age >= 18 produce a bool. None means “nothing here yet”, for example a delivery date that is not known. It is not the same as 0, False or an empty string.
Truthy and falsy values
Python can treat any value as true or false in a condition. The “falsy” values are False, None, 0, 0.0, the empty string "" and empty collections such as [] and {}. Everything else is “truthy”. bool() shows you which is which.
Converting between types
Data often arrives as text: from a form, a file or the keyboard. Before you can do maths with it, convert it with int(), float() or str(). This is called type conversion or casting.
qty_text = "3"
price_text = "149.50"
qty = int(qty_text)
price = float(price_text)
print(qty * price)
print("2" + "2")
print(int("2") + int("2"))
order_no = 1045
print("Order #" + str(order_no))
print(int(9.99))
print(bool(0), bool(""), bool("0"), bool([]), bool(None))448.5 22 4 Order #1045 9 False False True False False
Watch out: int() cuts off the decimal part instead of rounding, so int(9.99) is 9. And it cannot read decimal text: int("12.5") raises a ValueError. Use float("12.5") first, or round() if you want the nearest whole number.
When types do not mix
Python will not guess what you mean when two types do not fit together. "Age: " + 25 stops with TypeError: can only concatenate str (not "int") to str. This strictness is helpful: other languages might quietly produce a strange result, while Python points at the exact line. Fix it by converting, as in "Age: " + str(25), or more simply with an f-string: f"Age: {25}". Whenever you see a TypeError, print the type() of each value on that line and the cause is usually obvious.
Tip: Notice that bool("0") is True: it is a non-empty string, even though it contains the digit zero. Convert text to a number first if you mean the number.
With the types in place, the next step is working with text in detail: Python Strings.
