You are on page 1of 28

ISOM 3400 – PYTHON FOR BUSINESS ANALYTICS

4. Python Data Structure

GEN LI

JUN 25, 2019


Recap of Lab3

2
Recap of Lab3

3
Recap of Lab3

4
Recap of Lab3

5
Recap of Lab3

6
Recap of Last Class
Print method, escape characters
Interpret the sign after “\”
Use of sep and end
String operators
concatenation
[:] – indexing
[a:b] = [a,b-1]; what about [a:] or [:b]
String formatting - %, adding () for multiple
variables
Other string methods
String are immutable 7
Recap of Last Class

8
Class Objectives
Lists
Indexing of list
Slicing
Built-in list functions
Copying a list
List comprehension
Tuple
Definition
functions

9
Data Structure
In many applications, data is related in some way, and should be
organized in some structure that mirrors the semantics of data:
In programming, we use data structures to tie related data together.
A shopping cart of items
A gradebook for a class
A person's demographic characteristics
Districts of Hong Kong
In simple terms, data structure refers to the the collection or group of
data in a particular structure. In Python, the four common data
structures are:
List
Tuple
Set
Dictionary

10
List
Lists are the most commonly used data
structure. Think of it as a sequence of data
that is enclosed in square brackets [a, b,
c] and data are separated by comma. Each of
these data can be accessed by calling it's
index value.

May contain items of different types, but


usually items all have the same type
squares = [1, 4, 9, 16, 25]
names = ['Jenny', 'Michael', 'James', 'Sophia'] 11
Indexing of List
Similar to string, list has index and can be sliced.
Indexing starts from 0 as already seen for strings. Thus
now the list x, which has two elements will have apple at
0 index and orange at 1 index

Indexing can also be done in reverse order. That is the


last element can be accessed first. Here, indexing starts
from -1. Thus index value -1 will be 'orange' and index -2
will be 'apple'.
12
More than one list
Here we have declared two lists x and y each
containing its own data. Now, these two lists
can again be put into another list say z which
will have it's data as two lists. This list inside
a list is called as nested lists and is how an
array would be declared which we will see
later.
y = ['carrot','potato']
z = [x,y]
print(z)

13
Indexing in nested list
Indexing in nested lists can be quite confusing if
you do not understand how indexing works in
python. So let us break it down and then arrive
at a conclusion.

Let us access the data 'orange' in the above


nested list. First, at index 0 there is a list
['apple','orange'] and at index 1 there is another
list ['carrot','potato']. Hence z[0] should give us
the first list which contains 'apple' and 'orange'.
From this list we can take the second element
(index 1) to get 'orange'. 14
Slicing
Indexing was only limited to accessing a single element, slicing on
the other hand may access a sequence of data inside the list, in
other words "slicing" the list.

Slicing is done by defining the index values of the first element and
the last element from the parent list that is required in the sliced list.
It is written as [a:b]where a and b are the index values from the
parent list. If a or b is not defined then the index value is considered
to be the first value for a if a is not defined, and the last value for b if
b is not defined.

Assuming a and b are both defined, then the most important thing to
remember is that the slicing output will include the value for index a,
but not the value for index b. In other words, the last value in the
output will be at index position b-1 in the list.

15
Step length
You can also slice a parent list with a step
length.
(initial_value: index_position:
how_many_steps/intervals)

16
Built-in function
Many of the functions below will alter the
content of a list, because list is
mutable (unlike string, which is not mutable)!
For each function, please pay attention to
How it is used: list.function() or function(list)
What does it return? A value, a list, or None
Does it change the original value of the list?

17
Built-in function
len(list) Returns the number of elements in a list.
max(list) Returns the biggest element in a list based on
lexicographic order. Can be used on numeric or string
elements.
min(list) Returns the smallest element in a list based on
lexicographic order. Can be used on numeric or string
elements.
sum(list) Returns the sum of all elements in a list. Can
only be used on numeric elements.

18
Built-in function
In a list with string elements, max( ) and min(
) are still applicable and return the first/last
element in lexicographical order. The
lexicographical order is based on a character's
ASCII value: 0~9 (48~57); A~Z (65~90); a~z
(97~122).(Note that 0~9 here are not integers,
but characters!)

19
Built-in function
+/*/in/not in
Use + to concatenate two lists
use * to replicate a list
Use in or not in to check if a particular element is
inside a list, returns True/False

Lists can be concatenated by adding '+'


between them. The resultant list will contain
all the elements of the lists that were added.
The resultant list will NOT be a nested list.
20
Built-in function
list(str) vs. str.split()
list(str) converts a string into a list, by turning each character in a
string into an element in a list. Returns the list.
str.split() method breaks a string up into a list based on spaces
in the string. Also returns the list.

21
Built-in function
list.append() vs. list.extend() vs. list.insert(x,y) functions
append( ) adds a single element at the end of the list. The element could be a
single value or a list, or a tuple. Note that appending a list to a list would create a
sublist. Changes are made to the original list.
extend( ) is similar to append(), but only takes iterable element as input, and
adds each element separately at the end of the list. Note that extending a list to a
list would NOT create a sublist. Changes are made to the original list.
insert(x,y) can insert a element y at a specified index value x. Y can be a single
value, or a list, or a tuple, and will be added as is to the indexed position.
Changes are made to the original list.

list.count()/list.index()
count( ) is used to count the number of a particular element that is present in the
list. Returns an integer.
index( ) is used to find the index value of a particular element. Note that if there
are multiple elements of the same value then the first index value of that element
is returned. Returns an integer.

22
Built-in function
list.pop( ) vs. list.remove( ) functions.
pop( ) will remove the last element in the list, and return the
popped item. Changes made to the original list.
remove( ) can remove an element by specifying the element
itself. Changes made to the original list.

Changing the order of elements in a list:


list.reverse() Reverse items in a list, return None, and store the reversed
elements back to the original list.
list.sort() Sort items in a list in ascending order by default, return None, and store
sorted elements back to the origninal list.
sorted(list) Print a sorted copy of the list, return the sorted copy, doesn't change
original list.
The entire elements present in the list can be reversed by using
the reverse() method.

23
Copying a list
Assignment of a list does not imply copying. It
simply creates a second reference to the
same list. Most of new python programmers
get caught out by this initially. Consider the
following,

24
List comprehension
A very powerful concept in Python (that also
applies to tuples, sets and dictionaries as we
will see below), is the ability to define lists
using list comprehension (looping)
expression.

25
Tuples
Tuples are similar to lists but only big
difference is the elements inside a list can be
changed but in tuple it cannot be changed,
so tuple is immutable! For better
understanding, recall the divmod() function.

Unpacking Iterables – Iterable is an object


capable of returning its members one at a
time
Student = (‘Bob’, 19, ‘Finance’)
Name, age, studies = student #tuple unpacking 26

Name >> ‘Bob’


Why using a tuple instead of a list
Using a tuple instead of a list guards against
accidental modification.
Tuples are good for describing multiple
properties of one unchanging thing. E.g., the
parts of a phone number, the coefficients of a
polynomial, etc.
The immutability of a tuple makes it a very
lightweight data structure. Program execution
is faster when manipulating a tuple than it is
for the equivalent list.
27
Built-in tuple functions
tuple.count() function counts the number of
specified element that is present in the tuple.
tuple.index() function returns the index of
the specified element. If the elements are
more than one then the index of the first
element of that specified element is returned

28

You might also like