Python Data Types

In python, there are various types of data. Every data has a type and a value. Every value has a fixed data type but it should not specified beforehand. The most basic data types in python are:

  1. Boolean: These are data which have only two values: True or False.

  2. Numbers: These are numeric data.

  3. Strings: These data are sequences of unicode characters.

  4. Bytes: An immutable sequence of numbers.

Furthermore, these data types can be combined and following types of datasets can be produced:

  1. Lists: Ordered sequences of values. Can handle different data types within one list.

  2. Tuples: Ordered but immutable, i.e. cannot be modified, sequences of values.

  3. Sets: Unordered bags of values, i.e., the values may be stored in any order irrespective of how you create the set.

  4. Dictionaries: Unordered bag of key-value pairs.

  5. Arrays: Ordered sequences of data of same type.

Basic Data Types

In this section, a brief description of basic data types, their possible values, and various operations that can be applied to them are described.

Boolean Data

These data are either True or False. If an expression can produce either yes or no answers, booleans can be used to interpret the result. This kind of yes/no situations are known as boolean context. Here is a simple example.

Assign some variable (size) as 1.

size = 1

Did you notice that the variable definition is not needed?

Check if size is less than 0.

size < 0
↳ False

It is false as 1 > 0.

Check if size is greater than 0.

size > 0
↳ True

It is true as 1 > 0.

  • True or False can also be treated as numbers: True=1 and False=0.

  • The logical data types are often used as arguments to functions when some features, such as figure labels, need to be easily turned off or on.


Python supports both integers and floating point numbers. There's no type declaration to distinguish them and Python automatically distinguishes them apart by the presence or absence of a decimal point.

You can use type() function to check the type of any value or variable.

↳ int

As expected, 1 is an int.

↳ float

The decimal at the end make 1. a float.

↳ 2

Adding an int to an int yields an int.

↳ 2.0
  • Adding an int to a float yields a float. Python coerces the int into a float to perform the addition, then returns a float as the result.

  • Integer can be converted to float using float() and float can be converted to integer using int()

↳ 2.0
↳ 2
  • Python truncates the float to integer, 2.6 becomes 2 instead of 3.

To round the float number use

↳ 3.0

Numerical Operations

The / operator performs division.

↳ 0
↳ 0.5

Be careful on float or integer data type as the result can be different as shown above. These bugs are notoriously hard to trace. Unless an integer data type is needed, always use a decimal point. For Python 3, the / operator always performs a float division regardless of data types.

The // operator performs a division combined with truncating and rounding. When the result is positive, it truncates the result but when the result is negative, it rounds off the result to nearest integer but the result is always a float.

↳ 0.0
↳ -1.0

The ‘**' operator means "raised to the power of".

  • is 121.
↳ 121
↳ 121.0

Be careful on float or integer data type as the result can be different as shown above.

The ‘%' (modulo) operator gives the remainder after performing integer division.

↳ 1

11 divided by 2 is 5 with a remainder of 1, so the result here is 1.


To start using fractions, import the fractions module. To define a fraction, create a Fraction object as

import fractions
↳ Fraction(1, 2)

You can perform all the usual mathematical operations with fractions as

fractions.Fraction(1, 2)*2
↳ Fraction(1, 1)


You can also do basic trigonometry in Python.

import math
↳ 3.1415926535897931
math.sin(math.pi / 2)
↳ 1.0


In Python, all strings are sequences of Unicode characters. It is an immutable sequence and cannot be modified.

To create a string, enclose it in quotes. Python strings can be defined with either single quotes ( ' ' ) or double quotes ( " " ).


The built-in len() function returns the length of the string, i.e. the number of characters.

↳ 13

You can get individual characters out of a string using index notation.

↳ n

You can concatenate strings using the + operator.

s+' ' +'longer'
↳ 'UnicodeString longer'
  • note than ' ' in the middle is a string as well because it consists of a whitespace.


An immutable sequence of numbers between 0 and 255 is called a bytes object. Each byte within the bytes object can be an ascii character or an encoded hexadecimal number from \x00 to \xff (0–255).

To define a bytes object, use the b'' syntax. This is commonly known as "byte literal" syntax.

