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.

Numbers

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.

type(1)
↳ int

As expected, 1 is an int.

type(1.)
↳ float

The decimal at the end make 1. a float.

1+1
↳ 2

Adding an int to an int yields an int.

1+1.
↳ 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()

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

To round the float number use

round(2.6)
↳ 3.0

Numerical Operations

The / operator performs division.

1/2
↳ 0
1/2.
↳ 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.

1.//2
↳ 0.0
-1.//2
↳ -1.0

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

  • is 121.
11**2
↳ 121
11**2.
↳ 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.

11%2
↳ 1

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

Fractions

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

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

You can perform all the usual mathematical operations with fractions as

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

Trigonometry

You can also do basic trigonometry in Python.

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

Strings

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 ( " " ).

s='UnicodeString'
s="UnicodeString"

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

len(s)
↳ 13

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

s[1]
↳ 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.

Bytes

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.

len(by)
↳ 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

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_list
↳ ['a', 'b', 'mpilgrim', 'z', 'example', 2]

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

a_list[0]
↳ 'a'

List data can be accessed using index.

type(a_list[0])
↳ str
type(a_list[-1])
↳ 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:

len(a_list)
↳ 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:

b_list=a_list[0:3]
b_list
↳ ['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.
b_list=a_list+['Hydro','Aqua']
b_list
↳ ['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.

len(b_list)
↳ 9
b_list.append(['d','e'])
b_list
↳ ['a','b','mpilgrim','z','example',2,'Hydro','Aqua',True,['d','e'] ]
len(b_list)
↳ 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
b_list.extend(['d','e'])
b_list
↳ ['a','b','mpilgrim','z','example', 2,'Hydro','Aqua',True,'d','e']
len(b_list)
↳ 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]
b_list.insert(0,'d')

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

b_list
↳ ['d','a','b','mpilgrim','z','example',2,'Hydro','Aqua',True]
b_list.insert(0,['x','y'])
b_list
↳ [['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

b_list.count('b')
↳ 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.

b_list.index('a')
↳ 0
b_list.index('b')
↳ 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.

b_list[2]
↳ 'mpilgrim'
del(b_list[2])
b_list
↳ ['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:

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.

b_list[2]
↳ 'mpilgrim'
b_list.pop(2)
↳ '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_list.pop()
↳ 'b'

Now the b_list is as follows

b_list
↳ ['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:

b_list.pop()
↳ 'Hydro'
b_list
↳ ['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.

b_list.remove('b')
b_list
↳ ['a','z','example', 2,'Hydro','Aqua']

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

Tuples

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')
a_tuple.index('z')
↳ 3
  • item z is at the index 3, i.e., it is the fourth element of the tuple.
b_tuple=a_tuple[0:4]
b_tuple
↳ ('a','b','mpilgrim','z')

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

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

Sets

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'}
type(a_set)
↳ set
a_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,
set(a_list)
↳ {'example', 2, 'z', 'a', 'mpilgrim', 'b'}
set(a_tuple)
↳ {'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,

a_set={2,'Aqua','Hydro','a','b','example','mpilgrim','z'}

Using add: To add single item to a set.

a_set.add('c')
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.

a_set.update('a','Sujan','Koirala')
a_set
↳ {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,

a_set={2,'Aqua','Hydro','a','b','example','mpilgrim','z'}

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

a_set.remove('b')
a_set
↳ {2,'Aqua','Hydro','Koirala','Sujan','a','c','example','mpilgrim','z'}

b has been removed.

a_set.discard('Hydro')
a_set
↳ {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.

a_set.pop()
a_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,

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

Union: Can be used to combine two sets.

c_set=a_set.union(b_set)
c_set
↳ {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.

d_set=a_set.intersection(b_set)
d_set
↳ {9,2,12,5,21}

Dictionaries

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,

a_dict={'Hydro':'131.112.42.40','Aqua':'131.112.42.41'}
a_dict
↳ {'Aqua':'192.168.1.154','Hydro':'131.112.42.40'}

The order is changed automatically like set.

a_dict['Hydro']
↳ '131.112.42.40'

Key Hydro can be used to access the value 131.112.42.40.

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.

a_dict={'Aqua':'192.168.1.154','Hydro':'131.112.42.40'}

If you want to change the value of Aqua,

a_dict['Aqua']='192.168.1.154'
a_dict
↳ {'Aqua':'192.168.1.154','Hydro':'131.112.42.40'}

If you want to add new item to the dictionary,

a_dict['Lab']='Kanae'
a_dict
↳ {'Aqua':'192.168.1.154','Hydro':'131.112.42.40','Lab':'Kanae'}

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

k_lab={'Female':['Yoshikawa','Imada','Yamada','Sasaki','Watanabe','Sato'],'Male':['Sujan','Iseri','Hagiwara','Shiraha','Ishida','Kusuhara','Hirochi','Endo']}
k_lab['Female']
↳ ['Yoshikawa','Imada','Yamada','Sasaki','Watanabe','Sato']

Arrays

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.
b_list=['a','b',1,2]

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

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

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

b_list2=[1,2,3,4]

All items are numbers.

b_array2=array(b_list2)
b_array2
↳ 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.
yy=arange(2,5,1)
yy
↳ array([2,3,4])

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

yy=arange(5)
yy
↳ array([0,1,2,3,4])

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

yy=arange(5,2,-1)
yy
↳ 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.
zz=zeros(20)

will create an array with 20 zeros.

zz=zeros(20,20)

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

zz=zeros(20,20,20)

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)
b
↳ array([0,1,2,3])
c = a-b
c
↳ array([20, 29, 38, 47])

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

b**2
↳ array([0, 1, 4, 9])

Square of each element in b.

10*sin(a)
↳ array([ 9.12945251, -9.88031624, 7.4511316, -2.62374854])

Two operations can be carried out at once.

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

True if a<35 otherwise False.