Select Page

15 Data Types in Python – All in one

In this tutorial, you’ll learn about all of the available data types in Python. There are a total of 15 built-in data types, and we can classify them in 8 different sections, as listed here. This tutorial will be a quick overview of all the data types in Python, as some of them are very important in Python that each of them need a dedicated detailed tutorial. So, let’s get started!

What is Data Type

Data is information. But depending on the characteristics and use case, data can be classified. Some data are numerical, some are text-based, some are structured, and so on. Let’s take an example.

Animal is a general term. But are all the animals the same? Dog is a species, one kind of animal. They have some characteristics special to them. They can bark, are friendly, protective, can be kept in the house, and so on. On the other hand, Tiger is another species, another kind of animal. They have a different set of characteristics. But both dogs and tigers are animals. So they are different Animal TYPES.

Every data object (a variable or a constant) has a type that describes how to store it in the memory, what operations can be applied upon them, what they are capable of and not capable of, and so on. If you learn about different data types, you can choose which one to use to meet your need. You won’t try to play with a tiger, right?

Built-in Data Types of Python

The concept of data types is very important in every programming language. Only the values can have data types. But as variables contain or refer to values, they too can have data types.

Here we’ll be talking about the built-in data types of Python. Built-in means the ones that come with Python by default. And as you can guess, you can create some custom data types. More about them in Classes and Objects.

Python has 15 built-in data types, they fall in some categories:

Data Type NameHow Presented in Programming
Text Typestr
Numeric Typesint, float, complex
Sequence Types list, tuple, range
Mapping Typedict
Set Typesset, frozenset
Boolean Typebool
Binary Typesbytes, bytearray, memoryview
None TypeNone

Checking the Data Type of an Object

Before you start exploring data types of Python, let me introduce you to a built-in function of Python that checks the type of a variable or value. The function is: type()

Don’t worry about what a function or a built-in function is. Just keep in mind, it works! How? We will discuss this in a later tutorial in detail.

Pass a value or variable inside the parentheses of the type() function, it will return you the type of the object. In a text editor (e.g. left side in the image), use a print() function to see the output. But in REPL (e.g. right side in the image), you don’t need it, as REPL executes one command at a time. I’m using this online compiler.

checking the data type of an object with python built-in type() function
Checking the type of different objects

Notice that the type() function returns something like <class ‘data-type’> . It actually says that this object (say the value 1000 or variable x) belongs to that class (say int or float). You’ll learn more about classes and objects in a future tutorial, so don’t worry about it.

Now, it’s time to explore all the built-in data types in Python!

Strings in Python – str

Strings are a sequence of characters. When you work with textual data or information, the string data type comes in handy. In short, texts are called strings in computer science. Whatever you’re reading here is text, hence known as strings in programming. It is one of the most important data types in Python.

The format of string in Python is to wrap a text with quotes. Generally, a string is wrapped in single-quotes or double-quotes. But if you’ve read the previous tutorials, Getting Started with Python Programming, and Comments in Python: All Possible Ways, then you already know that triple single-quotes and triple double-quotes are also allowed.

  • Examples of String in single quotes
