Shivakumar IE6400 Lecture3 STUDENT Part 2
Shivakumar IE6400 Lecture3 STUDENT Part 2
Engineering
Fall 2024
-- STUDENT VERSION --
Python Tuples
Allow Duplicates
Tuple Length
print(type(thistuple))
<class 'tuple'>
print(tuple1)
print(tuple2)
print(tuple3)
print(tuple1)
print(thistuple)
banana
Negative Indexing
cherry
Range of Indexes
print(x)
print(thistuple)
print(thistuple)
thistuple = tuple(y)
print(thistuple)
('banana', 'cherry')
print(fruits)
print(green)
print(yellow)
print(red)
apple
banana
cherry
Using Asterisk*
print(green)
print(yellow)
print(red)
apple
banana
['cherry', 'strawberry', 'raspberry']
print(green)
print(tropic)
print(red)
apple
['mango', 'papaya', 'pineapple']
cherry
apple
banana
cherry
apple
banana
cherry
Exercise 10 Using a While Loop
apple
banana
cherry
print(tuple3)
print(mytuple)
Python Sets
print(thisset)
print(thisset)
print(thisset)
print(set1)
print(set2)
print(set3)
apple
cherry
banana
True
print(thisset)
print(thisset)
print(thisset)
{'apple', 'cherry'}
print(thisset)
{'apple', 'cherry'}
apple
{'cherry', 'banana'}
print(thisset)
set()
print(set3)
print(set1)
print(x)
{'apple'}
{'apple'}
print(x)
print(z)
z = x.symmetric_difference(y)
print(z)
Python Dictionaries
print(thisdict)
In [59]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
Ford
In [60]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(thisdict)
In [62]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(x)
Mustang
print(x)
Mustang
print(x)
In [65]: car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
car["color"] = "white"
print(x)
In [67]: car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
car["year"] = 2020
In [68]: car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print(x)
In [71]: car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
car["year"] = 2020
In [72]: car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
car["color"] = "red"
In [73]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
# --- Added the code here ---
if 'model' in thisdict:
# ---------------------------
print("Yes, 'model' is one of the keys in the thisdict dictionary")
In [75]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
In [76]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
In [78]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
In [79]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
In [80]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
{}
In [81]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(mydict)
In [82]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(mydict)
In [83]: myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
#print(myfamily)
myfamily
In [84]: child1 = {
"name" : "Emil",
"year" : 2004
}
child2 = {
"name" : "Tobias",
"year" : 2007
}
child3 = {
"name" : "Linus",
"year" : 2011
}
myfamily = {
"child1" : child1,
"child2" : child2,
"child3" : child3
}
#print(myfamily)
myfamily
Tobias
2011