Python Data Types

In Python programming, a data type is the classification of data that defines the possible operations that can be done on the object.

Python has various built-in types in the interpreter: numerics, sequences, mappings, classes, instances and exceptions.

This tutorial will show you all the Python data types and each of their characteristics.


Subscribe to my Newsletter


Python Data Types by Category

Built-in Python data types can be categorized based on their sets of characteristics: mutable, possible arithmetic operations, allowing indexing, etc.

CategoryType
text typestr
numeric typesint, float, complex
sequence typestuple, list, range
iterator typedict
sets typeset, frozenset
binary typesbytes, bytearray, memoryview
boolean typebool
date and time typesdate, time, datetime, timedelta, tzinfo
class typeclass (user-defined type)
instances typesobject (base for all objects), instance
exception typesException (base for all built-in exceptions)
NoneTypeNoneType

Showing the Data Type

To get the data type of an object in Python, use the type() method.

x = 1
print(type(x))

Python Data Types Characteristics

Each data type in Python has its own set of characteristics. Here are the characteristics of the most often used data types.

For example, certain data type can be mutable( e.g. lists), others immutable (e.g. sets). Similarly, some allow indexing and others not.

Data TypeData Type CategoryImmutableAllow Arithmetic OperationsAllow IndexingAllow Slicing
listSequenceNoYesYesYes
tupleSequenceYesNoYesYes
rangeSequenceYesNoYesYes
dictMappingNoNoYesNo
setSetNoNoNoNo
frozensetSetYesNoNoNo
intNumericYesYesYesYes
floatNumericYesYesNoNo
complexNumericYesYesNoNo
strTextYesNoYesYes
bytesByteYesNoYesYes
bytearrayByteNoYesYesYes
memoryviewByteNoYesYesYes
boolBooleanYesYesNoNo

Python Data Types Methods

Python Data Types come built-in with various sets of methods that can be used on their objects. Below are examples of Python methods that can be used on each data type’s object.

Data TypeExample Built-in Methods
listappend(), insert(), pop()
tuplecount(), index()
rangestart, stop, step
dictclear(), keys(), values()
setadd(), remove(), union()
frozensetintersection(), isdisjoint()
intbit_length(), to_bytes()
floatas_integer_ratio()
complexreal, imag
strlower(), upper(), strip()
bytesdecode(), hex()
bytearrayappend(), pop()
memoryviewcast()
datetoday()

When To Use Each Data Type?

Each data type in Python has its own set of rules and use. Below is a table showing when to use each data type and how to set it up in Python.

Data TypeWhen to Use ItSetting the Data Type
listWhen you need a mutable ordered sequence of elements[1, 2, 3]
tupleWhen you need an immutable ordered sequence of elements(1, 2, 3)
rangeWhen you need an immutable sequence of numbersrange(10)
dictWhen you need to map keys to values, and you need to be able to change the mappings{‘key1’: ‘value1’, ‘key2’: ‘value2’}
setWhen you need a mutable set of unique elements{1, 2, 3}
frozensetWhen you need an immutable set of unique elementsfrozenset({1, 2, 3})
intWhen you need a whole number with no decimal point42
floatWhen you need a number with a decimal point3.14
complexWhen you need a number with a real and imaginary part2 + 3j
strWhen you need a string of characters“hello world”
bytesWhen you need a sequence of bytesb”hello”
bytearrayWhen you need a mutable sequence of bytesbytearray(b”hello”)
memoryviewWhen you need to access the internal memory of an objectmemoryview(b”hello”)
boolWhen you need a boolean value (True or False)True or False

Data Types Constructors

To specify data types in Python, you can use Python constructor functions:

Data TypeConstructor Example
listmy_list = list([1, 2, 3, 4])
tuplemy_tuple = tuple((1, 2, 3, 4))
rangemy_range = range(10)
dictmy_dict = dict({‘key1’: ‘value1’, ‘key2’: ‘value2’})
setmy_set = set([1, 2, 3, 4])
frozensetmy_frozenset = frozenset([1, 2, 3, 4])
intmy_int = int(10)
floatmy_float = float(3.14)
complexmy_complex = complex(2, 3)
strmy_str = str(‘Hello, World!’)
bytesmy_bytes = bytes([0x41, 0x42, 0x43, 0x44])
bytearraymy_bytearray = bytearray([0x41, 0x42, 0x43, 0x44])
memoryviewmy_memoryview = memoryview(b’Hello, World!’)
boolmy_bool = bool(True)
Enjoyed This Post?