DOT 1003 - Fundamentals of Programming - Week 5
DOT 1003 - Fundamentals of Programming - Week 5
Programming
Week 5 - Lists and Operations
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.
● 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
108
Programming Task 55
● Please create a function which takes two input from user:
● Searchable string or list
● Item for search
● Print the result
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
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)
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)
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.
can be same
print(f"K: {key}, V: {my_dictionary[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
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@mu.edu.tr
● Think Python, 2nd Edition, How to Think
Like a Computer Scientist, Allen Downey,
Green Tea Press (2015)
● www.freecodecamp.com
Homework(s) ● www.geeksforgeeks.com
● finish assignment 3 and 4
128