🎓 A1Learner
🔍

Python Data Types

A data type is the family a value belongs to. The type() function shows the family of a value.

Text and numbers

These are the three families from earlier lessons.

name = "Ada"
age = 36
height = 1.68
print(type(name))
print(type(age))
print(type(height))

Sequences

A sequence is a group of values in order.

colors = ["red", "green"]
pair = (3, 4)
steps = range(5)
print(type(colors))
print(type(pair))
print(type(steps))

Labels, sets, and yes-or-no

A dict uses labels. A set is a group. A bool is True or False.

price = {"apple": 2}
tags = {"fruit", "sweet"}
ready = True
print(type(price))
print(type(tags))
print(type(ready))

No value yet

None means no value yet. Its type has its own name.

winner = None
print(type(winner))

Example

print(type("hello"))
print(type(42))
print(type(3.5))
print(type(2 + 3j))
print(type([1, 2]))
print(type((1, 2)))
print(type(range(3)))
print(type({"a": 1}))
print(type({1, 2}))
print(type(True))
print(type(None))

Key points

  • Type: the family a value belongs to, like text or whole numbers
  • Text type: str holds text in quotes
  • Number types: int, float and complex hold numbers
  • Sequence types: list, tuple and range hold values in order
  • Mapping type: dict holds pairs of a label and a value
  • Set type: set holds a group with no promised order
  • Bool type: bool holds True or False
  • NoneType: the type of None, which means no value yet
  • Use type() to ask Python which family a value belongs to

In this example, each line prints the type of one value. The first four values are text and the three number types. The next lines cover the sequence types and the dict. The set, the bool and None come last. You now have one printed type for every family on the map.

Note

In Python, you do not declare a type.

Python reads the type from the value you store.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

What does type(7) show?