by = b'abcd65' by
↳ 'abcde'
  • x65 is e.

  • Just like strings, you can use len() function and use the + operator to concatenate bytes objects. But you cannot join strings and bytes.

↳ 5
by += b'66' by
↳ 'abcdef'

Combined Data Types

The basic data types explained in the previous section can be arranged in sequences to create combined data types. These combined data types can be modified, for e.g., lists or are immutable which cannot be modified, for e.g., tuples. This section provides brief description of these data and the common operations that can be used.


Lists are the sequence of data stored in an arranged form. It can hold different types of data (strings, numbers etc.) and it can be modified to add new data or remove old data.

Creating a List

To create a list: use square brackets "[ ]" to wrap a comma-separated list of values of any data types.

a_list =['a','b','mpilgrim','z','example', 2]
↳ ['a', 'b', 'mpilgrim', 'z', 'example', 2]

All data except last data are strings. Last one is integer.

↳ 'a'

List data can be accessed using index.

↳ str
↳ int

Type of data can be checked using type().

Slicing a List

Once a list has been created, a part of it can be taken as a new list. This is called slicing the list. A slice can be extracted using indices. Let's consider same list as above:

a_list =['a','b','mpilgrim','z','example', 2]

The length of the list can be obtained as:

↳ 6

the index can be from 0 to 5 if we count from left to right or -1 to -6 if we count from right to left.

We can obtain any other list as:

↳ ['a', 'b', 'mpilgrim']

Adding Item to a List

There are 4 different ways to add item/items to a list. Let's consider same list as above:

a_list =['a','b','mpilgrim','z','example',2]
  1. ‘+' operator: The + operator concatenates lists to create a new list. A list can contain any number of items; there is no size limit.
↳ ['a','b','mpilgrim','z','example', 2,'Hydro','Aqua']
  1. append(): The append() method adds a single item to the end of the list. Even if the added item is a list, the whole list is added as a single item in the old list.
b_list.append(True) b_list
↳ ['a','b','mpilgrim','z','example', 2,'Hydro','Aqua',True]

This list has strings, integer, and boolean data.

↳ 9
↳ ['a','b','mpilgrim','z','example',2,'Hydro','Aqua',True,['d','e'] ]
↳ 10

The length of b_list has increased by only one even though two items, [d, e], were added.

  1. extend(): Similar to append but each item is added separately. For e.g., let's consider the list
b_list=['a','b','mpilgrim','z','example',2,'Hydro','Aqua',True] len(b_list)
↳ 9
↳ ['a','b','mpilgrim','z','example', 2,'Hydro','Aqua',True,'d','e']
↳ 11

The length of b_list has increased by two as two items in the list, [d, e], were added.

  1. insert(): The insert() method inserts a single item into a list. The first argument is the index of the first item in the list that will get bumped out of position.
b_list=['a','b','mpilgrim','z','example', 2,'Hydro','Aqua',True]

Insert d  in the first position,i.e., index 0.

↳ ['d','a','b','mpilgrim','z','example',2,'Hydro','Aqua',True]
↳ [['x','y'],'d','a','b','mpilgrim','z','example',2,'Hydro','Aqua', True]

The list [x, y] is added as one item as in the case of append().

Search for Item in a List

Consider the following list:

b_list=['a','b','mpilgrim','z','example', 2,'Hydro','Aqua','b']

count() can be used as in the case of string

↳ 2

in can be used to check if certain value exists in a list.

'b'in b_list
↳ True
'c'in b_list
↳ False

The output is boolean data, i.e., True or False.

index can be used to find the index of search data.

↳ 0
↳ 1

Even though there are 2 b, the index of first b is returned.

Removing Item from a List

There are many ways to remove an item from a list. The list automatically adjusts its size after some element has been removed.

Removing Item by Index

The del command removes an item from a list if the index of an element that needs to be removed is provided.

Consider the following list:

b_list=['a','b','mpilgrim','z','example', 2,'Hydro','Aqua','b']

Suppose we want to remove the element mpilgrim from the list. Its index is 2.

↳ 'mpilgrim'
↳ ['a','b','z','example', 2,'Hydro','Aqua','b']

