IE6400 Foundations for Data Analytics
Engineering
Fall 2024
-- STUDENT VERSION --
Basic of Python - Part 2
Python Tuples
Exercise 1 Create a Tuple
In [1]: thistuple = ("apple", "banana", "cherry")
print(thistuple)
('apple', 'banana', 'cherry')
Allow Duplicates
In [2]: thistuple = ("apple", "banana", "cherry", "apple", "cherry")
print(thistuple)
('apple', 'banana', 'cherry', 'apple', 'cherry')
Tuple Length
In [3]: thistuple = ("apple", "banana", "cherry")
# --- Added the code here ---
print(len(thistuple))
# ---------------------------
Create Tuple With One Item
In [5]: # --- Added the code here ---
thistuple = ('apple',)
# ---------------------------
print(type(thistuple))
<class 'tuple'>
In [6]: #NOT a tuple
thistuple = ("apple")
print(type(thistuple))
<class 'str'>
Tuple Items - Data Types
In [7]: tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)
print(tuple1)
print(tuple2)
print(tuple3)
('apple', 'banana', 'cherry')
(1, 5, 7, 9, 3)
(True, False, False)
In [8]: tuple1 = ("abc", 34, True, 40, "male")
print(tuple1)
('abc', 34, True, 40, 'male')
Exercise 2 The tuple Constructor
In [9]: # --- Added the code here ---
thistuple = (("apple", "banana", "cherry")) # note the double round-brackets
# ---------------------------
print(thistuple)
('apple', 'banana', 'cherry')
Exercise 3 Access Tuple Items
In [10]: thistuple = ("apple", "banana", "cherry")
# --- Added the code here ---
print(thistuple[1])
# ---------------------------
banana
Negative Indexing
In [11]: thistuple = ("apple", "banana", "cherry")
# --- Added the code here ---
print(thistuple[-1])
# ---------------------------
cherry
Range of Indexes
In [12]: thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
# --- Added the code here ---
print(thistuple[2:5])
# ---------------------------
('cherry', 'orange', 'kiwi')
In [13]: thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
# --- Added the code here ---
print(thistuple[:4])
# ---------------------------
('apple', 'banana', 'cherry', 'orange')
In [14]: thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
# --- Added the code here ---
print(thistuple[2:])
# ---------------------------
('cherry', 'orange', 'kiwi', 'melon', 'mango')
Range of Negative Indexes
In [15]: thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
# --- Added the code here ---
print(thistuple[-4:-1])
# ---------------------------
('orange', 'kiwi', 'melon')
Exercise 4 Check if Item Exists
In [16]: thistuple = ("apple", "banana", "cherry")
# --- Added the code here ---
if "apple" in thistuple:
# ---------------------------
print("Yes, 'apple' is in the fruits tuple")
Yes, 'apple' is in the fruits tuple
Python - Update Tuples
Exercise 5 Change Tuple Values
In [17]: x = ("apple", "banana", "cherry")
# --- Added the code here ---
y=list(x)
y[1]='kiwi'
x=tuple(y)
# ---------------------------
print(x)
('apple', 'kiwi', 'cherry')
Exercise 6 Add Items
In [18]: thistuple = ("apple", "banana", "cherry")
# --- Added the code here ---
y=list(thistuple)
y.append('orange')
thistuple=tuple(y)
# ---------------------------
print(thistuple)
('apple', 'banana', 'cherry', 'orange')
In [19]: thistuple = ("apple", "banana", "cherry")
# --- Added the code here ---
y="orange",
thistuple+=y
# ---------------------------
print(thistuple)
('apple', 'banana', 'cherry', 'orange')
Exercise 7 Remove Items
In [20]: thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
# --- Added the code here ---
y.remove("apple")
# ---------------------------
thistuple = tuple(y)
print(thistuple)
('banana', 'cherry')
Exercise 8 Unpacking a Tuple
In [21]: fruits = ("apple", "banana", "cherry")
print(fruits)
('apple', 'banana', 'cherry')
In [22]: fruits = ("apple", "banana", "cherry")
# --- Added the code here ---
(green,yellow,red)=fruits
# ---------------------------
print(green)
print(yellow)
print(red)
apple
banana
cherry
Using Asterisk*
In [23]: fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")
# --- Added the code here ---
(green, yellow, *red) = fruits
# ---------------------------
print(green)
print(yellow)
print(red)
apple
banana
['cherry', 'strawberry', 'raspberry']
In [24]: fruits = ("apple", "mango", "papaya", "pineapple", "cherry")
# --- Added the code here ---
(green, *tropic, red) = fruits
# ---------------------------
print(green)
print(tropic)
print(red)
apple
['mango', 'papaya', 'pineapple']
cherry
Exercise 9 Loop Through a Tuple
In [25]: thistuple = ("apple", "banana", "cherry")
# --- Added the code here ---
for x in thistuple:
# ---------------------------
print(x)
apple
banana
cherry
Loop Through the Index Numbers
In [26]: thistuple = ("apple", "banana", "cherry")
# --- Added the code here ---
for i in range(len(thistuple)):
# ---------------------------
print(thistuple[i])
apple
banana
cherry
Exercise 10 Using a While Loop
In [27]: thistuple = ("apple", "banana", "cherry")
# --- Added the code here ---
i=0
while i<len(thistuple):
print(thistuple[i])
i=i+1
# ---------------------------
apple
banana
cherry
Exercise 11 Join Two Tuples
In [28]: tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
# --- Added the code here ---
tuple3 = tuple1 + tuple2
# ---------------------------
print(tuple3)
('a', 'b', 'c', 1, 2, 3)
Exercise 12 Multiply Tuples
In [29]: fruits = ("apple", "banana", "cherry")
# --- Added the code here ---
mytuple = fruits *2
# ---------------------------
print(mytuple)
('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')
Python Sets
Exercise 13 Creating a set
In [31]: # --- Added the code here ---
thisset = {"apple", "banana", "cherry"}
# ---------------------------
print(thisset)
{'apple', 'cherry', 'banana'}
Exercise 14 Duplicates Not Allowed
In [32]: # --- Added the code here ---
thisset = {"", "banana", "cherry", ""}
# ---------------------------
print(thisset)
{'', 'cherry', 'banana'}
In [35]: # --- Added the code here ---
thisset = {"apple", "banana", "cherry", True, 1, 2}
# ---------------------------
print(thisset)
{'apple', True, 2, 'cherry', 'banana'}
Exercise 15 Get the Length of a Set
In [36]: thisset = {"apple", "banana", "cherry"}
# --- Added the code here ---
print(len(thisset))
# ---------------------------
Exercise 16 Set Items - Data Types
In [37]: set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
print(set1)
print(set2)
print(set3)
{'apple', 'cherry', 'banana'}
{1, 3, 5, 7, 9}
{False, True}
In [38]: set1 = {"abc", 34, True, 40, "male"}
print(set1)
{True, 34, 'abc', 40, 'male'}
Exercise 17 Access Items
In [40]: thisset = {"apple", "banana", "cherry"}
# --- Added the code here ---
for x in thisset:
# ---------------------------
print(x)
apple
cherry
banana
In [41]: thisset = {"apple", "banana", "cherry"}
# --- Added the code here ---
print("banana" in thisset)
# ---------------------------
True
Exercise 18 Add Items
In [42]: thisset = {"apple", "banana", "cherry"}
# --- Added the code here ---
thisset.add("orange")
# ---------------------------
print(thisset)
{'apple', 'orange', 'cherry', 'banana'}
Exercise 19 Add Sets
In [43]: thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
# --- Added the code here ---
thisset.update(tropical)
# ---------------------------
print(thisset)
{'apple', 'cherry', 'pineapple', 'mango', 'papaya', 'banana'}
Exercise 20 Remove Item
In [44]: thisset = {"apple", "banana", "cherry"}
# --- Added the code here ---
thisset.remove('banana')
# ---------------------------
print(thisset)
{'apple', 'cherry'}
In [45]: thisset = {"apple", "banana", "cherry"}
# --- Added the code here ---
thisset.discard('banana')
# ---------------------------
print(thisset)
{'apple', 'cherry'}
In [46]: thisset = {"apple", "banana", "cherry"}
# --- Added the code here ---
x = thisset.pop()
# ---------------------------
print(x)
print(thisset)
apple
{'cherry', 'banana'}
In [47]: thisset = {"apple", "banana", "cherry"}
# --- Added the code here ---
thisset.clear()
# ---------------------------
print(thisset)
set()
Exercise 21 Join Two Sets
In [48]: set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
# --- Added the code here ---
set3=set1.union(set2)
# ---------------------------
print(set3)
{'b', 1, 2, 'c', 3, 'a'}
In [49]: set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
# --- Added the code here ---
set1.update(set2)
# ---------------------------
print(set1)
{'b', 1, 2, 'c', 3, 'a'}
Exercise 22 Keep ONLY the Duplicates
In [50]: x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
# --- Added the code here ---
x.intersection_update(y)
# ---------------------------
print(x)
{'apple'}
In [51]: x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
# --- Added the code here ---
z=x.intersection(y)
# ---------------------------
print(z)
{'apple'}
Exercise 23 Keep All, But NOT the Duplicates
In [52]: x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
# --- Added the code here ---
x.symmetric_difference_update(y)
# ---------------------------
print(x)
{'cherry', 'microsoft', 'banana', 'google'}
In [53]: x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
# --- Added the code here ---
z=x.symmetric_difference(y)
# ---------------------------
print(z)
{'cherry', 'microsoft', 'banana', 'google'}
In [54]: x = {"apple", "banana", "cherry", True}
y = {"google", 1, "apple", 2}
z = x.symmetric_difference(y)
print(z)
{2, 'cherry', 'google', 'banana'}
Python Dictionaries
Exercise 24 Creating a dictionary
In [58]: # --- Added the code here ---
thisdict = {
"brand": 'ford',
"model": 'mustang',
"year": 1964
}
# ---------------------------
print(thisdict)
{'brand': 'ford', 'model': 'mustang', 'year': 1964}
Exercise 25 Dictionary Items
In [59]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
# --- Added the code here ---
print(thisdict['brand'])
# ---------------------------
Ford
Exercise 26 Duplicates Not Allowed
In [60]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(thisdict)
{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}
Exercise 27 Dictionary Length
In [61]: # --- Added the code here ---
print(len(thisdict))
# ---------------------------
Exercise 28 Accessing Items
In [62]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
# --- Added the code here ---
x = thisdict['model']
# ---------------------------
print(x)
Mustang
In [63]: # --- Added the code here ---
x = thisdict.get('model')
# ---------------------------
print(x)
Mustang
Exercise 29 Get Keys
In [64]: # --- Added the code here ---
x = thisdict.keys()
# ---------------------------
print(x)
dict_keys(['brand', 'model', 'year'])
In [65]: car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
print("Before: ", x) #before the change
car["color"] = "white"
print("After: ", x) #after the change
Before: dict_keys(['brand', 'model', 'year'])
After: dict_keys(['brand', 'model', 'year', 'color'])
Exercise 30 Get Values
In [66]: # --- Added the code here ---
x = thisdict
# ---------------------------
print(x)
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
In [67]: car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print("Before: ", x) #before the change
car["year"] = 2020
print("After: ", x) #after the change
Before: dict_values(['Ford', 'Mustang', 1964])
After: dict_values(['Ford', 'Mustang', 2020])
In [68]: car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print("Before: ", x) #before the change
car["color"] = "red"
print("After: ", x) #after the change
Before: dict_values(['Ford', 'Mustang', 1964])
After: dict_values(['Ford', 'Mustang', 1964, 'red'])
Exercise 31 Get Items
In [70]: # --- Added the code here ---
x = thisdict.items()
# ---------------------------
print(x)
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
In [71]: car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print("Before: ", x) #before the change
car["year"] = 2020
print("After: ", x) #after the change
Before: dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
After: dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 2020)])
In [72]: car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print("Before: ", x) #before the change
car["color"] = "red"
print("After: ", x) #after the change
Before: dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
After: dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964), ('colo
r', 'red')])
Exercise 32 Check if Key Exists
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")
Yes, 'model' is one of the keys in the thisdict dictionary
Exercise 33 Change Values
In [75]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print("Before: ", thisdict) #before the change
# --- Added the code here ---
thisdict["year"] = 2018
# ---------------------------
print("After: ", thisdict) #after the change
Before: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
After: {'brand': 'Ford', 'model': 'Mustang', 'year': 2018}
Exercise 34 Update Dictionary
In [76]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print("Before: ", thisdict) #before the change
# --- Added the code here ---
thisdict.update({'year':2020})
# ---------------------------
print("After: ", thisdict) #after the change
Before: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
After: {'brand': 'Ford', 'model': 'Mustang', 'year': 2020}
Exercise 35 Removing Items
In [78]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print("Before: ", thisdict) #before the change
# --- Added the code here ---
thisdict.pop('model')
# ---------------------------
print("After: ", thisdict) #after the change
Before: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
After: {'brand': 'Ford', 'year': 1964}
In [79]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print("Before: ", thisdict) #before the change
# --- Added the code here ---
thisdict.popitem()
# ---------------------------
print("After: ", thisdict) #after the change
Before: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
After: {'brand': 'Ford', 'model': 'Mustang'}
In [80]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
# --- Added the code here ---
thisdict.clear()
# ---------------------------
print(thisdict)
{}
Exercise 36 Copy a Dictionary
In [81]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
# --- Added the code here ---
mydict = thisdict.copy()
# ---------------------------
print(mydict)
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
In [82]: thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
# --- Added the code here ---
mydict = dict(thisdict)
# ---------------------------
print(mydict)
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
Exercise 37 Nested Dictionaries
In [83]: myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
#print(myfamily)
myfamily
{'child1': {'name': 'Emil', 'year': 2004},
Out[83]:
'child2': {'name': 'Tobias', 'year': 2007},
'child3': {'name': 'Linus', 'year': 2011}}
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
{'child1': {'name': 'Emil', 'year': 2004},
Out[84]:
'child2': {'name': 'Tobias', 'year': 2007},
'child3': {'name': 'Linus', 'year': 2011}}
Exercise 38 Access Items in Nested Dictionaries
In [85]: # --- Added the code here ---
print(myfamily['child2']['name'])
# ---------------------------
Tobias
In [86]: # --- Added the code here ---
print(myfamily['child3']['year'])
# ---------------------------
2011
Revised Date: July 18, 2024