0% found this document useful (0 votes)
8 views38 pages

16 List

The document is an online course provided by UNESCO UNITWIN in collaboration with Handong Global University, focusing on the understanding and usage of lists in programming. It covers various topics including list operations, methods, and 2D lists, along with exercises to practice these concepts. Additionally, it emphasizes the prohibition of commercial use of the educational materials provided.

Uploaded by

Thae Thae Aung
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)
8 views38 pages

16 List

The document is an online course provided by UNESCO UNITWIN in collaboration with Handong Global University, focusing on the understanding and usage of lists in programming. It covers various topics including list operations, methods, and 2D lists, along with exercises to practice these concepts. Additionally, it emphasizes the prohibition of commercial use of the educational materials provided.

Uploaded by

Thae Thae Aung
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/ 38

UNESCO UNITWIN

Online Course
Supported by

Handong Global University


Ministry of Education, Korea

* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational materials including the video and other
relevant materials for commercial and for-profit use.
List
16
Global Leadership School
Handong Global University
Learning Objectives
• Understand Lists
• Use operators on the Lists
• Use methods with Lists
• Understand 2D Lists
List
• List is sequence of values
• The components in the list
• Can store different data types
• They are called elements or items
• Can change value of list element/items
>>> cheeses = ['Cheddar', 'Edam', 'Gouda']
>>> numbers = [17, 123]
>>> print(numbers)
[17, 123]
>>> cheeses[0]
Cheddar
>>>numbers[1]
123
>>> numbers
[17, 123]
List – in operator
>>> cheeses = ['Cheddar', 'Edam', 'Gouda']

>>> cheeses
['Cheddar', 'Edam', 'Gouda']

>>> 'Edam' in cheeses


True
>>> 'Brie' in cheeses
False

>>> for food in cheeses :


print(food)
Cheddar
Edam
Gouda
Using Lists
cheeses = ['Cheddar', 'Edam', 'Gouda']
numbers = [1, 3, 5, 7, 9, 11]

for cheese in cheeses :


print(cheese)

for i in range(len(numbers)) :
numbers[i] = numbers[i] * 2
print(numbers[i])

print(numbers)
List, Operator
• Operator # The + operator concatenates lists
• +: Can join two lists to >>> a = [1, 2, 3]
create new list >>> b = [4, 5, 6]
• *: List * integer dupl >>> c = a + b
icate list content inte >>> c
ger times [1, 2, 3, 4, 5, 6]

# Similarly, the * operator repeats a list


