0% found this document useful (0 votes)
1 views44 pages

Python(MASAI) Module 3 Part I

The document provides an overview of Python data structures including lists, tuples, and dictionaries, focusing on their characteristics and methods. It explains lists as mutable sequences that can contain mixed data types, while tuples are immutable. Additionally, it discusses operations such as modifying lists, calculating mean and median, and the implications of data types in data science and artificial intelligence contexts.

Uploaded by

Rahul R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views44 pages

Python(MASAI) Module 3 Part I

The document provides an overview of Python data structures including lists, tuples, and dictionaries, focusing on their characteristics and methods. It explains lists as mutable sequences that can contain mixed data types, while tuples are immutable. Additionally, it discusses operations such as modifying lists, calculating mean and median, and the implications of data types in data science and artificial intelligence contexts.

Uploaded by

Rahul R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

List/Arrays, Tuple, Dictionaries,

Loops and File I/O


Python for Data Science & Artificial Intelligence

Instructor: Dr. Sneha Singh, SCEE


Term : June 2025
Primitive vs Compound Data Types

Primary

Compound

Image credit: https://pynative.com/python-data-types/


LISTS

● List is a general sequence object that allows the individual items to be of


different types.

● A list is denoted by square brackets, [ ].

● Equivalent to arrays in other languages.

● Lists are mutable, i.e., a list can be changed without having to create a
new list object
LISTS

?
Arrays

❏ How arrays are different ?

❏ Variable with fixed number (say N) of entries


❏ data structure containing a group of elements
❏ Elements must be of same type in other languages
❏ In python, they can be of any type
❏ data elements stored at contiguous memory locations
❏ Arrays are called lists in Python
Creating Lists
Creating Lists
Creating Lists
Creating Lists
Creating Lists
Modifying Lists: Lists are MUTABLE

Assigning to an element at an index changes the value

L is Now

L is Now
Modifying Lists: Methods

● Lists are Objects like Strings, so it has methods

● Objects have data and also have methods and functions

● objects_list.do_something( ), lists out the methods and functions

● However, methods modify the list itself unlike strings


Modifying Lists: Methods

● Lists are Objects like Strings, so it has methods

● Objects have data and also have methods and functions

● objects_list.do_something( ), lists out the methods and functions

● However, methods modify the list itself unlike strings

. represents object method


and used to access attributes
Modifying Lists: Methods

● Lists are Objects like Strings, so it has methods

● Objects have data and also have methods and functions

● objects_list.do_something( ), lists out the methods and functions

● However, methods modify the list itself unlike strings


Modifying Lists: Common Methods

● L.append( ) : Adds one item to the end of the list.

● L.extend( ) : Adds multiple items to the end of the list.

● L.count( ele ) : Count the element ‘ele’ in the list.

● L.insert( idx, ele ) : Inserts ‘element’ at position idx.

● L.pop( idx ) : Remove element at index ‘idx’ from the list. Default:Last.

● L.reverse( ) : Reverse the order of items in list.

● L.remove( ele ) : Finds ‘element’ in list and deletes it from the list.

● L.sort( ) : Sorts the list in- place i.e., changes the sequence in the list.
Modifying Lists: Common Methods
Modifying Lists: Common Methods
Modifying Lists: Common Methods
Modifying Lists: Common Methods
Modifying Lists: Common Methods
Modifying Lists: Common Methods
Modifying Lists: Common Methods
Searching Index: List Methods

>>> b = [ ] # empty list

>>> a = [1 ,3 , 5, 7, 9, 4, 5, "I am in list"]

>>> print(a.index(5))

>>> print(a[3:].index(5))

>>> print(a.index(5, 3)) # (element, start, end)

6
Concatenation: List Methods

>>> l1 = [1, 4, -2, 3]

>>> l2 = [1, 3, 4]

>>> print(l1 + l2, l1)

[1, 4, -2, 3, 1, 3, 4] [1, 4, -2, 3]

>>> l1.extend(l2) # same as l1 = l1 + l2

>>> print(l1)

[1, 4, -2, 3, 1, 3, 4]
Remove Vs POP: List Methods
>>> a = list(range(10)) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> a.insert(3,1) # [0, 1, 2, 1, 3, 4, 5, 6, 7, 8, 9]

>>> a.remove(1) # [0, 2, 1, 3, 4, 5, 6, 7, 8, 9], removes value in argument

>>> a.remove(10) # [0, 2, 1, 3, 4, 5, 6, 7, 8, 9],

