15 DataStructures - Chang
15 DataStructures - Chang
Structures
CMSC 201
Built in Types
Today we will be talking about some other built in
types in python!
• Tuples
• Sets
• Dictionaries
Terms We'll Use
Here is some vocab we'll be using in today's
lecture:
def myFunc():
return 1, 2
def main():
result = myFunc()
print(result)
mySet = set(['a'])
# Add an element:
mySet.add('b')
#Remove an element:
mySet.remove('b')
myDict = {}
myDict["hello"] = 'a'
myDict[1] = 'b'
myDict[3.3] = 'c'
print(myDict["hello"])
Prints:
'a'
Dictionaries
Imagine you have a bunch of university students,
and you're storing their grades in all their classes.
myDict["hello"] = 10
^ ^
Key Value
Dictionaries
Just like in a list, if you do this:
myDict["hello"] = 10
myDict["hello"] = 11
print(myDict["hello"])
Prints:
11
Dictionaries
If we want to get just the keys, or just the values,
there's a function for that!
listOfKeys = myDict.keys()
listOfValues = myDict.values()
listOfPairs = myDict.items()