a given number of times
>>> [‘a’] * 4
[‘a’, ‘a’, ‘a’, ‘a’]
>>> a = [1, 2, 3]
>>> a * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
List, slice
• Slice
#list slice
• Same with how it is >>> t = ['a', 'b', 'c', 'd', 'e', 'f']
used on String >>> t[1:3]
['b', 'c']
• Choose range of list >>> t[:4]
items using index ['a', 'b', 'c', 'd']
• List_fruit[ :3] >>> t[3:]
['d', 'e', 'f']
Exercise 1
• Receive input of the number of items in the
newly created list.
• Create a list
• Repletely receive inputs as many times as the
number of items
• Use ‘+’ operator to add item to list
• Print out entire list
Exercise 1, Code and Result
num = int(input(”Enter the number of List element : "))
NewList = []

for i in range(num):
print(“index”, i)
t = input(”Enter ta number to add: ")
tempList = [t]
NewList = NewList + tempList

print(NewList)
List, Methods
.append() Adds an element at the end of the list

.insert() Adds an element at the specified position

.extend() Add the elements of a list (or any iterable), to the


end of the current list
.sort() Sorts the list

.pop() Removes the element at the specified position

.remove() Removes the first item with the specified value


.append()
# method append

>>> t1 = [‘x', ‘y', ‘z']


>>> t1.append(‘a')
>>> t1
[‘x', ‘y', ‘z', ‘a']
>>> t1.append(‘e')
>>> t1
[‘x', ‘y', ‘z', ‘a‘, ‘e’]
.insert()
# method insert

>>> t1 = [‘x', ‘y', ‘z']


>>> t1
[‘x’, ‘y', ‘z']
>>> t1.insert(1, ‘a’)
>>> t1
[‘x’, ‘a’, ‘y', ‘z']
>>> t1.insert(1, ‘e’)
>>> t1
[‘x’, ‘e’, ‘a’, ‘y', ‘z']
.extend()
# method extend

>>> t1 = [‘x', ‘y', ‘z']


>>> t2 = ['d', 'e']
>>> t1.extend(t2)
>>> t1
[‘x’, ‘y', ‘z', 'd', 'e']
>>> t2.extend(t1)
>>> t2
[‘d’, ‘e’, ‘x’, ‘y', ‘z', 'd', 'e']
.sort()
#method sort

>>> t = ['d', 'c', 'e', 'b', 'a']


>>> t.sort()
>>> t
['a', 'b', 'c', 'd', 'e']
.pop()

#method pop(index)

>>> t = ['a', 'b', 'c‘, ‘d’, ‘e’]

>>> x = t.pop(0) + t.pop(1)

>>> t
[‘b', ‘d‘, ‘e’]

>>> x
‘ac’
.remove()

# method remove(value)

>>> t = ['a', 'b', 'c']


>>> t.remove('b')
>>> t
['a', 'c']
Method Example 1
f1=['apple', 'blueberry', 'melon', 'tomato']
f2=['strawberry', 'lemon', 'banana']
f3=f1+f2
print('f1+f2= ', f3)

f3.append('blackberry')
f3.sort()
print("after sorting = ", f3)
Method Example 2
f1=['apple', 'blueberry‘, 'melon', 'tomato']
f2=['strawberry', 'lemon', 'banana']
f3=f1+f2
print(f3)

#remove element with its first char 'b‘


total=len(f3)

i=0
while i < total:
if f3[i][0] == "b" :
f3.remove(f3[i])
i=i-1
total=total-1
i=i+1

print("remove all 'b' elements = ", f3)


Method Example 2, Explanation
• After removing string that
while i < total: begins with ‘b’
if f3[i][0] == "b" : • The total number of items in list
f3.remove(f3[i])
i=i-1
f3 is reduced by one.
total=total-1 (total=total-1)
i=i+1 • To check next string, we need to
do i=i-1
• The index of the deleted item
will be given to the item behind
it.
Exercise 2
• Receive a name, and create a list with the
letters of the name then print
• Receive a character to remove and delete it
from the list
• After deletion, print out the list
Exercise 2 Code
name = input(“Enter a name: ")

nameList = []
for ch in name :
nameList.append(ch)
print(nameList)

chDel = input(”Letter to remove: ")

count = 0
for ch in nameList :
if ch == chDel :
count = count + 1

for i in range(count) :
nameList.remove(chDel)
print(nameList)
Exercise 3
• Receive a string
• Make alphabets should be lowercase
• Arrange the alphabets in order
• All spaces should be deleted
Exercise 3, Code and Result
word = input(”Enter a word: ")
word = word.lower()

wordList = [] f
or ch in word :
wordList.append(ch)
print(wordList)

wordList.sort()
while wordList[0] == ' ':
wordList.remove(' ')

print(wordList)
2-dimensional List
• Create 2D list
• List within a list
• s = [ ["kim", 90, 75] , ["park", 80, 95] , ["choi", 76, 85] ]

S[0] S[1] S[2]

S[0][2]
S[0][0]

S[0][1]
S[1][0]
S[2][1]
Using 2-dimensional List

fq= [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]]

for i in range(5):
for j in range(6):
fq[i][ j] = i+j
print(i,"th row : ", fq[i])
print("="*50)
print("all : ", fq)
print("="*50)
2D list, Grade
s = [ ["kim", 90, 75], ["park", 89, 95], ["choi", 76, 85] ]
print( s )
for i in range( len(s) ):
print( s[i][0] )
sum=0
for j in range( 1, len(s[i]) ):
sum = sum + s[i][ j]

print("sum = ", sum, "average = ", sum/j, "\n")


Exercise 4
• Multiplication table, multiplicand from 2 to 16
• Save in created 2D list
• Save each multiplicand to single row
• Print saved result
Exercise 4, Code and Result
# create a 2D list with 15 items, each of which has 10 0s.
mul = [ 10 * [0] for i in range(15) ]

for i in range(15):
print(mul[i]) #print by row

print("Start!!")

for i in range(15):
for j in range(10):
mul[i][ j] = (i+2) * ( j+1)
print(mul[i]) #print by row

print("Done!!")
Exercise 5
• Make a unit matrix
• First, receive the size of the unit matrix.
• Create and output a unit matrix with a 2D list.
Exercise 5 Code
Size = int(input(”Size of matrix: "))

Unit_Matrix = []
for a in range(Size):
temp_row = []
for b in range(Size):
temp_row.append(0)
Unit_Matrix.append(temp_row)

for i in range(Size):
for j in range(Size):
if i == j:
Unit_Matrix[i][ j] = 1
else:
Unit_Matrix[i][ j] = 0

for i in range(Size):
print(Unit_Matrix[i])
Lecture Summary
• Understand Lists
• Sequence of values
• Can store different data types
• Can change value of list element/item
• Use operators on the List
• + : Join two lists to create new list
• * : (list) * (integer) duplicate list content integer times
• Use methods with Lists
• .append, .insert, .extend, .sort, .pop, .remove …
• Understand 2D Lists
• List within a list
Practice Problem 1
• What’s the result of the execution?
n = [1,3,5]
print(n * 2)

• [2, 6, 10]
• [2, 6, 10, 2, 6, 10]
• [1, 3, 5]
• [1, 3, 5, 1, 3, 5]
Practice Problem 1, Answer
• What’s the result of the execution?
n = [1,3,5]
print(n * 2)

• [2, 6, 10]
• [2, 6, 10, 2, 6, 10]
• [1, 3, 5]
• [1, 3, 5, 1, 3, 5]
Practice Problem 2
• What’s the result of the execution?
t1=[‘a‘, ‘b‘, ‘c‘]
t2=[‘A’, ‘B’]
t1.insert(1, ‘x’)
t1.extend(t2)
print(t1)

• [‘a‘, ‘b‘, ‘c‘, ‘x’, ‘A’, ‘B’]


• [‘x’, ‘a‘, ‘b‘, ‘c‘, ‘A’, ‘B’]
• [‘a‘, ‘x‘, ‘b‘, ‘c‘, ‘A’, ‘B’]
• [‘A’, ‘B’, ‘a‘, ‘x’, ‘b‘, ‘c‘]
Practice Problem 2, Answer
• What’s the result of the execution?
t1=[‘a‘, ‘b‘, ‘c‘]
t2=[‘A’, ‘B’]
t1.insert(1, ‘x’)
t1.extend(t2)
print(t1)

• [‘a‘, ‘b‘, ‘c‘, ‘x’, ‘A’, ‘B’]


• [‘x’, ‘a‘, ‘b‘, ‘c‘, ‘A’, ‘B’]
• [‘a‘, ‘x‘, ‘b‘, ‘c‘, ‘A’, ‘B’]
• [‘A’, ‘B’, ‘a‘, ‘x’, ‘b‘, ‘c‘]
Thank you
16 List
Contact Info
[email protected]
https://ecampus.handong.edu/

* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational
materials including the video and other relevant materials for commercial and for-profit use.
CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik.

You might also like