mpilgrim is now removed.

The pop() command can also remove an item by specifying an index. But, it is even more versatile as it can be used without any argument to remove the last item of a list.

Consider the following list:


Suppose we want to remove the element mpilgrim from the list. Its index is 2.

↳ 'mpilgrim'
↳ 'mpilgrim'

The item to be removed will be displayed.

Now the b_list is as follows

↳ ['a','b','z','example', 2,'Hydro','Aqua','b']

If pop() is used without an argument.

↳ 'b'

Now the b_list is as follows

↳ ['a','b','z','example',2,'Hydro','Aqua']

The last b is removed from the list.

If pop() is used once again. The list will be as follows:

↳ 'Hydro'
↳ ['a','b','z','example', 2,'Hydro']

Removing Item by Value

The remove command removes item/items from a list if the value of the item is specified.

Consider the following list:

b_list=['a','b','mpilgrim','z','example', 2,'Hydro','Aqua','b']

Suppose we want to remove the elements b from the list.

↳ ['a','z','example', 2,'Hydro','Aqua']

Note that only the first occurrence of the element will be removed at a time.


A tuple is an immutable list. A tuple can not be changed/modified in any way once it is created.

A tuple is defined in the same way as a list, except that the whole set of elements is enclosed in parentheses instead of square brackets.

The elements of a tuple have a defined order, just like a list. Tuples indices are zero based, just like a list, so the first element of a non empty tuple is always t[0].

Negative indices count from the end of the tuple, just as with a list.

Slicing works too, just like a list. Note that when you slice a list, you get a new list; when you slice a tuple, you get a new tuple.

A tuple is used because reading/writing a tuple is faster than the same for lists. If you do not need to modify a set of item, a tuple can be used instead of list.

Creating Tuples

A tuple can be created just like the list but parentheses "( )" has to be used instead of square brackets"[ ]". For e.g.,

a_tuple=('a','b','mpilgrim','z','example', 2,'Hydro','Aqua','b')

Tuple Operations

All the list operations except the ones that modify the list itself can be used for tuples too. For e.g., you cannot use append(), extend(), insert(), del, remove(), and pop() for tuples. For other operations, please follow the same steps as explained in the previous section. Here are some examples of tuple operations.

Consider the following tuple:

a_tuple=('a','b','mpilgrim','z','example', 2,'Hydro','Aqua','b')
↳ 3
  • item z is at the index 3, i.e., it is the fourth element of the tuple.
↳ ('a','b','mpilgrim','z')

New tuple can be created by slicing a tuple as original tuple does not change.

↳ ('a','b','mpilgrim','z','example', 2,'Hydro','Aqua','b')


A set is an unordered collection of unique values. A single set can contain values of any datatype.

Creating Set

There are basically two ways of creating set.

  1. From scratch: Sets can be created like lists but curly brackets "{ }" have to be used instead of square brackets "[ ]". For e.g.,
a_set={'a','b','mpilgrim','z','example', 2,'Hydro','Aqua','b'}
↳ set
↳ {2, 'Aqua', 'Hydro', 'a', 'b', 'example', 'mpilgrim', 'z'}

The set has different orders than the values given inside because it is unordered and original orders are ignored. Also, there is only one b in the set even though two b were given because a set is a collection of unique values. Duplicate values are taken as one.

  1. From list or tuple: A set can be created from a list or tuple as,
↳ {'example', 2, 'z', 'a', 'mpilgrim', 'b'}
↳ {'a','b','mpilgrim','z','example', 2,'Hydro','Aqua','b'}

Modifying Set

A set can be modified by adding an item or another set to it. Also, items of set can be removed.

Adding Elements

Consider a set as follows,


Using add: To add single item to a set.

↳ {'example', 2, 'c', 'z', 'Hydro', 'a', 'mpilgrim', 'b', 'Aqua'}

c is added after b.

Using update: To add multiple items as a set or list or tuple.

↳ {2,'Aqua','Hydro','Koirala','Sujan','a','b','c','example','mpilgrim','z'}

Koirala and Sujan are added but a is not added.

Removing Elements

Consider a set as follows,


Using remove() and discard(): These are used to remove an item from a set.

