Learn

Short lessons with real code you can edit and run on the page.

Data Types in Python

Lesson 3 Python 3.14 Runs in your browser Updated
In short

A data type is the kind of value a variable holds, such as text, a whole number or a list. Python's built-in types are often grouped into eight categories, and every value knows its own type, which is what type() reports.

Key facts

  • In Python, values have types and variables don't. type(x) reports the type of whatever x holds right now.
  • The eight common categories are text, numeric, sequence, mapping, set, boolean, binary and None.
  • Converting is done by calling the type's name as a function, as in int("42"), str(7) and float("2.5").
  • Lists, dictionaries and sets can be changed in place. Strings, numbers and tuples can't.

What are the data types in Python?

Python's built-in data types are often grouped into eight categories. Most beginners start with str, int, float, bool and list, and meet dict soon after. The table lists all eight categories, the types in each and an example value.

Python built-in data types by category
CategoryTypesExample value
Textstr"Ada"
Numericint, float, complex36, 1.75, 2+3j
Sequencelist, tuple, range["red", "green"], (3, 4), range(10)
Mappingdict{"name": "Ada"}
Setset, frozenset{1, 2, 3}
BooleanboolTrue, False
Binarybytes, bytearray, memoryviewb"data"
NoneNoneTypeNone

Empty curly braces, {}, make an empty dictionary, so an empty set is written set().

Every value carries its type with it. Ask with type() and Python gives back the type, which prints as <class 'str'> and so on.

Python
name = "Ada"
age = 36
height = 1.75
likes_python = True
nothing = None
print(type(name))
print(type(age))
print(type(height))
print(type(likes_python))
print(type(nothing))
Output
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'NoneType'>

The collection types hold other values. A list keeps order and can grow, a tuple keeps order and can't change, a dictionary maps keys to values, and a set keeps only unique items, in no fixed order. Each gets its own lesson later; for now, notice how each is written and what type() says.

Python
colors = ["red", "green", "blue"]
point = (3, 4)
person = {"name": "Ada", "age": 36}
unique = {1, 2, 2, 3}
print(type(colors), colors)
print(type(point), point)
print(type(person), person)
print(type(unique), unique)
Output
<class 'list'> ['red', 'green', 'blue']
<class 'tuple'> (3, 4)
<class 'dict'> {'name': 'Ada', 'age': 36}
<class 'set'> {1, 2, 3}

The set dropped the duplicate 2 on its own. That is what a set is for.

How do I check a value's type?

Use type() to look, and isinstance() to ask a yes-or-no question. isinstance() is usually the better test, because it also says yes for subtypes. For example, bool is a subtype of int, so isinstance(True, int) is True.

Python
x = 42
print(type(x))
print(isinstance(x, int))
print(isinstance(True, int))
print(isinstance(x, str))
Output
<class 'int'>
True
True
False

How do I convert between data types?

Call the target type like a function. int(), float(), str() and bool() each take a value and return a new value of their own type, and the original stays as it was.

Python
print(int("42") + 1)
print(float("2.5") * 2)
print(str(7) + " days")
print(int(3.99))
print(round(3.99))
print(bool(0), bool(""), bool("hi"))
Output
43
5.0
7 days
3
4
False False True

Two of those lines surprise people. int(3.99) chops the decimals off instead of rounding, so use round() when you want the nearest whole number. And bool() treats False, zero, None and empty things as False and almost everything else as True, which is how a program can check whether a string is empty, as the if, elif and else lesson shows.

Which data types can be changed?

Lists, dictionaries and sets are mutable, so their contents can change in place. Strings, numbers, booleans and tuples are immutable, so every "change" produces a new value and the old one stays untouched.

Mutable and immutable Python data types
Mutable (can change in place)Immutable (can't change in place)
list, dict, set, bytearraystr, int, float, bool, tuple, frozenset, bytes
Python
colors = ["red", "green"]
colors.append("blue")
print(colors)
word = "hello"
print(word.upper())
print(word)
Output
['red', 'green', 'blue']
HELLO
hello

append() changed the list itself. upper() returned a new string and left word alone, which is why the last line still prints lowercase. To keep an uppercase version, assign the result back with word = word.upper().

What is None in Python?

None is Python's value for "nothing here". A variable set to None exists but holds no useful value yet, and a function can give back None, as print() does even though it shows text on the screen. Its type is NoneType, and there is only ever one None, so you compare with is None rather than == None. The booleans and comparisons lesson explains why.

Common mistakes with data types in Python

Two of these four mistakes with Python data types raise an error, and two run without one.

  • TypeError: can only concatenate str (not "int") to str. Text and a number were joined with +. Convert one side first, as in "42" + str(1) or int("42") + 1, or use an f-string.
  • ValueError: invalid literal for int() with base 10: '3.5'. int() only reads whole numbers from text. Use float("3.5"), then pass the result to int() if you need a whole number.
  • Expecting int(3.99) to give 4. It gives 3, because int() truncates. round() rounds.
  • Comparing with == None. It works, but is None is the usual style, and code-checking tools called linters flag == None.

Run both broken lines to see the errors. The last line of each error message names the error and the value or type that caused it.

Python
print(int("3.5"))
Output
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    print(int("3.5"))
          ~~~^^^^^^^
ValueError: invalid literal for int() with base 10: '3.5'
Python
print("42" + 1)
Output
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    print("42" + 1)
          ~~~~~^~~
TypeError: can only concatenate str (not "int") to str

Exercise

The variable age holds the text "30", not the number 30. Change the print line so the program turns the text into a whole number with int() and prints the age one year from now, 31.

Python
age = "30"

print(age + 1)
Output
Show the solution
age = "30"

print(int(age) + 1)

Quiz

This quiz has 5 questions. Pick an answer to see why it is right or wrong.

  1. 1What does this program print?

    print(type(3.0))
  2. 2What does this program print?

    print(int(7.8))
  3. 3Which of these types can be changed in place?

  4. 4What does bool("") evaluate to?

  5. 5What does this program print?

    print({1, 2, 2, 3})

Frequently asked questions

What are the eight categories of data types in Python?
The eight common categories are text (str), numeric (int, float, complex), sequence (list, tuple, range), mapping (dict), set (set, frozenset), boolean (bool), binary (bytes, bytearray, memoryview) and None (NoneType).
How many data types are there in Python?
The eight common categories hold fifteen built-in types, and Python has many more for special jobs. Programs can also create their own types with classes. Most programs use str, int, float, bool, list and dict for nearly everything.
Which Python data types should I learn first?
Start with int, float, str and bool, then list. Dictionaries come next, because much real-world data is made of labeled values, such as a person's name and age.
Does Python have static types?
No. Python is dynamically typed, so a name has no fixed type and can refer to a value of a different type later. Optional type hints, labels that say what type a name should hold, let code editors warn you about mistakes, but Python does not enforce them.
What is the difference between int and float in Python?
An int is a whole number with no fixed size limit. A float has a decimal point and is stored as a fixed-size binary fraction. Most decimal fractions, such as 0.1, can only be stored approximately, which is why 0.1 + 0.2 prints as 0.30000000000000004.
What does NoneType mean in Python?
NoneType is the type of the single value None, which stands for "no value". A function that ends without returning a value gives back None.