Tutorials › Python Tutorial › Python Data Types

Python Data Types

9 min read · Lesson 5 of 24

Every value in Python has a data type. The type decides what you can do with the value: you can multiply two numbers, but not two names; you can make a word uppercase, but not a number. Knowing the type of your data prevents a lot of confusing errors.

The built-in types

Python comes with these core types. You will meet each one in detail later in the tutorial.

CategoryTypeExample
Textstr"Eldoret"
Numbersint, float, complex42, 3.5, 2+3j
Sequenceslist, tuple, range[1, 2], (1, 2), range(5)
Mappingdict{"name": "Amina"}
Setsset, frozenset{1, 2, 3}
BooleanboolTrue, False
NothingNoneTypeNone
Binarybytes, bytearrayb"hi"

Checking a type with type()

The built-in type() function tells you what type a value is.

Example PYTHON
print(type("Eldoret"))
print(type(42))
print(type(3.5))
print(type(True))
print(type(None))
Result
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'NoneType'>
Try it Yourself »

The word class appears because in Python every type is a class. You will learn what that means in Python Classes and Objects.

Types are set automatically

You never announce the type yourself. Python looks at the value and picks the type for you.

Example PYTHON
county = "Nyeri"             # str
population = 759164          # int
avg_rainfall = 1050.5        # float
is_capital = False           # bool
crops = ["tea", "coffee"]    # list
location = (-0.42, 36.95)    # tuple
farmer = {"name": "Kamau", "acres": 3}  # dict
grades = {"A", "B", "C"}     # set

for value in [county, population, avg_rainfall, is_capital,
              crops, location, farmer, grades]:
    print(type(value).__name__)
Result
str
int
float
bool
list
tuple
dict
set
Try it Yourself »

Using type(value).__name__ gives just the type name without the <class ...> wrapper. Do not worry about the for loop yet; it simply repeats the print() for each value.

Setting a specific type

Sometimes you want a value of a particular type. Each type name can also be called like a function to create a value of that type. This is called casting, and it is covered in Python Numbers and Casting.

Example PYTHON
a = str(2024)
b = int("15")
c = float(7)
d = list("abc")
print(a, type(a).__name__)
print(b, type(b).__name__)
print(c, type(c).__name__)
print(d, type(d).__name__)
Result
2024 str
15 int
7.0 float
['a', 'b', 'c'] list
Try it Yourself »

Why types matter

The same operator can behave differently depending on the types involved. The + operator adds numbers but joins strings:

Example PYTHON
print(5 + 5)
print("5" + "5")
print([1, 2] + [3])
Result
10
55
[1, 2, 3]
Try it Yourself »

Mixing types that do not fit together raises an error. For example, "5" + 5 gives a TypeError because Python will not guess whether you meant 10 or "55". You must convert one side first.

Warning: Numbers read from a file, a form or a spreadsheet often arrive as strings. Always convert them with int() or float() before doing arithmetic.

The None value

None is a special value meaning "nothing" or "no value yet". It is useful as a placeholder.

Example PYTHON
winner = None
print(winner)
winner = "Faith Kipyegon"
print(winner)
Result
None
Faith Kipyegon
Try it Yourself »

Checking a type with isinstance()

To ask "is this value of type X?", use isinstance(). It returns True or False.

Example PYTHON
amount = 1500
print(isinstance(amount, int))
print(isinstance(amount, str))
Result
True
False
Try it Yourself »
Tip: Prefer isinstance() over comparing type(x) == int in real code. It also works correctly with subclasses, which you will meet later.

Exercises

Exercise

Fill in the function that shows the type of a value.

print((3.14))
Exercise

What type name does Python give to True and False?

<class ''>

Next: Python Numbers and Casting.

Sign in or create a free account to track progress, earn XP, badges and a certificate.