Python Data Types

Data types in Python, is basically an attribute of data which tells the compiler or interpreter, how to interpret its value. For example, if we have a numeric types of data, means that, it can be used to perform arithmetic operations.

Python 7 Built-in Data Types

Data type represents the type of value and determines how the value can be used in a Python program. Here are the list of 7 built-in data types:

  1. Text Type
  2. Numeric Types
  3. Sequence Types
  4. Mapping Type
  5. Set Types
  6. Boolean Type
  7. Binary Types

Let's describe briefly about these 7 built-in data types, one by one along with example programs. But before starting, let's first understand how can we get the type of data, a variable holds.

How to Get the Type of Data, an Object (Variable) Holds ?

To find and print the type of data, a variable holds in Python, we need to use type method. Here is an example program, demonstrates it:

myvar = 10

The output produced by above Python program, is shown in the snapshot given below:

python data types

As you can see from the above output, because the variable myvar holds a value 10 that is an integer value, therefore the output produced, saying the variable myvar is of the class int.

Python Text Data Type

The str class comes under the category of text data type in Python. This data type holds all the string values. Here is an example:

myvar = 'c'
myvar = "c"
myvar = ""
myvar = "Hey, What's Up!"

Here is its sample output:

python text data types

The above program can also be created as:

a, b, c, d = 'c', "c", "", "Hey, What's Up!"

You'll get the same output as of previous program's output.

Python Numeric Data Types

Under the category of numeric data types, we have:

Here is an example program, uses numeric data types. I've used three variables with three different numeric values such as int, float, and complex:

a, b, c = 123, 3.49, 95.4j

The output produced by above program will be:

<class 'int'>
<class 'float'>
<class 'complex'>

Python Sequence Data Types

This is another and important data types we must have to aware about. This data type deals with values in sequence. A sequence allows us to store multiple values of same/different types in a same variable. These are the list of sequence types:

Note - To learn about these three topics in detail, refer to its separate tutorial.

Let's take an example, uses all these three types of sequence in Python:

var = [1, "codescracker", True, 23.4, 234]
var = (1, "codescracker", True, 23.4, 234)
var = range(10)

The output produced by above program will be:

<class 'list'>
<class 'tuple'>
<class 'range'>

Python Mapping Data Type

The dictionary comes under the category of mapping data type in Python. In dictionary, each item are in the pair of key and value. Since, every item (element) are in the form of key and value pair, therefore it comes under the category of mapping data type.

Like list, dictionary is also mutable. Here is an example shows the mapping type in Python:

var = {'day': 'Thursday', 'month': 'September', 'year': '2021'}
var = {1: 'day', 2: 'month', 3: 'year'}

Now the output produced by above program will be:

<class 'dict'>
<class 'dict'>

Python Set Data Types

Under this data type category, we have basically two types. Here are the list of these two types:

Here is an example, uses both type.

var = {"Liam", "Noah", "Oliver"}
var = ["Elijah", "William", "Benjamin", "Lucas"]
var = frozenset(var)

Now, if you execute this program, then the output produced will exactly looks like:

<class 'set'>
<class 'frozenset'>

Python Boolean Data Type

The boolean data type is used when we need to evaluated one of the two possible values say True and False. Here is an example, demonstrates the boolean data type in Python:

var = True

var = False


The output produced by above Python program, demonstrating boolean data type will be:

<class 'bool'>
<class 'bool'>
<class 'bool'>
<class 'bool'>

Python Binary Data Types

Most of the time, binary data types in Python are used to improve the performance. Here are the list of three binary types:

Here is an example, demonstrates these three binary types.

var = b"codescracker"

var = bytes("codescracker", "utf-8")

var = bytearray(10)

var = memoryview(bytes(10))

The snapshot given below shows the sample output produced by above Python program:

python data types example

From above program, the following statement:

var = bytes("codescracker", "utf-8")

encodes the string codescracker using utf-8 encoding method.

Mega Program on Python Data Type

This is the last example program, covering all the data types in Python. First I've initialized all type of values to some variables, then printed the values and types. Let's take a look:

a = 'c'
b = "c"
c = "codescracker"

d = 12
e = 43.93
f = 43j

g = ["Henry", "Alexander"]
h = ("Mason", "Michael", "Ethan")
i = range(5)

j = {"Name": "Daniel", "Age": 38}

k = {"Owen", "Aiden"}
l = frozenset(g)

m = True
n = False

o = bytes(c, "utf-8")
p = bytearray(b, "utf-8")
q = bytearray(c, "utf-8")
r = memoryview(bytes(d))
s = memoryview(bytes(c, "utf-8"))
t = memoryview(bytearray(d))
u = memoryview(bytearray(c, "utf-8"))

print("Variable\t Value\t\t\t\t\t\t\t\t Type")
print("a\t\t\t", a, "\t\t\t\t\t\t\t\t\t", type(a))
print("b\t\t\t", b, "\t\t\t\t\t\t\t\t\t", type(b))
print("c\t\t\t", c, "\t\t\t\t\t\t", type(c))
print("d\t\t\t", d, "\t\t\t\t\t\t\t\t", type(d))
print("e\t\t\t", e, "\t\t\t\t\t\t\t\t", type(e))
print("f\t\t\t", f, "\t\t\t\t\t\t\t\t", type(f))
print("g\t\t\t", g, "\t\t\t", type(g))
print("h\t\t\t", h, "\t\t", type(h))
print("i\t\t\t", i, "\t\t\t\t\t\t", type(i))
print("j\t\t\t", j, "\t\t", type(j))
print("k\t\t\t", k, "\t\t\t\t\t", type(k))
print("l\t\t\t", l, "\t", type(l))
print("m\t\t\t", m, "\t\t\t\t\t\t\t\t", type(m))
print("n\t\t\t", n, "\t\t\t\t\t\t\t\t", type(n))
print("o\t\t\t", o, "\t\t\t\t\t", type(o))
print("p\t\t\t", p, "\t\t\t\t\t", type(p))
print("q\t\t\t", q, "\t\t", type(q))
print("r\t\t\t", r, "\t", type(r))
print("s\t\t\t", s, "\t", type(s))
print("t\t\t\t", t, "\t", type(t))
print("u\t\t\t", u, "\t", type(u))

Seriously!, I mean so many horizontal tabs (\t). The snapshot given below shows the sample output produced by this mega program on data types in Python:

python data types mega program

More Examples

Python Online Test

« Previous Tutorial Next Tutorial »

© Copyright 2021. All Rights Reserved.