↳ {2,'Aqua','Hydro','Koirala','Sujan','a','c','example','mpilgrim','z'}

b has been removed.

↳ {2,'Aqua','Koirala','Sujan','a','c','example','mpilgrim','z'}

Using pop() and clear(): pop() is same as list but it does not remove the last item as list. pop() removes one item ramdomly. clear() is used to clear the whole set and create an empty set.

↳ {2,'Koirala','Sujan','a','c','example','mpilgrim','z'}

Set Operations

Two sets can be combined or common elements in two sets can be combined to form a new set. These functions are useful to combine two or more lists.

Consider following two sets,


Union: Can be used to combine two sets.

↳ {1,2,195,4,5,6,8,12,76,15,17,18,3,21,30,51,9,127}

Intersection: Can be used to create a set with elements common to two sets.

↳ {9,2,12,5,21}


A dictionary is an unordered set of key-value pairs. A value can be retrieved for a known key but the other-way is not possible.

Creating Dictionary

Creating a dictionary is similar to set in using curled brackets "" but key:value pairs are used instead of values. The following is an example,

↳ {'Aqua':'','Hydro':''}

The order is changed automatically like set.

↳ ''

Key Hydro can be used to access the value

Modifying Dictionary

Since the size of the dictionary is not fixed, new key:value pair can be freely added to the dictionary. Also values for a key can be modified.

Consider the following dictionary.


If you want to change the value of Aqua,

↳ {'Aqua':'','Hydro':''}

If you want to add new item to the dictionary,

↳ {'Aqua':'','Hydro':'','Lab':'Kanae'}

Dictionary values can also be lists instead of single values. For e.g.,

↳ ['Yoshikawa','Imada','Yamada','Sasaki','Watanabe','Sato']


Arrays are similar to lists but it contains homogeneous data, i.e., data of same type only. Arrays are commonly used to store numbers and hence used in mathematical calculations.

Creating Arrays

Python arrays can be created in many ways. It can also be read from some data file in text or binary format, which are explained in latter chapters of this guide. Here, some commonly used methods are explained. For a detailed tutorial on python arrays, refer here.

  1. From list: Arrays can be created from a list or a tuple using: somearray=array(somelist). Consider the following examples.

The list has mixed datatypes. First two items are strings and last two are numbers.

↳ array(['a','b','1','2'], dtype='|S8')

Since first two elements are string, numbers are also converted to strings when array is created.


All items are numbers.

↳ array([1,2, 3, 4])

Numeric array is created. Mathematical operations like addition, subtraction, division, etc. can be carried in this array.

  1. Using built-in functions:

  2. From direct values:

xx = array([2, 4, -11])

xx is an array of length 3 or shape (1,3) means 1 row and 3 columns.

  1. From arange(number): Creates an array from the range of values. Examples are provided below. For details of arange follow chapter 4.
↳ array([2,3,4])

Creates an array from lower value (2) to upper value (5) in specified interval (1) excluding the last value (5).

↳ array([0,1,2,3,4])

If the lower value and interval are not specified, they are taken as 0 and 1, respectively.

↳ array([5,4,3])

The interval can be negative.

  1. Arrays of fixed shape: Sometimes it is necessary to create some array to store the result of calculation. Fuctions zeros(someshape) and ones(someshape) can be used to create arrays with all values as zero or one, respectively.

will create an array with 20 zeros.


will create an array with 20 rows and 20 columns (total 20*20=400 elements) with all elements as zero.


will create an array with 20 blocks with each block having 20 rows and 20 columns (total 202020=8000 elements) with all elements as zero.

Array Operations

Arithmetic operators on arrays apply elementwise. A new array is created and filled with the result.

a = array([20,30,40,50])
b =arange(4)
↳ array([0,1,2,3])
c = a-b
↳ array([20, 29, 38, 47])

Each element in b is subtracted from respective element in a.

↳ array([0, 1, 4, 9])

Square of each element in b.

↳ array([ 9.12945251, -9.88031624, 7.4511316, -2.62374854])

Two operations can be carried out at once.

↳ array([True, True, False, False], dtype=bool)

True if a<35 otherwise False.