Fundamentals of
Programming
Week 5 - Lists and Operations
04.11.2024 | DOT 1003 | Mahmut Can Kovan
Agenda
● More list methods
● Matrix
● References in Python
● Dictionary
105
Programming Task 53 - Assignment 3
● Please write a function which create a spruce. Ask user for spruce
height and box size. User will enter positive integer. Box size couldn’t
smaller than the spruce.
>Spruce height: 3 >
>Box Size: 20 >
>-------------------- >
>| | >
>| * | >
>| *** |
>|***** |
>| * |
>--------------------
106
List Methods
>my_list = ["a","b","c","d","a"]
● count() ->count that element in >print(my_list.count("a"))
>my_list.reverse()
the list >print(my_list)
clear() ->Removes all element
>my_list.clear()
● >print(my_list)
● reverse() ->reverses the order of
the element
>2
>["a","d","c","b","a"]
● sort() >
● insert()
● pop()
● remove()
107
Programming Task 54
● Please create a program which takes an input from user and search it
in this sentence below:
● “The quick brown fox jumps over the lazy dog”
● Print the result
>Which item do you want to search? T >
>item T appeared 1 times
>Which item do you want to search? he
>item he appeared 2 times
108
Programming Task 55
● Please create a function which takes two input from user:
● Searchable string or list
● Item for search
● Print the result
>Enter the input to search: Game of the Year >
>Which item do you want to search?: Balatro
>item T appeared 0 times
>Enter the input to search: 00OOO0O0O0O
>Which item do you want to search?: O
>item O appeared 6 times
109
Programming Task 56
● Please create function which takes one argument as string
● remove the vowels from that argument and return new string
●
>nw gm >#Create a loop here for input
>
>menu_button = "new game"
>print(clear_vowels(menu_button))
>
>#create your function below:
>
>
>
>
110
Programming Task 57
● Please create a function named anarya which takes input as list and
return as reversed.
>["Doom", "Max Payne", "FTL"] >game_list=["Doom", "Max Payne", "FTL"]
>['FTL', 'Max Payne', 'Doom'] >print(game_list)
>print(anarya(game_list))
>
>#create your function below:
>
>
>
>
>
111
Programming Task 58 - Assignment 4
● Please create program with loop inside which ask user for input and
append them to the variable named game_list. If user writes exit, print
the list and finish the program.
● Please create a function named anarya which takes input as list and
return as reversed inside of that program.
● Don’t use reversed() method
>["Doom", "Max Payne", "FTL"] >#Create a loop here for input
>['FTL', 'Max Payne', 'Doom'] >
>print(game_list)
>print(anarya(game_list))
>
>#create your function below:
>
>
>
>
112
Programming Task 59
● Please create a function named longest_name which takes list as
argument and return shortest string
>["Doom", "Max Payne", "FTL"] >["Doom", "Max Payne", "FTL"]
>Max Payne >
>print(game_list)
>print(longest_name(game_list))
>
>#create your function below:
>
>
>
>
113
Matrix
>my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
● two dimensional array >print(my_matrix[0][0])
>for row in my_matrix:
● we can implement in python > print(row)
using “list contains lists”
● we can use matrix[row][column]
syntax to access specific element
>1
>[1,2,3]
>[4,5,6]
>[7,8,9]
1 2 3
4 5 6
7 8 9
114
Programming Task 60
● Please create a function named finder() which takes list and element
as arguments and return True or False
● Bonus: Print the location of the element
>item to search: 3 >my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
>[1,2,3] >element = int(input("item to search: "))
>[4,5,6] >for row in my_matrix:
>[7,8,9] > print(row)
>find at row: 0 column: 2 >print(finder(my_matrix, element))
>
>True
>#create your function below:
>
>
>
115
Programming Task 61
● Please create a function named sum_of_row() which takes list and and
row no and return the sum
>item to search: 0 >my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
>[1,2,3] >element = int(input("row no: "))
>[4,5,6] >for row in my_matrix:
>[7,8,9] > print(row)
>6 >print(sum_of_row(my_matrix, element))
>
>#create your function below:
>
>
>
116
Programming Task 62
● Please create a function named sum_of_column() which takes list and
and row no and return the sum
>item to search: 0 >my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
>[1,2,3] >element = int(input("column no: "))
>[4,5,6] >for row in my_matrix:
>[7,8,9] > print(row)
>12 >print(sum_of_column(my_matrix, element))
>
>#create your function below:
>
>
>
117
References in Python
>game_list=["Doom", "Max Payne", "FTL"]
● value of the variable not stored in >print(id(game_list)) #ID NO: 1
>my_num = 3
the variable itself. Variable just >print(id(my_num)) #ID NO: 2
store a “reference no” which is
>my_num += 10
>print(id(my_num)) #ID NO: 3
about the location in memory >my_another_num = 3
>print(id(my_another_num)) #ID NO: 2
where the actual value stored. >my_new_list = game_list
print(id(my_new_list)) #ID NO: 1
● string, int, float, bool are
immutable but list is mutable >140493213341000
>140493412784096
>140493412784416
>140493412784096
>140493213341000
118
Copying the List
>game_list=["Doom", "Max Payne", "FTL"]
● If you assign list_a = list_b, they >my_new_list = game_list
>my_new_list[0] = ["Doom 2"]
will store same reference. >print(game_list)
Lists are mutable, so when you
>print(my_new_list)
●
change the element, you change
just the value, not the reference.
>["Doom 2", "Max Payne", "FTL"]
>["Doom 2", "Max Payne", "FTL"]
119
Copying the List
>game_list=["Doom", "Max Payne", "FTL"]
● If we want to separate the list >my_new_list = []
>
from each other, we can use >for item in game_list:
these:
my_new_list.append(item)
>my_new_list[0] = "Doom 2"
○ for and append to create new list
>my_newest_list = game_list[:]
○ [:] slices >my_newest_list[1] = "Max Payne 2"
>
>print(game_list)
>print(my_new_list)
>print(my_newest_list)
>["Doom", "Max Payne", "FTL"]
>["Doom 2", "Max Payne", "FTL"]
>["Doom", "Max Payne 2", "FTL"]
120
Programming Task 63
● Please create a function named tripler() which takes list as argument
and return a new list multiplied by 3
● Your function should not change the original list.
>My Lucky Numbers: [4,8,15,16,23,42] >my_lucky_numbers = [4,8,15,16,23,42]
>Tripled Numbers: [12,24,45,48,69,126] >tripled_numbers = tripler(my_lucky_numbers)
>print(f"My Lucky Numbers: {my_lucky_numbers}")
>print(f"Tripled Numbers: {tripled_numbers}")
>
>#create your function below:
>
>
>
>
121
Dictionary
>my_dictionary = {}
>
>my_dictionary["FPS"] = "Half Life"
>my_dictionary["TPS"] = "Mafia"
>my_dictionary["RTS"] = "Age of Empires"
>
● Data stored by Key, Value pairs. >print(my_dictionary["RTS"])
● Keys are unique but Values can for key in my_dictionary:
can be same
print(f"K: {key}, V: {my_dictionary[key]}")
● Keys must be immutable, thus we for key, value in my_dictionary.items():
print(f"K: {key}, V: {value}")
can’t use list as key
>Age of Empires
>K: FPS V: Half Life
>K: TPS V: Mafia
>K: RTS V: Age of Empires
122
Programming Task 64
● Please create an inventory system and add at least 3 items as key and
quantities as values.
● print them line-by-line
>item1: 3 >inventory = {
>item2: 1 >"item1": 3,
>item3: 5 >"item2": 1,
>"item3": 5
>}
>#dictionary traversing print code here
>
>
123
Programming Task 65
● use the same list from task 64 and create a function named add_item
which takes two input as string and integer and no return parameter
● don’t forget to check key before adding them to the list. if key exist,
just increase quantity.
>item1: 8 >inventory = {
>item2: 1 >"item1": 3,
>item3: 5 >"item2": 1,
>item4: 1 >"item3": 5
>}
>
>add_item("item1", 5)
>add_item("item4", 1)
>#dictionary traversing print code here
>def add_item(item, quantity) # function here
124
Programming Task 65
● Use the same program from task 65 and create a function named
remove_item which takes two input as string and integer and no
return parameter
● don’t forget to check values. Item quantity couldn’t lower than 0
>item1: 6 >inventory = {"item1": 3, "item2": 1, "item3": 5}
>item2: 1 >
>item3: 5 >add_item("item1", 5)
>item4: 0 >add_item("item4", 1)
>
>remove_item("item4", 6)
>remove_item("item1", 2)
>
>#dictionary traversing print code here
>def add_item(item, quantity) # function here
>def remove_item(item, quantity) #function here 125
Dictionary
>my_dictionary = {}
>
>my_dictionary["FPS"] = "Half Life"
>my_dictionary["TPS"] = "Mafia"
>my_dictionary["RTS"] = "Age of Empires"
>
Removing Items >del my_dictionary["TPS"]
>
● we can use del for deleting items >for key, value in my_dictionary.items():
print(f"K: {key}, V: {value}")
from dictionary but don’t forget >
>my_dictionary["TBS"] = "Civilization 5"
to check if the key already exists >my_dictionary.pop("RTS")
>
in the dictionary >print("")
>for key, value in my_dictionary.items():
● another method is pop. Pop > print(f"K: {key}, V: {value}")
method is also have return value.
● If you want to completely remove >K: FPS V: Half Life
>K: RTS V: Age of Empires
the dictionary, use .clear() >
>K: FPS V: Half Life
>K: TBS V: Civilization 5
126
Programming Task 66
● Use the same program from task 66 and create a function named
remove_item which takes two input as string and integer and no
return parameter
● don’t forget to check values. Item quantity couldn’t lower than 0
● if item quantity == 0, delete it from the inventory.
>item1: 6 >inventory = {"item1": 3, "item2": 1, "item3": 5}
>item2: 1 >
>item3: 5 >add_item("item1", 5)
>add_item("item4", 1)
>
>remove_item("item4", 6)
>remove_item("item1", 2)
>
>#dictionary traversing print code here
>def add_item(item, quantity) # function here
>def remove_item(item, quantity) #function here 127
End of the Week
Thanks Materials
mahmutcankovan@[Link]
● Think Python, 2nd Edition, How to Think
Like a Computer Scientist, Allen Downey,
Green Tea Press (2015)
● [Link]
Homework(s) ● [Link]
● finish assignment 3 and 4
128