- tuples
- lists
Burkay Genç, Ahmet Selman Bozkır, and Selma Dilek
19/04/2023
names = ['Ali', 'Ahmet', 'Fatma', 'Kezban'] grades = ['B', 'A+', 'A', 'A'] courses = [101, 102, 101, 201]
names = ['Ali', 'Ahmet', 'Fatma', 'Kezban'] grades = ['B', 'A+', 'A', 'A'] courses = [101, 102, 101, 201] def get_grade(student): i = names.index(student) grade = grades[i] course = courses[i] return (course, grade) print(get_grade("Fatma"))
## (101, 'A')
my_dict = {"burkay":41, "ahmet": 23, "ayşe":34} print(my_dict["ahmet"])
## 23
print(my_dict["ayşe"])
## 34
months = {1:"January", 2:"February", 3:"March", 4:"April"} married = {"burkay":True, "ahmet":False, "ayşe":False} print("The third month is", months[3])
## The third month is March
for i in married: if married[i]: print(i, "is married.") else: print(i, "is not married.")
## burkay is married. ## ahmet is not married. ## ayşe is not married.
married = {"burkay":True, "ahmet":False, "ayşe":False} print(married["hasan"])
## KeyError: 'hasan'
married["hasan"] = True print(married)
## {'burkay': True, 'ahmet': False, 'ayşe': False, 'hasan': True}
"burkay" in married
## True
"fatma" in married
## False
print(married)
## {'burkay': True, 'ahmet': False, 'ayşe': False, 'hasan': True}
del(married["burkay"]) print(married)
## {'ahmet': False, 'ayşe': False, 'hasan': True}
print(married)
## {'ahmet': False, 'ayşe': False, 'hasan': True}
married["hasan"] = False print(married)
## {'ahmet': False, 'ayşe': False, 'hasan': False}
dictionary.get(key, val)
function to safely check the value of a key
key
exists, returns dictionary[key]
val
testDict = {1:"a", 2:"b", 3:"c", 4:"d"} testDict.get(2, "does not exist")
## 'b'
testDict.get(9, "does not exist")
## 'does not exist'
dictionary[i]
does not give you the ith item in the dictionaryi
squares = {5:25, 4:16, 3:9, 2:4, 1:1} squares[1]
## 1
squares[5]
## 25
Write a function that counts the number of letters in a string.
strategy
testString = "this is a long string used for testing purposes" def letterCount(s): d = {} for c in s: if c in d: d[c] += 1 # Same as d[c] = d[c] + 1 else: d[c] = 1 return d print(letterCount(testString))
## {'t': 4, 'h': 1, 'i': 4, 's': 7, ' ': 8, 'a': 1, 'l': 1, 'o': 3, 'n': 3, 'g': 3, 'r': 3, 'u': 2, 'e': 3, 'd': 1, 'f': 1, 'p': 2}
d = letterCount(testString) d2list = list(d.items()) print(d2list)
## [('t', 4), ('h', 1), ('i', 4), ('s', 7), (' ', 8), ('a', 1), ('l', 1), ('o', 3), ('n', 3), ('g', 3), ('r', 3), ('u', 2), ('e', 3), ('d', 1), ('f', 1), ('p', 2)]
print("Value for key", d2list[3][0], "is", d2list[3][1], ".")
## Value for key s is 7 .
d = letterCount(testString) d.keys()
## dict_keys(['t', 'h', 'i', 's', ' ', 'a', 'l', 'o', 'n', 'g', 'r', 'u', 'e', 'd', 'f', 'p'])
dict_keys
object which is iterable:for key in d.keys(): print(key)
## t ## h ## i ## s ## ## a ## l ## o ## n ## g ## r ## u ## e ## d ## f ## p
dict_keys
to a list:keys_list = list(d.keys()) print(keys_list)
## ['t', 'h', 'i', 's', ' ', 'a', 'l', 'o', 'n', 'g', 'r', 'u', 'e', 'd', 'f', 'p']
d.values()
## dict_values([4, 1, 4, 7, 8, 1, 1, 3, 3, 3, 3, 2, 3, 1, 1, 2])
dict_values
object which is iterable:for value in d.values(): print(value)
## 4 ## 1 ## 4 ## 7 ## 8 ## 1 ## 1 ## 3 ## 3 ## 3 ## 3 ## 2 ## 3 ## 1 ## 1 ## 2
dict_values
to a list:values_list = list(d.values()) print(values_list)
## [4, 1, 4, 7, 8, 1, 1, 3, 3, 3, 3, 2, 3, 1, 1, 2]
int, float, string, tuple, bool
)d = {4:{1:0}, (1,3):"twelve", 'const':[3.14,2.7,8.44]}
(item, price)
.(item, price)
.def find_the_most_expensive(d): max_price = -1 max_item = None for key, value in d.items(): if value > max_price: max_price = value max_item = key return (max_item, max_price)
items = {'item1': 45.50, 'item2':35, 'item3': 41.30, 'item4':55, 'item5': 24}
items = {'item1': 45.50, 'item2':35, 'item3': 41.30, 'item4':55, 'item5': 24} print(find_the_most_expensive(items))
## ('item4', 55)
These slides are a direct adaptation of the slides used for MIT 6.0001 course present (as of February 2020) on MIT OCW web site.
Original work by:
Ana Bell, Eric Grimson, and John Guttag. 6.0001 Introduction to Computer Science and Programming in Python. Fall 2016. Massachusetts Institute of Technology: MIT OpenCourseWare. License: Creative Commons BY-NC-SA.
Adapted by and for:
Asst. Prof. Dr. Burkay Genç. MUH101 Introduction to Programming, Spring 2020. Hacettepe University, Computer Engineering Department.