Week 4 MAK
Week 4 MAK
12.03.2025
Dr. Merve AKBAŞ KAPLAN 1
Data Structures
You can think of a data structure as a way of organizing and storing
data such that we can access and modify it efficiently. Earlier, we have
seen primitive data types like integers, floats and Booleans.
• Now, we’ll take a deeper look at the advanced Python data structures.
• Lists – list()
• Tuples – tuple()
• Dictionaries – dict()
• Strings – str()
• Sets and frozen sets – set() & frozenset()
2
Lists
3
Lists
4
Lists
5
Lists: MUTABILITY
6
List OPERATORS
7
List Methods
Method Description
append(item) Adds a new item to the end of the list
clear() Removes all items from the list
copy() Returns a shallow copy of the list
count(item) Returns the total number of items passed as an argument
extend(iterable) Extends the list by appending elements from the iterable
index(item) Returns the index of an item
insert(index, item) Inserts an item to the list at the defined index
pop() Removes and returns the last item from the list
remove(item) Removes an item from the list
reverse() Reverses the order of all elements of the list
sort(key=None, reverse=False) Sort all elements of a list in the ascending order
USAGE: your_list.method() 8
List Methods
➢append(item) ➢count(item)
>>> L = [1, 'a', 2, 'b', 1] >>> L = [1, 'a', 2, 'b', 1]
>>> L.append(3) >>> L.count(1)
>>> L 2
[1, 'a', 2, 'b', 1, 3] ➢extend(iterable)
➢clear() >>> L1 = ['a', 'b', 'c']
>>> L = [1, 'a', 2, 'b', 1] >>> L2 = [1, 2, 3]
>>> L.clear() >>> L1.extend(L2)
>>> L >>> L1
[] ['a', 'b', 'c', 1, 2, 3] 9
List Methods
➢index(item)
➢extend(iterable)
➢ >>> L = [1, 'a', 2, 'b', 1]
➢ >>> L = [] ➢ >>> L.index('a')
➢ >>> L.extend('hello') ➢ 1
➢ >>> L
➢ ['h', 'e', 'l', 'l', 'o']
➢pop()
➢insert(index, item)
➢ >>> L = [1, 'a', 2, 'b', 1]
➢ >>> L = [1, 'a', 2, 'b', 1] ➢ >>> L.pop()
➢ >>> L.insert(3, 'c') ➢ 1
➢ >>> L ➢ >>> L
➢ [1, 'a', 2, 'c', 'b', 1] ➢ [1, 'a', 2, 'b'] 10
List Methods
➢remove(item) ➢sort(key=None, reverse=False)
>>> L = [1, 'a', 2, 'b', 1] >>> L = [3, 2, 4, 1, 5, 4]
>>> L.remove('a') >>> L.sort()
>>> L >>> L
[1, 2, 'b', 1] [1, 2, 3, 4, 4, 5]
➢reverse()
>>> L.sort(reverse=True)
>>> L = [3, 2, 4, 1, 5, 4] >>> L
>>> L.reverse() [5, 4, 4, 3, 2, 1]
>>> L
[4, 5, 1, 4, 2, 3]
11
TUPLES
12
TUPLES
➢The general syntax of a tuple is:
>>> T = (1, "a", True, 2, "b")
>>> T
(1, 'a', True, 2, 'b')
14
SETS
15
Sets
• Creating sets:
>>> S1 = {1, 2, 3}
>>> S2 = set([2, 4,
6])
>>> S1
{1, 2, 3}
>>> S2
{2, 4, 6}
>>> len(S1)
3
16
Set OPERATIONS AND METHODS
➢Union
>>> S1.union(S2) # or S1|S2
{1, 2, 3, 4, 6}
➢ Intersection
>>> S1.intersection(S2) # or S1&S2
{2}
➢ Difference
>>> S1.difference(S2) # or S1-S2
{1, 3}
17
Set OPERATIONS AND METHODS
➢Difference
>>> S2.difference(S1) # or S2-S1
{4, 6}
➢ Symmetric difference
>>> S1.symmetric_difference(S2) # or S1^S2
{1, 3, 4, 6}
18
Set OPERATIONS AND METHODS
MORE SET Methods
Method Description
add(item) Add an item to the set
clear() Removes all items from the set
copy() Returns a shallow copy of the set
discard(item) Removes the item from the set, if not found do nothing
isdisjoint(other_set) Returns True if two sets have a null intersection
issubset(other_set) Report whether another set contains this set
issuperset(other_set) Report whether this set contains another set
pop() Removes and returns a random item from the set
remove(item) Removes the item from the set, if not found raise KeyError
USAGE: your_set.method()
20
MORE SET Methods
21
DICTIONARIES
22
DICTIONARIES
23
Dictionaries: Examples
➢Creating a dictionary
>>> d = {'Ahmet':117,'Mehmet':118,'Ayse':119,'Ezgi':120}
➢ Getting/querying a value
>>> d['Ayse']
119
➢ Modifying/adding a value
>>> d['Ezgi'] = 130
>>> d
>>> d = {'Ahmet':117,'Mehmet':118,'Ayse':119,'Ezgi':130}
24
Dictionaries: Examples
➢Modifying/adding a value:
>>> d = {'Ahmet':117,'Mehmet':118,'Ayse':119,'Ezgi':120}
>>> d['Burcu'] = 121
>>> d
>>> d = {'Ahmet': 117,'Mehmet':118,'Ayse': 119,'Ezgi': 120, 'Burcu': 121}
26
DICTIONARY Methods
Method Description
clear() Removes all items from the dictionary
copy() Returns a shallow copy of the dictionary
get(key, default=None) Return the value for key if key is in the dictionary, else default
items() An object providing key-value pairs from the dictionary
keys() An object providing keys from the dictionary
pop(key) Removes the specified key and returns the corresponding value
popitem() Removes the last key and returns a key-value pair as a tuple
update(other_dict) Updates the original dictionary with the values from the other dictionary
values() An object providing values from the dictionary
USAGE: your_dictionary.method()
27
DICTIONARY Methods: Examples
➢clear()
>>> D = {"a": 1, "b": 2, "c": 3, "d": 4}
>>> D.clear()
>>> D
{}
➢get(key, default=None)
>>> D = {"a": 1, "b": 2, "c": 3, "d": 4}
>>> D.get("c")
3
28
DICTIONARY Methods: Examples
➢get(key, default=None)
>>> D = {"a": 1, "b": 2, "c": 3, "d": 4}
>>> D.get("e")
>>> D.keys()
dict_keys(['a', 'b', 'c', 'd', 'e'])
>>> D.values()
dict_values([1, 2, 3, 4, 5])
>>> D.items()
dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4)]) 31
DICTIONARY Methods: Examples
➢update(other_dict)
>>> D1 = {"a": 1, "b": 2, "c": 3, "d": 4}
>>> D2 = {"e": 5, "f": 6, "a": 100}
>>> D1.update(D2)
>>> D1
{'a': 100, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}
>>> D2
{'e': 5, 'f': 6, 'a': 100}
32
MUTABLE AND IMMUTABLE TYPES
>>> x = [1, 2, 3]
>>> y = x.copy()
>>> y.append(4)
>>> y
[1, 2, 3, 4]
>>> x
[1, 2, 3]
34
Exercise 1
a) Write a program that puts 5, 10, and "twenty" into a list. Then
remove 10 from the list.
b) Write a program that puts 5, 10, and "twenty" into a tuple. Try
removing 10 from the tuple.
c) Write a program that puts 5, 10, and "twenty" into a set. Put
"twenty", 10, and 5 into another set in a different order. Print
both of them out and notice the ordering.
d) Write a program that creates a dictionary mapping 1 to
"Monday," 2 to "Tuesday," etc. 35
Exercise 2
➢Find a way to store the data given in the table:
Student ID Name Midterm exam Final exam
101 Ahmet 70 85
102 Ezgi 85 100
103 Fatih 65 45
104 Murat 45 55
105 Hande 50 60
106 Rasim 35 75
107 Deniz 90 70
➢ From your dataset, pick a random student, assign it to a variable and print it.
➢ Print the following text for the random student that you picked.
Ahmet scored 70 from the midterm and 85 from the final. His end of term grade is calculated as 79.
Note: The weight of the midterm exam is 40% and the final exam is 60%.
36
➢ Re-run your program a few times to see different results.