Introduction to Data Types
You have already used three of them without being told their family name: 42, 3.14, 'hello', True. Python sorts every value in the world into a small number of data types. Learn the family tree once, and nothing that comes later is a stranger.
1What is a data type?
A data type is simply what kind of value something is. Python needs to know this, because the kind decides what it is allowed to do with the value.
Think about +. Give it two numbers and it adds them. Give it a number and a piece of text and it refuses. Nothing is wrong with the + — it is the types on either side that decide the outcome. That is why types matter from day one.
42). A data type is the kind of thing it is (an integer). Every value in Python has exactly one type.2The family tree
Every value you will ever write in Python belongs to one of five families. Here is the whole picture. Tap any box: you will see an example, and what Python calls it.
Numbers you can do maths with.
Many items, kept in order, one after another.
Each item is found by a key, not by its position.
A bag of items with no duplicates and no order.
The value that means 'nothing here'.
You already know four of them — int, float, bool and str. The rest (lists, tuples, dictionaries, sets, None) get whole lessons of their own later. For now it is enough to know they exist, and which family they sit in.
boolean is not drawn beside integer; it is drawn underneath it, as a child. That is not a drawing mistake. In Python a boolean genuinely is a kind of integer: True counts as 1 and False counts as 0. Watch:total = True + True
mixed = False + 10
print(total)
print(mixed)2 10
Two Trues add up to 2. That is not a bug and it is not a trick — it is the tree telling the truth. A boolean is an integer that has made up its mind. (It still has its own type name, bool, which you can check with type() in a moment.)
3The Numeric family
These are the values you can do arithmetic with. There are four, and you already know two of them.
intA whole number. No decimal point.
3 · 0 · -15 · 1000000floatA number with a decimal point.
3.75 · -0.5 · 2.0boolOnly two values ever exist.
True · FalsecomplexFrom higher maths. Not in your syllabus — just say hello and walk on.
3 + 7jTrue or False.4Sequential, Mapping, Set and None
You will meet these properly in later chapters. Right now, one line each is all you need — enough to recognise them when they turn up.
| Family | Type | Looks like | In one line |
|---|---|---|---|
| Sequential | str | 'hello' | Text. A sequence of characters, in order. |
| Sequential | list | [10, 20, 30] | Many items in square brackets. You can change it later. |
| Sequential | tuple | (10, 20, 30) | Many items in parentheses. Locked — it cannot be changed. |
| Mapping | dict | {'name': 'Ramesh', 'marks': 87} | Key and value pairs. You look up by the key: 'name' gives 'Ramesh'. |
| Set | set | {10, 5, -2, 18} | A bag of items. No duplicates, no order. |
| None | NoneType | None | Means 'no value at all'. Not 0, not empty text. |
[ ] is a list. Parentheses ( ) is a tuple. Curly { } is a dictionary or a set. The brackets are how you spot the type from across the room.5Ask Python: the type() function
You never have to guess a type. You can simply ask Python, using type().
type(). A word, and then parentheses ( ) right after it. You know what that means by now: it is a function, exactly like print(). That is the rule you learnt in the print lesson, and it has already paid for itself — you recognised a brand new word without being told a thing about it.And it works the same way as print(). Whatever you put inside the parentheses is the argument. print() takes an argument and shows it. type() takes an argument and tells you what kind of value it is.
print(3.75)Shows you the value.
3.75type(3.75)Tells you the type of that value.
<class 'float'> So the family tree is not something you have to memorise blindly. Any time you are unsure, hand the value to type() and let Python answer.
print(type(3))
print(type(3.75))
print(type(3 + 7j))
print(type('hello'))
print(type(True))<class 'int'> <class 'float'> <class 'complex'> <class 'str'> <class 'bool'>
Read the answer past the word class: what matters is the name in the quotes — int, float, str, bool. That is the type. (The word class has its own meaning in Python, and it can wait.)
Try it yourself. Ask Python about any value you like:
type(42) and then type('42'). They look almost the same, and Python calls one an int and the other a str. The quotes changed the type — and the type changes what you are allowed to do with the value.6Recap
True + True is 2. And type(value) tells you the type of anything, any time.What does type('42') report?
Which family does a boolean sit under in the tree?
What does True + True give?