TALENTS FOR DEVELOPMENT -Petrovietnam UniversityFoundations of Engineering II- PVUTrần Ngọc Hiệp, MSc.... TALENTS FOR DEVELOPMENT -Petrovietnam UniversityFoundations of Engineering II- P
Trang 1TALENTS FOR DEVELOPMENT -Petrovietnam University
Chapter 5: List, Tuple, Set and Dictionary
Lecturer: Trần Ngọc Hiệp, MSc.
Email: hieptn@pvu.edu.vn Mobile: 0901 25 2468
Contents
Array Advantages of Array Tuples
Sets Dictionaries
1
Trang 2TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
Scenario
3
Scenario
What will we do if the number of
students are 1000 or more…
3
Trang 3TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
5.1 Arrays
Array are data structures consisting of related data
items of the same type.
An array is a consecutive group of memory locations.
The memory locations in the array are known as
elements of array.
Total number of elements in the array is call the
length of the array
5
5.1 Arrays
To refer to a particular location or element in the
array, we specify the name of the array and the
position number of the particular element in the
array in square brackets ([]);
The position number is more formally called a
subscript or index
The rules of variable naming convention apply to
array names
5
Trang 4TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
5.1 Arrays
In Python use Lists instead of Arrays
Lists are used to store multiple items in a single
variable.
List items are ordered, changeable, and allow
duplicate values.
The list is ordered, it means that the items have a
defined order, and that order will not change.
The list is changeable, meaning that we can change,
add, and remove items in a list after it has been
created.
The list is allowed duplicates, lists can have items with
the same value
7
5.1 Arrays
The first element has subscript 0
The highest subscript in array is 1 less than the
number of elements in the array.
Array
Data type
Name Size
7
Trang 5TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
5.1 Arrays
9
5.2 Advantages of Arrays
Array can store a large number of value with single name.
The values store in the array can sorted easily
The search process can be applied on array easily.
It can be used to implement other data structures like linked lists, stacks, queues, trees, graphs…
2D arrays are used to represent matrices.
9
Trang 6TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
5.3 Type of Arrays
Type of Arrays
One dimensional Arrays (One-D)
Two dimensional Arrays (Two-D)
Multi dimensional Arrays (Multi-D)
11
One – D Arrays
A type of array in which all elements are arranged in the
form of a list is known as One-D array or single
dimensional array or linear list
Declaring and Initializing One-D Array:
11
Trang 7TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
Accessing Array Elements
13
An individual element within an array is accessed by
use of an index
An index describes the position of an element within
an array.
Note: In Python the first element has the index zero!
Accessing Array Elements
Negative index!
Range indexes
13
Trang 8TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
Accessing Array Elements
15
Index from beginning to:
Index to the end
Accessing Array Elements
Example: Finding the Maximum
15
Trang 9TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
Lists
17
Strings
The most common use for one-dimensional arrays is
to store strings of characters.
In Python, a string is defined as a character array
terminated by a null symbol (‘\0’).
Access character in string
17
Trang 10TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
Strings
19
Reading string from keyboard
Strings 19
Trang 11TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
Strings
21
Strings 21
Trang 12TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
Strings
23
Two-Dimensional Arrays
A two-dimensional array is a list of one-dimensional arrays.
Declaring and initializing Two-D Array:
23
Trang 13TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
Two-Dimensional Arrays
Input and output:
25
Your Tasks
1 Write a Python program to find the sum and average of one
dimensional integer array.
2 Write a Python program to swap first and last element of
an integer 1-d array.
3 Write a Python program to find the sum of all elements of
matrix of size m x n
4 Write a Python program to find the sum by column and
sum by row of matrix of size m x n
5 Write a Python program to find the largest and smallest
element in each row and each column of matrix of size m x n
25
Trang 14TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
5.2 Tuples
Tuples are used to store multiple items in a single
variable.
Tuple items are ordered, unchangeable, and allow
duplicate values.
Tuples are unchangeable, meaning that we cannot
change, add or remove items after the tuple has been
created.
27
5.2 Tupes 27
Trang 15TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
5.3 Sets
Sets are used to store multiple items in a single
variable.
Set items are unordered, unchangeable, and not
allow duplicate values.
Unordered means that the items in a set do not have a
defined order.
Set items can appear in a different order every time
you use them, and cannot be referred to by index or
key.
Sets cannot have two items with the same value.
29
5.3 Sets 29
Trang 16TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
5.3 Sets
31
5.4 Dictionaries
Dictionaries are used to store data values in key:value
pairs.
Dictionary items are ordered, changeable, and not
allow duplicate values.
31
Trang 17TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
5.4 Dictionaries
33
Summary
1 • Arrays, type of arrays
2 • Why we need arrays?
3 • Tuples
4 • Sets
5 • Dictionaries 33
Trang 18TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU Trần Ngọc Hiệp, MSc
Quiz
35
Discussion
35
Trang 19TALENTS FOR DEVELOPMENT -Petrovietnam University
Foundations of Engineering II- PVU
37