ValueError: list.remove(x): x not in list

>>> p = a.pop() # p = 9, a = [0, 2, 1, 3, 4, 5, 6, 7, 8]

>>> q = a.pop(1) # q = 2, a = [0, 1, 3, 4, 5, 6, 7, 8], removes value at index

>>> q = a.pop(20) #q = 2, a = [0, 1, 3, 4, 5, 6, 7, 8],

IndexError: pop index out of range


List Methods: Exercise_Mean/Median

● Mean or Average is ratio of sum of numbers (x1, x2, … ,xN) and total
numbers (N)

● Solution of argmin
x
ΣNi=1 (x - xi)2

● Mean of [1, 2, 4] is 7/3

● Mean of [1, 2, 4, 6] is 13/4


List Methods: Exercise_Mean/Median

● Median is the value/values in the middle of the series of values

● To get median

○ First sort the values


○ Then find the value/values in the middle

● Solution of argminx ΣNi=1 |x - xi|

● Mean of elements in [1, 2, 4] is 7/3, their Median is/are 2

● Mean of elements in [1, 2, 4, 6] is 13/4, their Median is/are: range [2, 4]


Median (N Even)
Solution of argminx ΣNi=1 |x - xi|

Proof (if N is even):

● Let’s assume x1 to xN are sorted.


● Take two terms from both ends, i.e., first and last term, i.e.:

● s1 = |x - x1| + |x - xN|
● When will s1 be minimum?
x x1 xN
● x < x 1:

● x > x N: x1 xN x

● x 𝟄 [x1 xN] : x1 x xN
Median (N Even)

● s1 = |x - x1| + |x - xN|
● When will s1 be minimum?
x1 x xN
● x 𝟄 [x1 xN] :
x1 x2 x3 x4 x5 x6 x7

● Similarly, s2 = |x - x2| + |x - xN-1| will be minimum at x ∈ [x2, xN-1]


● .
● .
● s(N-1)/2 = |x - x(N-1)/2| + |x - x(N-1)/2 + 2| will be minimum at x ∈ [x(N-1)/2, x(N-1)/2+2 ]
● Instead of s(N+1)/2, we will be left with |x - x(N+1)/2|, All min at x = x(N+1)/2!
Median
Mean Vs Median

Median has some advantages w.r.t. Mean.

● Who is better?
○ A team performing best consistently,
○ Or a team who performs bad all the times but outperform by large margin
in just one game?
● Consider the array: [1, 1, 2, 2, 2]
● What will be its mean and median?
● Now, let’s assume that there is a noise/mistake in the data:- [1, 1, 2, 2, 2, 100]
● 100 is added in the end by mistake
● Now, what will be mean and median?
● Median remains the same, i.e. it is less sensitive to the outliers (100 in our
case).
Median using LISTs
List: Exercise

How to sort the following array without using sort() method?

- [5, 2, 3, 1, 9, 0]
- [0, 5, 2, 3, 1, 9]
- [0, 1, 5, 2, 3, 9]
-
-
-
- [0, 1, 2, 3, 5, 9]
List: Exercise (Finding Temperature of Rooms)

1. N (=3) sensors in each room at different locations


2. readings = [ [12.4, 14.19, 23.19], [ 10.32, 20.14, 30.53] ]
3. Generate alarm if average temperature > 20
4. Or should we use median?
5. Should we treat fire at a corner as outlier!

Images by Elias and kewl from Pixabay


List of Lists

● Lists are of arbitrary length and and easily be nested.


● Simplest nested lists are 2D matrices.

my2DList = [ [1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16] ]


List of Lists

● Need not to be homogeneous


● Nested lists can be created using any data types of arbitrary lengths.

my2DList = [ [1,2,3,’a’],[5,6,7,’cat’],[9,10,’e’,12],[’beta’,14,15,16] ]
List of Lists
Output: Iteration 1
List of Lists
Output: Iteration 2
List of Lists
Output: Iteration 3
2D Data: Grayscale Image

2D Array
Image
239 245 255 240 219

229 255 238 239 245

255 255 255 246 255

235 251 247 238 255

200 207 234 220 240


2D Data: RGB Image
Tuples

● Equivalent to lists, except that they can’t be changed.

● A tuple is denoted by parenthesis and elements separated by commas, ( ).

● Tuples are immutable, and allows the individual items to be of different types.
Tuples: IMMUTABLE

● Tuples are immutable, and allows the individual items to be of different types.
Tuples: Assignment

You might also like