Chapter 7 - Iterative - Data Types - Part1
Chapter 7 - Iterative - Data Types - Part1
Iterative data
types
Lecturer: Đinh Thị Hà
Email: [email protected]
Mobile: 0947830983
Contents
• 7.1. List
• 7.2. Tuple
• 7.3. Dictionary
• 7.4. Set
• 7.5. Array
• 7.6. Convert among iterative data types
7.1. List
• 7.1.3. Methods
• Concept: Lists are one of 4 built-in data types in Python used to store
collections of data. It is used to store multiple items in a single variable.
• Create a list:
• Syntax: Var_name=[list of values]
Or use the constructor method list()
Var_name=list(list of values)
• Examples:
L1=[1, 5, 8]
L=list(“math”, “physics”, “chemistry”)
• Features/characteristics:
• List items are indexed, ordered, changeable, and allow duplicate values, items can be of
any data type.
• A list can contain different data types
• Items in a list are indexed begin with 0
7.1.2. Access list
• Notes: items in a list are indexed begin with 0 and Negative indexing means start from
the end.
List = [12, ‘learning’, 3, ‘Python’, 5]
print("Accessing element using negative indexing")
# print the last element of list
print(List[-1])
# print the third last element of list
print(List[-3])
#####
L2=[“toan”, “ly”,”hoa”, “van”,”su”, “dia”]
Print(L2[1])
Print(L2[-1]) #dia
Print(L2[-2]) #su
7.1.2. Access list
• To determine if a specified item is present in a tuple use the ”in” operator/ keyword: .
If “sinh” in L2:
Print(“mon ’sinh’ co trong L2”)
• To go through items in a list, use for loop along with “in” operator or refer to the
index of item with range() function or use while loop along with len() function .
Ls=[“toan”, “ly”,”hoa”, “van”,”su”, “dia”]
For x in Ls:
Print(x)
For i in range(len(Ls)):
Print(Ls[i])
i=0
While i<len(Ls):
Print(Ls[i])
i=i+1
7.1.4. List of list
• List of list / nested list allow create a list in which each item in the list is also another list.
• Examples:
matrix = [[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]
• Or:
matrix = []
for i in range(5):
# Append an empty sublist inside the list
matrix.append([])
for j in range(5):
matrix[i].append(j)
print(matrix)
• Or:
matrix = [[j for j in range(5)] for i in range(5)]
print(matrix)
7.1.4. List of list
• Examples:
matrix = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
flatten_matrix = []
for sublist in matrix:
for val in sublist:
flatten_matrix.append(val)
print(flatten_matrix)
• Or:
matrix = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
# Nested List Comprehension to flatten a given 2-D matrix
flatten_matrix = [val for sublist in matrix for val in sublist]
print(flatten_matrix)
7.1.3. Methods
Practice exercises
1. Write a program that asks the user to enter a list of integers. Do the
following:
(a) Print the total number of items in the list.
(b) Print the last item in the list.
(c) Print the list in reverse order.
(d) Print Yes if the list contains a 5 and No otherwise.
(e) Print the number of fives in the list.
(f) Remove the first and last items from the list, sort the remaining items, and print
the result
(g) Print how many integers in the list are less than 5.
Practice exercises
• Notes: Tuples are unchangeable, meaning that you cannot change, add, or remove items
once the tuple is created.
• Solutions/ workarounds:
• convert the tuple into a list, change the list (by add or remove items), and convert the list
back into a tuple.
• Or add tuple to tuple by + operator
• Ví dụ:
T=(“Apple”, “Grape”, “Mango”)
L=list(T) ; #convert tuple into a list
L[1]=“Cherry”; L.append(“Lemon”); L.pop(2) #change the list
T=tuple(L) #convert the list into a tuple
Print(T)
• Examples:
• tup = ("apple", "banana", "cherry", "orange", "kiwi")
print(tup[1]); print(tup[-1])
• print(tup[2:4]); print(tup[:4]); print(tup[2:])
• if "apple" in tup:
print("Yes, 'apple' is in the fruits tuple")
• for x in tup:
print(x)
• for i in range(len(tup)):
print(tup[i])
7.2.2. Access tuple
• Packing/Unpacking a Tuple
• " packing" a tuple: When creating a tuple and assigning values to it.
• "unpacking": extract the values in a tuple back into variables
• Examples:
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)
• Can use asterisk (*) to replace a part of a tuple (some of items) to match with
some of the variables corresponding
• Example:
T=(“Math”, “Physics”, ”Chemistry”, “Biology”, “History”)
(to,*ph, su)=T
Print(to);print(ph); print(su) #Math [‘Physics’, ‘Chemistry’, ‘Biology’] History
7.2.3. Methods
THANK YOU