print('')                # Empty string
print('123')             # Wrapped in quotes, so string
print('Hello, World!') 
print(type('a string))   # Checking the type
  • Examples of string in double quotes
var1 = "Hello, World"                      # var1 also becomes a string
var2 = "123"
print(type(var1), type(var2), sep='\n')    # Reducing one line of code
  • Examples of string in triple single-quotes
text = '''\
this is
a multiline string

print(text, type(text), sep='\n')
  • Examples of string in triple double-quotes
msg = """I'll start here
and continue as much as I want
as a string, I don't have a limit"""

print(msg, type(msg), sep='\n')

A string in Python (not in most other languages) can be as long as you want, from zero character to infinity. If your device has enough memory, then you don’t need to worry about the length.

That’s all for now. For detailed characteristics and operations about strings, stay tuned.

Numbers in Python – int, float, complex

Numbers, the most important data for programmers, hence the most important data types in Python too. All types of numeric data fall under this section. Numbers have three different data types. They are:

int – Integers

int stands for integers. This represents whole numbers, i.e. positive, negative, and zero. In Python 3, there is no limit to how long an integer value should be. You can use as long number as you wish. The only limitation is your device memory.

The structure of an integer is some raw digits. You don’t need any special character to tell the Python compiler that its a digit. Keep in mind that a decimal integer (10 base number system) can’t start with a zero unless the number itself is zero. Let’s see some examples of the int data type.

n = 1234567890
m = 9876543210
summation = n + m
print(type(n), type(m), type(summation), sep='\n')

Here’s how it looks like.

examples of some integers in python
integer example

Let’s see some examples of integers in REPL. Notice that wrapping the number converts it into a string. This way you can represent invalid numbers.

some invalid ints and how to handle them
some invalid ints and how to handle them

float – Floating Point Numbers

Floats are similar to ints, but they have a decimal point within them. Here are some examples. Notice that 100 is an integer, while 100.0 is a float. Although the values are equal the later one has a decimal point, hence, that’s a float.

floats in python
floats in Python

In mathematics, the scientific notation of a large number, say 3 million, is 3×106. Similarly, for a small number, say 3 ml, it’s 3×10-3. To represent the 10 bases in Python, you can use either e or E. Even if you don’t use any decimal point, this scientific notation behaves as a float in Python. Note that e or E must have a number before and after it to behave like a 10 base. Let’s see some examples.

scientific notations as float in python
scientific notations as floats

Python floats have a limitation. The Python floats are 64-bit double-precision values. So the maximum float is 1.8×10308 and the minimum is 5.0×10-324 which is actually zero. A floating-point number is accurate up to 15 decimal points. Above that, the floats get truncated.

As computers store numbers in the binary number system, float sometimes cannot be 100% accurate. For example, 0.1+0.2 = 0.3. But in any programming language, this will return False. Let’s see:

Floating point arithmetic: Issues and limitations
Floating point arithmetic

To learn more about this, read the official Python documentation for Floating Point Arithmetic: Issues and Limitations.

complex – Complex Numbers

All the complex number maths you probably solved falls under this data type. The structure is the same as what you’ve used, but you must use only j to denote the imaginary part. So the structure is: x + yj.

complex number in python
complex number
Join Facebook Group

Public Group

A group optimized for learning purposes. Track your progress by joining a unit

Follow on Quora

Public Space

Best place to ask questions! Join the built-in community to get help from others

Sequence Types in Python – list, tuple, range

A sequence is a collection of data. Sometimes it’s easy to declare only one variable for multiple values rather than declaring one variable per value. For example, say a math teacher storing math exam numbers for 100 students. If he uses one variable per student, then he needs a total of 100 different variables. When working with this data, he can’t remember all the 100 variables. Even if he does, it’s not practical. Alternatively, he can store those 100 different numbers in a list and can access them depending on the order they have been saved. Keep reading for a better understanding.

There are different sequence types in Python depending on how you want to store multiple values and what kind of operations you want to apply to them. The basic types include list, tuple, and range. Some other sequence types are string, dictionary, set, bytearray. Let’s discuss the basic ones in this section.

list – Listing Multiple Values

List is an ordered sequence of data. It is known as array in other programming languages. The best thing about Python list is that it can store any type of data in it. The structure of a list is very simple, separate the values by comma and wrap them with square brackets [ ]. It is one of the amazing data types in Python. Here’s an example of how it works:

items = [1, 'one', 3.9, '012']

Each of the values in a list has a unique index, which will help you to use that value. Indexing starts from zero. To extract one data from a list, use this format: list_name[index]

Python list
Python list

Notice that if you try to index a position that is not available in the list, Python will show you an IndexError. Which says the list index is out of its range.

Although the sequence is a list here, all the values inside it still reserves their type. Try this out yourself:

items = [1, 'one', 3.9, '012']

print(type(items[0]), type(items[1]), type(items[1]), type(items[1]))

Python also supports negative indexing. Negative indexing starts from the right side and from -1.

negative indexing
Negative indexing

List is mutable. That means, the values of a list can be changed. Say I want to add an extra value at the end of the previous list, and also change 3.9 to 9.3.

list is mutable
List is mutable

The concept of list is so huge that it needs a dedicated tutorial, or maybe 2. Keep updated for the Data Structure section.

tuple – Immutable List

The concept of tuple is the same as list, but the only difference, tuple is immutable. That means, once a tuple is declared, its values cannot be changed afterwards. So tuple is safer, efficient and works faster. Instead of using square brackets, tuple uses parentheses in declaration. Everything else is the same.

Python tuple
Python tuple

Notice that Tuple worked properly till 5th line. In line 6 and 7, I tried to modify the tuple and Python raises a TypeError.

More about tuple will be discussed in Data Structure section soon.

range() – A range of numbers

range() is one of the simplest data types in Python. It is actually a built-in function of Python. It is used to generate a sequence of integer numbers. In Python 3, the range() function creates an object which is then converted into other datatype or accessed by iteration with a for loop. You’ll learn about the for loop soon. For now, let’s see how the range() function generates a sequence of numbers.


range([start], stop, [step])

Here are a few points to keep in mind when working with range().

  1. Range returns an object, which is not a sequence of numbers, but represents a sequence of numbers
python range data type returns an object of sequential data
  1. The returned object can be converted into other sequential data types such as list, tuple, set
the range data can be converted into other sequential data types such as list, tuple, and set
  1. The [Start] and [Step] arguments of range() are optional. Default values are 0 and 1 respectively. Notice that if only one argument is passed, Python treats it as Stop, if two arguments are passed, then [Start] Stop respectively. And if all of the three are passed, then as [Start] Stop [Step].
the range function has 2 optional arguments, Start and Step. The default values are 0 and 1 respectively.
  1. [Start] means from where the sequence should start. Stop means where it should stop. [Step] indicates the difference between every two numbers.
  2. The Stop number is not included in the sequence.

range(10) starts the sequence from 0, increases by 1, and continues till 10, but does not include 10.

There’s one more use case left for the range data type. You’ll learn about that in the for loop tutorial.

dict – Dictionary or Hash Table

The Python dictionary data type is also known as Hash Table in many other programming languages. It is one of the amazing data types in Python! Think of a dictionary book. There you’ll find a word-meaning pair for each of the words. To know the meaning, you search the dictionary with the word. The concept of dict data type in Python is the same.

A dictionary is a collection of unordered values. But for each value, there must be a key. This key is used to index the dictionary and find out the value. Otherwise, you can’t retrieve a value from a dictionary as the values are unordered. That’s why a dictionary data type is called a collection of key-value pairs.

Why would you need a dictionary instead of a list?

Say you’re buying a bunch of products and listing their prices. If you use a list, you only know the costs, but don’t know which price belongs to which product. But if you use a dictionary, use the product names as keys and assign their corresponding prices. Thus you now know which product costs how much.


variable = {"key1": "value1", "key2": 2, "key3": 3}

Keep in mind that the key must be a string, whereas the value can be anything, even another dictionary.

Let’s see how much it may cost to build a PC. Store the values in a dictionary.

costs = {
  'CPU': 434.99,
  'GPU': 3099,
  'MotherBoard': 179.99,
  'Ram': 100, 
  'SSD': 100,
  'Cooler': 69.69 

Notice that you can break the entries of a dict data type in this way to make your code clean and readable. It even doesn’t matter how much space you use before each entry, as the entries are within the curly brackets.

To extract values from a dictionary, index the dictionary with the keys. The syntax is the same as the list. Let’s see them in Sublime Text.

Python dictionary example and how to extract data from a dictionary
dictionary example
  • To change the value of any key, use indexing and assign a new value to it.
costs['SSD'] = 85
  • To assign a new entry to the dictionary, use indexing again. Just use a new key. Duplicate values are allowed, but not duplicate keys.
costs['Power Supply'] = 134.99
  • To delete an item, use the del method along with indexing
del costs['Cooler']

Here are the results in REPL:

Basic dictionary methods
Basic dictionary methods

An in-depth tutorial for dictionary will be posted in the data structure section soon.

Set Types – set, frozenset

If you’re familiar with set in mathematics, Python set is the same. It can be used to perform mathematical set operations like Union, Intersection, Symmetric Difference, etc. Depending on the type of it can store and operations it can handle, sets are divided into 2 data types: set, frozenset.

set – mutable

A set is an unordered collection of unique values. In a list, you can add duplicate elements or values multiple time. But in set, no matter how many times you try to add a duplicate element, set will save only one copy of that element.

Set itself is mutable. That means, the value of a set can be changed over time. But the values of it must be immutable objects like string, integer, float, tuple. It cannot store mutable objects like set, list, dictionary.

A set is unordered. So you can’t access its values by indexing. So a set only supports adding, removing, and some mathematical operations.

Declaring a set:

values = {1, 2, 3, 4, 5}    # set declaration with some values


{1, 2, 3, 4, 5}
<class 'set'>

To add new values to a set, use the add() method (for one value at a time) or the update() method (for multiple values at a time). The update() method can take a list, tuple, string, or another set as its argument.




{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5, 6, 7, 8, 9}

To remove elements from a set, use either the remove() method or the discard() method. Both of them remove one item at a time. But if the item you’re trying to remove does not exist in the set, Python will raise a KeyError, saying that the key or value does not exist. On the other hand, discard() will silently ignore.

Python set data type showing remove and discard operations
set remove, discard operations

Remember that as Python is a dynamically typed language, if there’s an error in the code, Python will still try to execute the entire code. Once the error is encountered, only then Python stops executing. That’s why you can see the values has been printed until line 10 and then encountered an error.

frozenset – Immutable Set

A frozenset has all the characteristics of a set, but it’s immutable. That means, the elements of a frozenset are immutable (characteristic of a set) as well as frozenset itself is immutable. Once it’s declared, then no values can be added or removed from it. Only mathematical operations are allowed.

While tuples are immutable lists, frozensets are immutable sets.

To create a frozenset, you must use the frozenset() condtructor.

A = frozenset([1, 2, 3, 4, 5])


frozenset({1, 2, 3, 4, 5})

Traceback (most recent call last):
  File "path/to/file/", line 3, in <module>
AttributeError: 'frozenset' object has no attribute 'add'

Boolean Type – bool

An object of the Boolean data type can contain either of the 2 values: True or False. Notice the word must start with a capital letter. The boolean data type is regularly used in evaluating conditions and expressions.

boolean data type in python with example
bool data type

In the first two examples, the word True and False are directly behaving as boolean values. In the third example, 2>1 is an expression. As 2 is greater than 1, this expression returns True. The opposite goes for the last example.

Any value other than 0 is considered as True in programming, while 0 is considered as False.

If you use True and False in mathematical operations (addition, subtraction, multiplication, division), then True behaves like 1 and False behaves like 0.

Notice the image carefully and try to understand what you just read.

characteristics of boolean values in programming
Characteristics of bool
Join Facebook Group

Public Group

A group optimized for learning purposes. Track your progress by joining a unit

Follow on Quora

Public Space

Best place to ask questions! Join the built-in community to get help from others

Binary Types – bytes, bytearray, memoryview

Although Python is a human readable program, the machine only understands the binary language. Python is an interpreted language. Although the code seems very short, the underlying steps are not that much short. So a Python script is translated into an intermediate script, then the interpreter translate it into machine readable language. This intermediate script is called byte string. To manipulate this byte strings, there are 3 data types: bytes, bytearray, memoryview. The former two are identical and directly related to the byte string, whereas the later one deals with the memory location of an object.

All of these three data types in Python are not commonly discussed in tutorials as they are a little bit of an advanced level. So if you don’t understand the rest of this section, don’t worry about this.

bytes – Immutable Binary Data Type

bytes is a data type that consists of 8 bit of memory, where each bit contains either 0 or 1. So a bytes data type can contain a value in the range 0<=x<256. The bytes data type is immutable. That means, the value assigned to it cannot be modified. The bytes() method creates n byte of empty data when an integer n is passed to it, otherwise, it converts into a bytes datatype when a value (string or a list of numbers) is passed to it.

Let’s create a 3 bytes of empty data, assign it to a variable and print it.

empty_byte = bytes(3)


<class 'bytes'>

The prefix b tells that the value is in binary data type. \x says the next value is a hexadecimal representation of the binary data. 00 is the value in hexadecimal, which represents empty data. Creating empty bytes is not that useful. let’s work with some real data.

When you pass a string to the byte() method, you also need to specify an encoding (encoding means converting into a coded form). This is needed as the interpreter needs to know how many bytes to declare to store that value. The most common and Python’s default encoding system is UTF-8. It can declare 1 to 4 bytes depending on the character. If the characters are the first 256 characters of Unicode (the English characters and symbols), the UTF-8 takes one byte only. Thus it’s good for memory.

data = bytes('This is a line', encoding='utf-8')


<class 'bytes'>
b'This is a line'

Although they look similar, but they actually are not. The things get crazy when you’re working with non English characters. Let’s see.

from sys import getsizeof   # importing an external function

a = 'This is a line!'
print(len(a))          # Showing the length of 'a'
print(bytes(a, encoding='utf-8'))
print(getsizeof(a))    # size of 'a' in bytes

b = 'আমরি বাংলা ভাষা'   # A line in Bengali
print(len(b))          # Showing the length of 'b'
print(bytes(b, encoding='utf-8'))


b'This is a line!'


Notice that both of the sentences have an equal number of characters (15) but the first one took 64 bytes and the second one took 104 bytes. Why? Because the English letters fit within the first byte of UTF-8 but Bengali letters do not. That’s why a bytes string of English looks the same as a string.

To check all the 256 bytes string that a bytes() method converts into, pass a list of 0 to 255 in a list to the method. Here’s an example. Skip it if you don’t understand.

bytes(i for i in range(256))


\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?

bytearray – Mutable Binary Data Type

bytearray is a data type that consists of 8 bit of memory, where each bit contains either 0 or 1. So a bytearray data type can contain a value in the range 0<=x<256. Everything is same as bytes, the only difference is that bytearray is mutable. That means, the value assigned to it can be modified.

data = bytearray('This is a line', encoding='utf-8')

data[-4:] = bytearray('word', encoding='utf-8')

string, bytes, bytearray are also iterables like list, set, tuple. So you can index their value. In line 5, data[-4:] indicates the last 4 letters. And then replacing them by a new entry word.


bytearray(b'This is a line')
<class 'bytearray'>
bytearray(b'This is a word')
[Finished in 0.14s]

memoryview – Views the memory of some objects

memoryview object allows Python to access and modify the values stored within some objects without even loading that data. Instead of loading the data, it works with the memory location of that data. But memoryview can only do this for certain objects, like bytes, bytearray and a few third party objects.

Notice the block of code below and try to understand.

a = b'MLwiki'    # Shortcut way of declaring bytes datatype

b = memoryview(a)


<class 'bytes'>
<memory at 0x7fa68a323340>
<class 'memoryview'>

What if I want to read the data at that memory location?

print(b.tolist())   # Convert in a list of Unicodes(UTF-8), 10 base



[77, 76, 119, 105, 107, 105]

Notice that you’re converting values of b, but b didn’t load the value MLwiki. It just loaded the memory location. That’s why memoryview becomes handy when manipulating large data. Note that you may want to use bytearray data type to change or add data with memoryview as bytes data type is immutble.

None – Absence of value

None is just a value that is commonly used to describe ’empty’ or ‘no value’. It is a data type that represents the absence of a value, the null value in other programming languages. It is not the same as False, 0, empty string. It is a value that describes the absence of value in a variable or function. When a function does not return any value, the default value is None, which means, there is no value in this function. All the variables assigned to None indicates to the same object and does not create different instance of it. Read about Variables in Python to get this point.

Python None data type
None data type

Final Thoughts

As of now, Python has total 15 built-in data types as described above. Some are very commonly used, but in the long run, every one of these will be helpful. If you’ve read everything from top to bottom, then Congratulations! Thanks for your patience!

Recommended Reading:


Mind Sharing It?
Join Facebook Group

Public Group

A group optimized for learning purposes. Track your progress by joining a unit

Follow on Quora

Public Space

Best place to ask questions! Join the built-in community to get help from others