ML Lab 02 - Python Data Structures, NumPy, SciPy
ML Lab 02 - Python Data Structures, NumPy, SciPy
3. Sets: A collection of data which is unordered, unchangeable and does not allow
duplicate members
x_list = [1, 2, 3, 4, 5]
x_tuple = (1, 2, 3, 4, 5)
x_set = {1, 2, 3, 4, 5}
print(x)
Output:
[1, 2, 3, 4, 5]
Lists
• Lists can hold different types of data:
a = [11, 2, 93, 401, 560]
b = [1.5, 6.6, 7.3, 8.9]
c = ["apple", "banana", "cherry"]
d = [True, True, False, True, False]
e = []
f = list("ROBOT")
Output:
print(g)
Output:
print(h)
print(h[3])
print(h[3][0])
print(h[3][1])
Output:
print(j)
Output:
0 1 2 3 4 5 6 7
print(my_list[0]) Output:
print(my_list[1])
print(my_list[5]) 14
print(my_list[7]) 20
77
62
Changing Items in a List
• The value of an item in a list can be changed by using its index
0 1 2 3 4 5 6 7
my_list[2] = 37
print(my_list)
Output:
0 1 2 3 4 5 6 7
my_list = [14, 20, 93, 41, 56, 77, 38, 62]
-8 -7 -6 -5 -4 -3 -2 --1
print(my_list[-1]) Output:
print(my_list[-2])
print(my_list[-3]) 62
print(my_list[-8]) 38
77
14
Changing Items with Negative Index
• Negative indexing can also be used to change the value of items
-8 -7 -6 -5 -4 -3 -2 --1
my_list[-5] = 500
print(my_list)
Output:
print( my_list[2:5] ) 0 1 2 3 4 5 6 7
print( my_list[2:] ) 0 1 2 3 4 5 6 7
print( my_list[:5] ) 0 1 2 3 4 5 6 7
0 1 2 3 4
0 1 2 3
0 1 2 3
my_list[1:3] = ["strawberry"]
print( my_list)
Output: True
False
False
True
Looping a List
• The for loop can used to iterate through the items of a list
• The number of times the loop executes is equal to the number of items
• The iterator (fruit) takes the value of each item of every iteration
my_list = ["apple", "banana", "cherry", "orange"]
Output: apple
banana
cherry
orange
Looping a List
• If the indices are needed in the loop, then the range and length functions can be
used
my_list = [1,2,3,4,5]
for i in range(len(my_list)):
my_list[i] = 2 * my_list[i]
print(my_list)
listA.append("mango")
print(listA)
listA.insert(1, "mango")
print(listA)
• Another way to do this is to use the addition operation to concatenate the lists
(listA = listA + listB)
listA.remove("cherry")
print(listA)
listB.pop(1)
print(listB)
Output: 4
• The index function returns the index of the first occurrence of a specified item
index_val = listC.index(3)
print(index_val)
Output: 2
Other List Functions
• The reverse function reverses the order of the items in the list
listA = ["apple","banana","cherry", "mango"]
listA.reverse()
print(listA)
print( my_2d_list[2][3] )
Output: 64
Strings - Review
• Python supports the string data type which is an array of characters
h = “Manipulator”
print(h) Output: Manipulator
• You can get the number of characters with the len() function
g = len(h)
print(g) Output: 11
Strings - Review
• You can concatenate strings easily in python
c = “Computer”
d = “Vision” Output: ComputerVision
e = c + d
print(e)
f = c + “ “ + d
print(f) Output: Computer Vision
• You can check if a character is present in the string with the “in” keyword
print(eng2span)
uno
Output: dos
tres
Dictionaries - Accessing
• The following dictionary maps English (keys) to Numbers (values)
eng2num = {'one': 1,
'two': 2,
'three': 3}
1
Output: 2
3
Dictionaries – Changing Value
• Consider the following dictionary which maps prices
prices = {'burger': 250,
'sandwich': 150,
'pizza': 400}