Data Types in Python

Learn about all the built-in data types in Python, including strings, numbers, booleans, sequences, sets, dictionaries, and more.

Loading...
Data Types in Python

What are Data Types?

A data type tells Python what kind of value something is.

For example:

name = "John"
age = 27
is_learning = True

Here:

  • "John" is a string
  • 27 is an integer
  • True is a boolean

Python figures out the type automatically based on the value you assign.

You can check the type of a value using the type() function:

name = "John"
age = 27
 
print(type(name))
print(type(age))
 
# Output:
 
# <class 'str'>
# <class 'int'>

Let’s look at the built-in data types Python provides.

Python’s Built-in Data Types

1. Text Type - str

str is used for text.

name = "John"
message = 'Hello'

You can use either single or double quotes to create a string.

2. Numeric Types - int, float, complex

Python has three main numeric types.

int - Whole numbers:

age = 27
temperature = -5

float - Numbers with decimal points:

price = 99.99
height = 5.6

complex - Numbers with a real and imaginary part:

number = 2 + 3j

You probably won’t use complex numbers often as a beginner, but it’s one of Python’s built-in numeric types.

3. Boolean Type - bool

A boolean has only two possible values:

True
False

For example:

is_logged_in = True

Booleans are commonly used when making decisions in your code.

4. Sequence Types - list, tuple, range

Python has several types that can store or represent a sequence of values.

list - A collection of values that you can change:

fruits = ["apple", "banana", "orange"]

tuple - A collection of values that you cannot change after creating it:

colors = ("red", "green", "blue")

range - Represents a sequence of numbers:

numbers = range(5)

We’ll explore lists, tuples, and ranges in more detail later.

5. Set Types - set, frozenset

A set is a collection of unique values.

numbers = {1, 2, 3, 3}
print(numbers)
 
# Output:
 
# {1, 2, 3}

Notice that 3 appears only once.

A set can be changed, while a frozenset cannot be changed after it is created.

6. Mapping Type - dict

A dictionary (dict) stores data as key-value pairs.

person = {
    "name": "John",
    "age": 27
}

Here:

  • "name" is a key
  • "John" is its value
  • "age" is a key
  • 27 is its value

We’ll learn more about dictionaries later.

7. None Type - NoneType

None represents the absence of a value.

result = None

You can think of it as “there is no value here right now.”

8. Binary Types - bytes, bytearray, memoryview

These types are used when working with binary data, such as data from files or other sources.

  • bytes - An immutable sequence of bytes.
  • bytearray - A sequence of bytes that can be changed.
  • memoryview - Allows you to work with data stored in bytes or bytearray without making a copy.

A Pattern Worth Noticing

Some Python data types can be changed after they are created, while others cannot.

Types like list, set, dict, and bytearray can be changed. These are called mutable types.

Types like str, tuple, frozenset, and bytes cannot be changed after they are created. These are called immutable types.

You’ll come across this distinction a lot as you write more Python, so it’s good to know the terms early.

Summary of Data Types

Data Type Description Example
str String type for textual data "Hello World!"
int Integer type for whole numbers 5
float Float type for decimal numbers 5.5
complex Complex number type (real + imaginary part) 5j
bool Boolean type, represents True or False True or False
list Mutable sequence of items ["one", "two", "three"]
tuple Immutable sequence of items ("one", "two", "three")
range Sequence of numbers (usually used in loops) range(5)
set Unordered collection of unique items {"one", "two", "three"}
frozenset Immutable set frozenset({"one", "two", "three"})
dict Key-value pair collection {"name": "John", "age": 25}
NoneType Represents no value (None) None
bytes Immutable binary data b"hello"
bytearray Mutable binary data bytearray(b"hi")
memoryview View of binary data memoryview(b"hi")

👉 Next tutorial: Typecasting in Python