Data Types in Python

Eliud Nduati
5 min readOct 12, 2021

Python, like other programming languages has different data types that come in handy when writing your code. Regardless of whether your focus is data analysis, data science or general python usage, you will need an understanding of these data types to be effective in your programing.

Let’s look at the data types in python and the different methods that they have.

Python Data types exist in different groups. In this article, we will focus on:

i. Boolean type: Boolean

ii. Mapping types: dictionary

iii. Set types: set

iv. Sequence type: tuple, list

v. Numeric types: float, int

vi. Text types: string

Definitions of common data types in python

A. Integers — these represent whole numbers. examples include 10 people, 3 computers etc.

As this datatype holds numeric values, they support all arithmetic operations. in the below code cell, you can see the different operations that are supported on integers.

# The following are integer variablesa = 32
b = 25
c = 5
# let's confirm by checking their data type
type(a)
# Output
int

Some arithmetic operations on integers:

# It's easy to carry out arithmetic operations on intergers
print(a + b)
print(a - b)
print(b * c)
print(a / c)
print(b / c)
# Output
57
7
125
6.4
5.0

One thing you note is that division yields a quotient with a decimal point (float data type). We can convert this to an integer if we want the value to remain an integer as shown below

print(int(a / c))
print(int(b / c))
#Output
5
6

B. Floats — floats differ from integers as they have a decimal point. They are accurate up to about 15 decimal places. examples include 13.5 onions, 2.9 kgs, 0.5 liters of milk 😁

The only difference between integers and floats is that floats have decimal points. to convert an integer to a float, we use the following method:

x = 1
x = float(x)
print(x)
# Output
1.0

C. Strings — these represent types that are made of Unicode characters. strings are defined or represented using single or double quotes. the characters held within the quotes are items of the string.

# declaring a string variablename = "Jason Borne"
statement = "My name is Borne."
# joining two stringsprint(statement + ' ' + name)# Output
My name is Borne. Jason Borne

Strings are immutable which means we cannot change the items on a string. However, we can convert a string to a list and change the items we want to.

# Split method on stringssentence = 'Hello I am Jason Borne. super spy.'
sentence.split()
# Output
['Hello', 'I', 'am', 'Jason', 'Borne.', 'super', 'spy.']
# String capitalize methodsentence.capitalize()# Output'Hello i am jason borne. super spy.'sentence.upper()# Output'Hello i am jason borne. super spy.'

More string methods can be found on the python documentation here.

D. Lists — these are ordered sequence of items. the values in the list do not have to be of the same data type. list are mutable which means the values can eb changed.

lists store collections of homogeneous items- the degree of the items varies. lists are iterable.

list are indexed starting with index 0. To pull an element from the list we therefore use the list name and the index as shown below

items = ['car', 'apple', 5.5, 'hyena', 13]
items[2]
# Output
5.5

lists can be sliced, one thing to remember is that the lower index is inclusive and the upper index is exclusive.

items[:3]# Output
['car', 'apple', 5.5]
items[2:4]#Output
[5.5, 'hyena']

More on lists methods can be found here

E. Tuples — tuples are a sequence of items in order. Unlike lists however, tuples are immutable. The core use of tuples is to write-protect data.

example:

Nairobi = (1.2921, 36.8219)

The above tuple shows the cordinates of Nairobi city in Kenya. Since the infomation is not likely to change, it is wise to store them in a tuple to prevent accidental editing. The infomation on the Tuple can be accessed by their indeces ie.

Nairobi[0], and Nairobi[1]

One of the uses of tuples is to assign closely related infomation to variables. See below:

dimensions = 52, 40, 100
length, width, height = dimensions
print("The dimensions are {}*{}*{}".format(length, width, height))# Output
The dimensions are 52*40*100

In creating Tuples, there are two main challenges, one is creating an empty tuple and the other is creating a tuple with only one item.

# Empty tuple.
# we use a pair parenthesese
house_no = ()# to construct a tuple with only one item
# we use a comma at the end of the first element
name = 'Samson',

More on tuples can be found here.

F. Sets — These are collections of unique items that are not in order. the items in a set are unordered. Sets make it easier to carry out operations such as intersections and unions on two sets.

Suppose you have collected infomation in a survey. If you consolidate the name of the capital cities in a list, you should end up with 195 Capital Cities. However in some cases, you list might have as many capital city entries as the number of respondents in your survey. To get the unique capital city values, you use sets. Sets have no duplicate elements. Therefore sets are important when you are eliminating duplicate entries or testing for membership.

# creating a set
basket = {'apples', 'mangoes', 'apples', 'oranges', 'remote', 'remote', 'hotdog'}
basket#Output
{'apples', 'hotdog', 'mangoes', 'oranges', 'remote'}
# The resulting output has no duplicate entries

Something to note, when creating empty sets use () brackets and not {} braces. The latter will initialize a dictionary when used to create an empty set. More on sets can be found here.

G. Dictionaries — dictionaries hold collections of unordered, pairs of items. the pairs contain keys and values. A pair in a dictionary is a datatype represented as key: value. The key and value can eb of any type.

Examples:

fruits = {
'Apples': 12,
'Oranges': 13,
'mangoes': 1,
'pineapples': 3
}
fruits#Output
{'Apples': 12, 'Oranges': 13, 'mangoes': 1, 'pineapples': 3}
# Accessing items using key
fruits['Apples']
# Output
12
# adding items to the dictionary
fruits['bananas'] = 234
fruits
#Output
{'Apples': 12, 'Oranges': 13, 'mangoes': 1, 'pineapples': 3, 'bananas': 534}

YES! The guy from elementary maths who buys 534 bananas 😆

image from here

Dictionaries and compound dictionary structures are related. However retriving values in compund data strucrures such as compoud dictionaries, sublists requres additional index or key. Check the disctionary below:

grocery = {
'fruits': {'Apples': 12, 'Oranges': 13, 'mangoes': 1, 'pineapples': 3},
'milk': 10,
'Greens': ['kale', 'spinach', 'bell-pepper', 'pepper']
}
grocery
#Output{'fruits': {'Apples': 12, 'Oranges': 13, 'mangoes': 1, 'pineapples': 3},
'milk': 10,
'Greens': ['kale', 'spinach', 'bell-pepper', 'pepper']}
# Retiving value number of apples bought
grocery['fruits']['Apples']

More on dictionaries can be found here and here.

Conclusion.

Understanding the different data types in python and the methods associated with each is important to learning python programming and python for data analytics. Find the practice notebook here.

--

--