A data-structure is a way of arranging and organizing data in a computer program
Python has several useful data-structures built into the language
One is a list (already covered)
Another, dictionary
Many data structures allow data to be stored and retrieved using a concept called mapping
Mapping is the process of associating one value with another (a key with a value)
Lists map keys to values too!
Indices of the list are the keys
Elements in the list are the values
Keys (indices) are used to acess or modify the elements in the list
Like lists:
Unlike lists:
Example (mapping strings to integers)
Attendance Evaluate the expression on Gradescope.
.append(value)
.remove(value)
.pop(index)
in
operatorWith strings:
With lists:
And dictionary keys:
count_vowels
string
argumentdictionary
dictionary
with the count of every lowercase vowel in string
(iterate over the string with a for loop)def count_vowels(string):
counts = {"a": 0, "e": 0, "i": 0, "o": 0, "u": 0}
for i in range(len(string)):
char = string[i]
if char in counts:
counts[char] += 1
return counts
def main():
print( count_vowels("") ) # {"a": 0, "e": 0, "i": 0, "o": 0, "u": 0}
print( count_vowels("pineapple") ) # {"a": 1, "e": 2, "i": 1, "o": 0, "u": 0}
main()
{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}
{'a': 1, 'e': 2, 'i': 1, 'o': 0, 'u': 0}
count_chars
string
argumentdictionary
dictionary
with the count of every characters in string
You have 10 minutes to complete the quiz
main()
, no test casesBuilt-in functions you can use: round()
, input()
, float()
, str()
, int()
, len()
— you don’t have to use all of these