Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
148 views

Programming Fundamentals Lab 11

1. The document describes a lab on sets in Python. It introduces sets as unordered collections of unique elements and discusses how to create, add/remove elements from sets using various methods like add(), remove(), discard(), and pop(). 2. Examples are provided to initialize empty and non-empty sets, add new elements using add(), and remove elements using remove(), discard(), and pop(). The differences between remove() and discard() are also highlighted. 3. Comparisons are made between using curly braces to initialize sets versus dictionaries. Initializing empty sets and dictionaries are demonstrated.

Uploaded by

Rushan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views

Programming Fundamentals Lab 11

1. The document describes a lab on sets in Python. It introduces sets as unordered collections of unique elements and discusses how to create, add/remove elements from sets using various methods like add(), remove(), discard(), and pop(). 2. Examples are provided to initialize empty and non-empty sets, add new elements using add(), and remove elements using remove(), discard(), and pop(). The differences between remove() and discard() are also highlighted. 3. Comparisons are made between using curly braces to initialize sets versus dictionaries. Initializing empty sets and dictionaries are demonstrated.

Uploaded by

Rushan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

January 10, 2019 Lab 11 – Sets in Python.

Student Name: ___________________________ Roll No: ________________ Section: ______________

Lab Series No. 11.


Lab 11 –Sets in Python.

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.

1.1 Creating a Set


A set is created by placing all the items (elements) inside curly braces {}, separated by comma or by using
the built-in function set(). It can have any number of items and they may be of different types (integer, float,
tuple, string etc.).
But a set cannot have a mutable element, like list, set or dictionary, as its element. 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.

Student Name: ___________________________ Roll No: ________________ Section: ______________


Program 2: Write a Python program to store mix data in the form of a set.

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:

1.2 Initializing an Empty Set


Sets are a mutable collection of distinct (unique) immutable values that are unordered.
You can initialize an empty set by using set().

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.

Student Name: ___________________________ Roll No: ________________ Section: ______________


Output:

1.3 Similarity between Dictionary and Sets


Keep in mind that curly braces can only be used to initialize a set containing values. Below shows that
using curly braces without values is one of the ways to initialize a dictionary and not a set.

# initialize empty set


My_empty_set = set()

# initialize empty set


My_empty_dictionary1 = dict()
My_empty_dictionary2 = {}

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.

Student Name: ___________________________ Roll No: ________________ Section: ______________


1.4 Adding and Removing Values from Set
To add or remove values from set, let us initialize some values inside the set. Then use add method, to
add new elements in set.

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:

1. Removing Items from the Set using .remove()


You can also use remove method to remove an element from a set.

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.

Student Name: ___________________________ Roll No: ________________ Section: ______________


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')
Output:

2. Removing Items from the Set using discard ()


You can use the discard method to remove a value from a set.

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.

Student Name: ___________________________ Roll No: ________________ Section: ______________


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')
Output:

Compare Program 6 and Program 7, what is your analysis? Write your findings in the
following box:

3. Removing Items from the Set using POP()


You can also use the pop(), method to remove an item, but this method will remove the last item.
Remember that sets are unordered, so you will not know what item that gets removed. The return value of
the pop() method is the removed item.

Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 6
January 10, 2019 Lab 11 – Sets in Python.

Student Name: ___________________________ Roll No: ________________ Section: ______________


Program 8: Use pop() to remove the key and its item from the exiting 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')
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.

Student Name: ___________________________ Roll No: ________________ Section: ______________


4. Clearing Items from the Set using clear()

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:

1.5 Use Set Operations in SET data

We can use almost all the set operations on a set.

Program 10: Write a program that will operate on phonebook1 and phonebook3 for union, intersection,
difference and symmetric difference.

Code:

phonebook1 = {'123-45-67', '234-56-78', '345-67-89','123-45-67', '345-


67-89'}
phonebook3 = {'345-67-89','456-78-90'}
print("Phonebook1 is :",phonebook1)
print("Phonebook3 is :",phonebook3)
print("The Union of Phonebook1 and Phonebook3:",phonebook1 |
phonebook3)
print("The Union of Phonebook3 and Phonebook1:",phonebook3 |
phonebook1)
print("The Intersection of Phonebook1 and Phonebook3:",phonebook1 &
phonebook3)
print("The Intersection of Phonebook3 and Phonebook1:",phonebook3 &
phonebook1)
print("The Difference Between Phonebook1 and Phonebook3:",phonebook1 -
phonebook3)

Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 8
January 10, 2019 Lab 11 – Sets in Python.

Student Name: ___________________________ Roll No: ________________ Section: ______________


print("The Difference Between Phonebook3 and Phonebook1:",phonebook3 -
phonebook1)
print("The Symmetric Difference Between Phonebook1 and
Phonebook3:",phonebook1 ^ phonebook3)
print("The Symmetric Difference Between Phonebook3 and
Phonebook1:",phonebook3 ^ phonebook1)
Output:

Prepared By: Asst. Prof. Syed Faisal Ali CS-112 | Introduction to Programming Fundamentals 9
January 10, 2019 Lab 11 – Sets in Python.

Student Name: ___________________________ Roll No: ________________ Section: ______________

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.

Student Name: ___________________________ Roll No: ________________ Section: ______________


7. Solve the following problem of real world.
A camp of international students has 110 students, as shown in the diagram. The diagram will elaborate
that all the students speak some kind of a language. We need to find out how many that speak none of
them out of 110 students.
Find how many students speak
a. English and Spanish but not French?
b. Neither English, Spanish, nor French?
c. French, but neither English nor Spanish?
d. Only one of the three languages?
e. Exactly two of the three languages?

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

You might also like