Programming Fundamentals Lab 11
Programming Fundamentals Lab 11
Lab Objectives:
1. Introduction to Sets
1. Introduction to Set
1. A set is an unordered collection of items. Every element is unique (no duplicates) and must be
immutable.
2. However, the set itself is mutable. We can add or remove items from it.
3. Sets can be used to perform mathematical set operations like union, intersection, symmetric
difference etc.
Program 1: Write a Python program to store the information of a student cell number in the form of a
set.
Code:
phonebook1 = {'123-45-67', '234-56-78', '345-67-89','123-45-67', '345-
67-89'}
print (phonebook1)
set_len = len (phonebook1)
print ("The length of phonebook1 is:", set_len)
print ("The given phonebook1 is:", phonebook1)
Output:
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 1
January 10, 2019 Lab 11 – Sets in Python.
Code:
mix_data = {'Usman Institute of Technology', 17.5, (1,2,3,4,5,6)}
print(mix_data)
set_len = len(mix_data)
print ("The length of mix data is:",set_len)
Output:
Change the above program and try to use dictionary and list to store, if you find an error report
this error and analyze why this error has generated:
Program 3: Write a program to initialize two different sets one consists the data elements for Data
Scientist and other set for Data Engineer. You can use list with explicitly type casting of set().
Code:
dataScientist = set(['Python', 'R', 'SQL', 'Git', 'Tableau', 'SAS'])
dataEngineer = set(['Python', 'Java', 'Scala', 'Git', 'SQL',
'Hadoop'])
print ("For a Data Scientist specialization you need:", dataScientist)
print ("For a Data Engineer specialization you need:", dataEngineer)
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 2
January 10, 2019 Lab 11 – Sets in Python.
Program 4: Write a program which will initialize an empty set and similarly initialize two dictionaries
with two different methods.
Code:
My_empty_set = set()
My_empty_dictionary1 = dict()
My_empty_dictionary2 = {}
print(My_empty_set)
print(My_empty_dictionary1)
print(My_empty_dictionary2)
Output:
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 3
January 10, 2019 Lab 11 – Sets in Python.
Program 5: Write a program which will have some top scoring students in a set. Now add some new
values inside a set, using add method.
Code:
My_topStudents = {'Bassam','Usman','Rafeh', 'Ahad', 'Wadood','Yusra',
'Asma'}
print('My top scoring students in Programming fundamentals are:',
My_topStudents)
print('Oh, I guess I miss one student, let me add his name too')
print('Previously I have added :'+ str(len(My_topStudents)) +'
students in my list')
My_topStudents.add('Khurram')
print('Thanks, I have remember him')
print('Now my top scoring students names are :', My_topStudents)
print('Now after adding I have :'+ str(len(My_topStudents)) +'
students in my list')
Output:
Program 6: Write a program which will remove some information from the exiting set. Use a
method remove to remove an element from a set.
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 4
January 10, 2019 Lab 11 – Sets in Python.
Program 7: Write a program which will remove some information from the exiting set. Use a method
discard to remove an element from a set.
Code:
My_topStudents = {'Bassam','Usman','Rafeh', 'Ahad', 'Wadood','Yusra',
'Asma'}
print('My top scoring students in Programming fundamentals are:',
My_topStudents)
print('Oh, I guess I miss one student, let me add his name too')
print('Previously I have added :'+ str(len(My_topStudents)) +'
students in my list')
My_topStudents.add('Khurram Khalil')
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 5
January 10, 2019 Lab 11 – Sets in Python.
Compare Program 6 and Program 7, what is your analysis? Write your findings in the
following box:
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 6
January 10, 2019 Lab 11 – Sets in Python.
Code:
My_topStudents = {'Bassam','Usman','Rafeh', 'Ahad', 'Wadood','Yusra',
'Asma'}
print('My top scoring students in Programming fundamentals are:',
My_topStudents)
print('Oh, I guess I miss one student, let me add his name too')
print('Previously I have added :'+ str(len(My_topStudents)) +'
students in my list')
My_topStudents.add('Khurram Khalil')
My_topStudents.add('Khurram Khan')
print('Thanks, I have remember him')
print('Now my top scoring students names are :', My_topStudents)
print('Now after adding I have :'+ str(len(My_topStudents)) +'
students in my list')
print('Oh, I guess I have added one student with similar name, let me
remove his name.')
My_topStudents.remove('Khurram Khalil')
print('Now my top scoring students names after removing extra name are
:', My_topStudents)
print('Now after removing I have :'+ str(len(My_topStudents)) +'
students in my list')
My_topStudents.discard('Khurram Khalil')
print('Now my top scoring students names after removing extra name are
:', My_topStudents)
print('Now after removing I have :'+ str(len(My_topStudents)) +'
students in my list')
My_topStudents.pop()
print('Now my top scoring students names after removing extra name are
:', My_topStudents)
print('Now after removing I have :'+ str(len(My_topStudents)) +'
students in my list')
Output:
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 7
January 10, 2019 Lab 11 – Sets in Python.
The clear() methods removes all the elements from the sets.
Program 9: Write a program which will clear the set from all the existing elements inside the set.
Code:
My_topStudents = {'Bassam','Usman','Rafeh', 'Ahad', 'Wadood','Yusra',
'Asma'}
print('My top scoring students in Programming fundamentals are:',
My_topStudents)
print('Lets clear this list and prepare new list for the final exams')
My_topStudents = My_topStudents.clear()
print('Now after clearing the set I have :', My_topStudents)
Output:
Program 10: Write a program that will operate on phonebook1 and phonebook3 for union, intersection,
difference and symmetric difference.
Code:
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 8
January 10, 2019 Lab 11 – Sets in Python.
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 9
January 10, 2019 Lab 11 – Sets in Python.
Programming Exercise
1. Write a program which will add your best five students name in a set. You will use a loop
to insert names in set.
2. Write a program which will remove 2 friends who left UIT.
3. Write a program which will add your best dishes and then pop one by one until the set is
empty.
4. Write a program which will store number of items in a set after each purchasing the items
will be pop from the set and compare its price at the end program will give you the total
amount of items have been sold. Also find the max amount and minimum amount of items
sold.
5. Write a program which will compare two sets, Set A and Set B. Both the sets have some students
who love to play one is hockey and other one is cricket. 10 of them play both. Now using sets find
how many of them are playing cricket only, if universal set is 40, students who play hockey are 21.
6. A pet store keeps track of the purchases of customers over a four-hour period. The store manager
classifies purchases as containing a dog product, a cat product, a fish product, or product
for a different kind of pet. She found.
a. 83 purchased a dog product
b. 101 purchased a cat product
c. 22 purchased a fish product
d. 31 purchased a dog and a cat product
e. 8 purchased a dog and a fish product
f. 10 purchased a cat and a fish product
g. 6 purchased a dog, a cat and a fish product
h. 34 purchased a product for a pet other than a dog, cat or a fish.
i. How many purchases were for a dog product only?
ii. How many purchases were for cat product only?
iii. How many purchases for a dog or a fish product?
iv. How many purchases were there in total?
Dog Cat
Fish
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 10
January 10, 2019 Lab 11 – Sets in Python.
As a programmer your task is to verify that above Venn diagram is correctly filled.
Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 11