Chapter 34 - Sets in Python
- Authors
- Ms. Samavi Salman
- Dr. Rao Muhammad Adeel Nawab
- Supporting Material
Quick Recap
- Quick Recap – Tuple in Python
In previous Chapter, I presented
- Tuple
- Definition
- Tuples are core data structures in Python that can store an ordered sequence of items
- Individual values in a tuple are called items. Tuples can store values of any data type
- Syntax
- A Tuple can be declared using two methods
- Method 1: Use ()
- Method 2: Use tuple() Built in Function
- A Tuple can be declared using two methods
- Declaring an Empty Tuple
- Declare an Empty Tuple using the following two Methods
- Method 1: Use ()
- Method 2: Use tuple() Built in Function
- Declare an Empty Tuple using the following two Methods
- Initializing a Tuple
- Initialize a Tuple using the following two Methods
- Method 1: Building a Complete Tuple
- Method 2: Building a Tuple Incrementally
- Initialize a Tuple using the following two Methods
- Definition
- Accessing Elements of a Tuple
- There are three methods to access element(s) of a Tuple
- Method 1 – Access Element(s) of a Tuple using Indexing
- Method 2 – Access Element(s) of a Tuple using Negative Indexing
- Method 3 – Access Element(s) of a Tuple using Slicing
- There are three methods to access element(s) of a Tuple
- Operations on Tuple
- Operation 1 – Creation
- Operation 2 – Insertion
- Operation 3 – Traversal
- Operation 4 – Searching
- Operation 5 – Sorting
- Operation 6 – Merging
- Operation 7 – Updation
- Operation 8 – Deletion
- Nested Tuple
- Definition
- A Nest Tuple is a Tuple of Tuple(s)
- Definition
- Access Elements of a Nested Tuple
- You can access individual items in a Nested Tuple using multiple indexes
- Add Elements in a Nested Tuple
- Add element(s) in a Nested Tuple using
- Method 1 – Add a New Element at the Start of a Nested Tuple
- Method 2 – Add a New Element at the End of a Nested Tuple
- Method 3 – Add a New Element at a Desired Location of a Nested Tuple
- Add element(s) in a Nested Tuple using
- Methods to Traverse a Tuple
- Traverse a Tuple
- Method 1: Traversing an Entire Nested Tuple
- Method 2: Traversing a Nested Tuple (Element by Element)
- Traverse a Tuple
- Update Elements of a Nested Tuple
- The value of a specific item in a Nested Tuple is unchanged even if it is converted into a List. As Tuple are immutable.
- Remove Elements from a Nested Tuple
- There are three methods to remove element(s) in a Nested Tuple
- Method 1 – Remove a New Element at the Start of a Nested Tuple
- Method 2 – Remove a New Element at the End of a Nested Tuple
- Method 3 – Remove a New Element at a Desired Location of a Nested Tuple
- There are three methods to remove element(s) in a Nested Tuple
Basics of Sets
- Sets
- Definition
- The set—an unordered and unindexed collection of unique and immutable objects that supports operations corresponding to mathematical set theory. An item appears only once in a set, no matter how many times it is added
- Purpose
- A set is an unordered collection of items.
- Every set element is unique (no duplicates) and must be immutable (cannot be changed). However, a set itself is mutable. We can add or remove items from it.
- Sets are used to store multiple items in a single variable.
- Importance
- Set operations have a variety of common uses, some more practical than mathematical.
- Filter duplicates out of other collections
- Keep track of where you’ve already been when traversing a graph or other cyclic structure.
- They are important in mathematics. Because every field of mathematics uses or refers to sets in some way. They are important for building more complex mathematical structure.
- Advantages
- Because sets cannot have multiple occurrences of the same element, it makes sets highly useful to efficiently remove duplicate values from a tuple and to perform common math operations like unions and intersections.
- The major advantage of using a set is that it has a highly optimized method for checking whether a specific element is contained in the set.
- This is based on a data structure known as a hash table.
- Disadvantage
- Since sets are unordered, we cannot access items using indexes
- Characteristics
- Set object contains one or more items, not necessarily of the same type, which are separated by a comma and enclosed in curly brackets {}.
- The elements in the set cannot be duplicates.
- The elements in the set are immutable(cannot be modified) but the set as a whole is mutable.
- There is no index attached to any element in a python set. So they do not support any indexing or slicing operation.
- The order of elements in the set is not necessarily the same as the order given at the time of assignment. Python optimizes the structure of a set for performing operations over it, as defined in mathematics.
- Examples
- Kitchen, Set of crockery
- School Bag, Set of Books
- Universe, Set of Galaxies and Stars
- Shopping Malls, Set of Food Items, Set of HouseHold etc.
- Wardrobe – Set of Clothes
- Set operations have a variety of common uses, some more practical than mathematical.
- A set is an unordered collection of items.
- Array vs. List vs. Tuple vs. Set vs. Dictionary
Collection | Order | Changeable | Duplicate | Nesting |
Array | Ordered | Changeable | Allows Duplicate Members | Allows Nesting |
List | Ordered | Changeable | Allows Duplicate Members | Allows Nesting |
Tuple | Ordered | Unchangeable | Allows Duplicate Members | Allows Nesting |
Set | Unordered and Unindexed | Changeable | No Duplicate Members | Allows Nesting |
Dictionary | Ordered | Changeable | No Duplicate Members | Allows Nesting |
Declaring a Set
- Set Declaration
- A Set can be declared using two methods
- Method 1: Use {}
- Method 2: Use set() Function
- In Sha Allah, in next Slides, I will present both methods in detail
- Syntax - Method 1 (Use {})
- The Syntax to declare a Set using {} is as follows
SET_NAME = {
,
,
,
.
.
.
,
,
}
- Syntax – Method 2 (Use set() Function)
- The Syntax to declare a Set using set() Built in Function is as follows
SET_NAME = set((
,
,
,
.
.
.
,
))
Declaring an Empty Set
- Declaring an Empty Set
- In Sha Allah, in the next Slides, I will show how to declare an Empty Set using the following two Methods
- Method 1: Use {}
- Method 2: Use set() Built in Function
- Example 1 – Declaring an Empty Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Declaring an Empty Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to declare an empty set using {} and set() Function
'''
try:
# Declare an Empty Set - Method 1: Use {}
print("---Declare an Empty Set - Method 1: Use {}---")
# Declare an Empty Set
car_companies_1 = {}
# Display Data Values of a Set
print("Data Values of Set:", car_companies_1)
# Display Data-Type of a Set using type() Function
print("Data-Type of Set:", type(car_companies_1))
# Display Length of a Set using len() Function
print("Length of Set:", len(car_companies_1))
except:
print("Error Occurred")
print("---------------------------------------------------------")
try:
# Declare an Empty Set - Method 2: Use set() Function
print("---Declare an Empty Set - Use set() Function---")
# Declare an Empty Set
car_companies_2 = set()
# Display Data Values of a Set
print("Data Values of Set:", car_companies_2)
# Display Data-Type of a Set using type() Function
print("Data-Type of Set:", type(car_companies_2))
# Display Length of a Set using len() Function
print("Length of Set:", len(car_companies_2))
except:
print("Error Occurred")
---Declare an Empty Set - Method 1: Use {}---
Data Values of Set: {}
Data-Type of Set:
Length of Set: 0
---------------------------------------------------------
---Declare an Empty Set - Use set() Function---
Data Values of Set: set()
Data-Type of Set:
Length of Set: 0
- Example 1 - Variable Initialization vs Array Initialization
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Declaring an Empty Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to declare an empty set using {} and set() Function
'''
try:
# Declare an Empty Set - Method 1: Use {}
print("---Declare an Empty Set - Method 1: Use {}---")
# Declare an Empty Set
prime_numbers_1 = {}
# Display Data Values of a Set
print("Data Values of Set:", prime_numbers_1)
# Display Data-Type of a Set using type() Function
print("Data-Type of Set:", type(prime_numbers_1))
# Display Length of a Set using len() Function
print("Length of Set:", len(prime_numbers_1))
except:
print("Error Occurred")
print("---------------------------------------------------------")
try:
# Declare an Empty Set - Method 2: Use set() Function
print("---Declare an Empty Set - Use set() Function---")
# Declare an Empty Set
prime_numbers_2 = set()
# Display Data Values of a Set
print("Data Values of Set:", prime_numbers_2)
# Display Data-Type of a Set using type() Function
print("Data-Type of Set:", type(prime_numbers_2))
# Display Length of a Set using len() Function
print("Length of Set:", len(prime_numbers_2))
except:
print("Error Occurred")
---Declare an Empty Set - Method 1: Use {}---
Data Values of Set: {}
Data-Type of Set:
Length of Set: 0
---------------------------------------------------------
---Declare an Empty Set - Use set() Function---
Data Values of Set: set()
Data-Type of Set:
Length of Set: 0
- Example 3 - Variable Initialization vs Array Initialization
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Declaring an Empty Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to declare an empty set using {} and set() Function
'''
try:
# Declare an Empty Set - Method 1: Use {}
print("---Declare an Empty Set - Method 1: Use {}---")
# Declare an Empty Set
vowels_1 = {}
# Display Data Values of a Set
print("Data Values of Set:", vowels_1)
# Display Data-Type of a Set using type() Function
print("Data-Type of Set:", type(vowels_1))
# Display Length of a Set using len() Function
print("Length of Set:", len(vowels_1))
except:
print("Error Occurred")
print("---------------------------------------------------------")
try:
# Declare an Empty Set - Method 2: Use set() Function
print("---Declare an Empty Set - Use set() Function---")
# Declare an Empty Set
vowels_2= set()
# Display Data Values of a Set
print("Data Values of Set:", vowels_2)
# Display Data-Type of a Set using type() Function
print("Data-Type of Set:", type(vowels_2))
# Display Length of a Set using len() Function
print("Length of Set:", len(vowels_2))
except:
print("Error Occurred")
---Declare an Empty Set - Method 1: Use {}---
Data Values of Set: {}
Data-Type of Set:
Length of Set: 0
---------------------------------------------------------
---Declare an Empty Set - Use set() Function---
Data Values of Set: set()
Data-Type of Set:
Length of Set: 0
Initializing a Set
- Initializing a Set
- In Sha Allah, in the next Slides, I will show how to initialize a Set using the following two Methods
- Method 1: Building a Complete Set
- Method 2: Building a Set Incrementally
Initializing a Set - Method 1: Building a Complete Set
- Initializing a Set - Method 1: Building a Complete Set
- In Sha Allah, I will build a complete set using two Approaches
- Approach 1: Use {}
- Approach 2: Use set() Built in Function
- Example 1 – Initializing a Set - Method 1: Building a Complete Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to initialize a complete set and display its content (data values) on the Output Screen
'''
try:
# Declare an Empty Set - Method 1: Use {}
print("---Declare an Empty Set - Method 1: Use {}---")
# Declare an Empty Set
car_companies_1 = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
# Display Data Values of a Set
print("Data Values of Set:", car_companies_1)
# Display Data-Type of a Set using type() Function
print("Data-Type of Set:", type(car_companies_1))
# Display Length of a Set using len() Function
print("Length of Set:", len(car_companies_1))
except:
print("Error Occurred")
print("---------------------------------------------------------")
try:
# Declare an Empty Set - Method 2: Use set() Function
print("---Declare an Empty Set - Use set() Function---")
# Declare an Empty Set
car_companies_2 = set(('Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'))
# Display Data Values of a Set
print("Data Values of Set:", car_companies_2)
# Display Data-Type of a Set using type() Function
print("Data-Type of Set:", type(car_companies_2))
# Display Length of a Set using len() Function
print("Length of Set:", len(car_companies_2))
except:
print("Error Occurred")
---Declare an Empty Set - Method 1: Use {}---
Data Values of Set: {'Toyota', 'Kia', 'Hyundai', 'Suzuki', 'Honda', 'Changan'}
Data-Type of Set:
Length of Set: 6
---------------------------------------------------------
---Declare an Empty Set - Use set() Function---
Data Values of Set: {'Toyota', 'Kia', 'Hyundai', 'Suzuki', 'Honda', 'Changan'}
Data-Type of Set:
Length of Set: 6
- Example 2 – Initializing a Set - Method 1: Building a Complete Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to initialize a complete set and display its content (data values) on the Output Screen
'''
try:
# Declare an Empty Set - Method 1: Use {}
print("---Declare an Empty Set - Method 1: Use {}---")
# Declare an Empty Set
prime_numbers_1 = {2, 3, 5, 7, 11, 13, 17, 19}
# Display Data Values of a Set
print("Data Values of Set:", prime_numbers_1)
# Display Data-Type of a Set using type() Function
print("Data-Type of Set:", type(prime_numbers_1))
# Display Length of a Set using len() Function
print("Length of Set:", len(prime_numbers_1))
except:
print("Error Occurred")
print("---------------------------------------------------------")
try:
# Declare an Empty Set - Method 2: Use set() Function
print("---Declare an Empty Set - Use set() Function---")
# Declare an Empty Set
prime_numbers_2 = set((2, 3, 5, 7, 11, 13, 17, 19))
# Display Data Values of a Set
print("Data Values of Set:", prime_numbers_2)
# Display Data-Type of a Set using type() Function
print("Data-Type of Set:", type(prime_numbers_2))
# Display Length of a Set using len() Function
print("Length of Set:", len(prime_numbers_2))
except:
print("Error Occurred")
---Declare an Empty Set - Method 1: Use {}---
Data Values of Set: {2, 3, 5, 7, 11, 13, 17, 19}
Data-Type of Set:
Length of Set: 8
---------------------------------------------------------
---Declare an Empty Set - Use set() Function---
Data Values of Set: {2, 3, 5, 7, 11, 13, 17, 19}
Data-Type of Set:
Length of Set: 8
- Example 3 – Initializing a Set - Method 1: Building a Complete Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to initialize a complete set and display its content (data values) on the Output Screen
'''
try:
# Declare an Empty Set - Method 1: Use {}
print("---Declare an Empty Set - Method 1: Use {}---")
# Declare an Empty Set
vowels_1 = { 'a', 'e', 'i', 'o', 'u'}
# Display Data Values of a Set
print("Data Values of Set:", vowels_1)
# Display Data-Type of a Set using type() Function
print("Data-Type of Set:", type(vowels_1))
# Display Length of a Set using len() Function
print("Length of Set:", len(vowels_1))
except:
print("Error Occurred")
print("---------------------------------------------------------")
try:
# Declare an Empty Set - Method 2: Use set() Function
print("---Declare an Empty Set - Use set() Function---")
# Declare an Empty Set
vowels_2= set(('a', 'e', 'i', 'o', 'u'))
# Display Data Values of a Set
print("Data Values of Set:", vowels_2)
# Display Data-Type of a Set using type() Function
print("Data-Type of Set:", type(vowels_2))
# Display Length of a Set using len() Function
print("Length of Set:", len(vowels_2))
except:
print("Error Occurred")
---Declare an Empty Set - Method 1: Use {}---
Data Values of Set: {'u', 'o', 'e', 'i', 'a'}
Data-Type of Set:
Length of Set: 5
---------------------------------------------------------
---Declare an Empty Set - Use set() Function---
Data Values of Set: {'u', 'o', 'e', 'i', 'a'}
Data-Type of Set:
Length of Set: 5
Initializing a Set - Method 2: Building a Set Incrementally
- Initializing a Set - Method 2: Building a Set Incrementally
- In Sha Allah, in this Example, we will show how to Initialize a Set using following approach:
- Approach 1: Use add() Built-in Function
- Example 1 - Initializing a Set – Method 2: Building a Set Incrementally
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to initialize a set incrementally and display its content (data values) on the Output Screen
'''
try:
# Set Initialization using {}
print("---Initialize a Set - Use {}---")
car_companies = {''}
# Before Adding Elements in Set
print("Before Adding Elements in Set \n", car_companies)
# Building a Set Incrementally
car_companies.add('Changan')
car_companies.add('Suzuki')
car_companies.add('Honda')
car_companies.add('Toyota')
car_companies.add('Hyundai')
car_companies.add('Kia')
# After Adding Elements in Set
print("After Adding Elements in Set \n", car_companies)
except:
print("Error Occurred")
---Initialize a Set - Use {}---
Before Adding Elements in Set
{''}
After Adding Elements in Set
{'', 'Hyundai', 'Changan', 'Honda', 'Toyota', 'Suzuki', 'Kia'}
- Example 2 - Initializing a Set – Method 2: Building a Set Incrementally
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to initialize a set incrementally and display its content (data values) on the Output Screen
'''
try:
# Set Initialization using {}
print("---Initialize a Set - Use {}---")
prime_numbers = {''}
# Before Adding Elements in Set
print("Before Adding Elements in Set \n", prime_numbers)
# Building a Set Incrementally
prime_numbers.add(2)
prime_numbers.add(3)
prime_numbers.add(5)
prime_numbers.add(7)
prime_numbers.add(11)
prime_numbers.add(13)
prime_numbers.add(17)
prime_numbers.add(19)
# After Adding Elements in Set
print("After Adding Elements in Set \n", prime_numbers)
except:
print("Error Occurred")
---Initialize a Set - Use {}---
Before Adding Elements in Set
{''}
After Adding Elements in Set
{'', 2, 3, 5, 7, 11, 13, 17, 19}
- Example 3 - Initializing a Set – Method 2: Building a Set Incrementally
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to initialize a set incrementally and display its content (data values) on the Output Screen
'''
try:
# Set Initialization using {}
print("---Initialize a Set - Use {}---")
vowels = {''}
# Before Adding Elements in Set
print("Before Adding Elements in Set \n", vowels)
# Building a Set Incrementally
vowels.add('a')
vowels.add('e')
vowels.add('i')
vowels.add('o')
vowels.add('u')
# After Adding Elements in Set
print("After Adding Elements in Set \n", vowels)
except:
print("Error Occurred")
---Initialize a Set - Use {}---
Before Adding Elements in Set
{''}
After Adding Elements in Set
{'', 'o', 'a', 'u', 'e', 'i'}
TODO and Your Turn
Todo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
- Task
- Declare and Initialize a Set called ghazwa_in_Islam using the {} and set() Function?
- Note
- The elements of ghazwa_in_Islam are as follows
- Badr
- Uhud
- Khandaq
- Banu Qurayza
- Banu l-Mustaliq
- Khaybar
- Conquest of Mecca
- Hunayn
- Al-Ta’if
- The elements of ghazwa_in_Islam are as follows
Your Turn Tasks
Your Turn Task 1
- Task
- Declare and Initialize a Set called ghazwa_in_Islam using the {} and set() Function?
Accessing Elements of a Set
- Access Element(s) of a Set
- We cannot access individual values in a set. We can only access all the elements together by looping through the set
- Example 1 - Access Element(s) of a Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of Sets
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to access element(s) of a Set
'''
try:
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
# Access Element(s) of a Set by Iterating through Elements Altogether
print("---Access Element(s) of a Set by Iterating through Elements---")
for elements in car_companies:
print(elements)
except:
print("Error Occurred")
---Access Element(s) of a Set by Iterating through Elements---
Suzuki
Honda
Changan
Toyota
Kia
Hyundai
- Example 2 - Access Element(s) of a Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of Sets
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to access element(s) of a Set
'''
try:
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
# Access Element(s) of a Set by Iterating through Elements Altogether
print("---Access Element(s) of a Set by Iterating through Elements---")
for elements in prime_numbers:
print(elements)
except:
print("Error Occurred")
---Access Element(s) of a Set by Iterating through Elements---
2
3
5
7
11
13
17
19
- Example 3 - Access Element(s) of a Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of Sets
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to access element(s) of a Set
'''
try:
vowels = { 'a','e','i','o','u'}
# Access Element(s) of a Set by Iterating through Elements Altogether
print("---Access Element(s) of a Set by Iterating through Elements---")
for elements in vowels:
print(elements)
except:
print("Error Occurred")
---Access Element(s) of a Set by Iterating through Elements---
e
a
u
i
o
TODO and Your Turn
Todo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
- Task
- Consider the following two Sets and answer the questions given below
- prophets = {‘Hazrat Muhammad (PBUH)’, ‘Hazrat Ibrahim (A.S.)’, ‘Hazrat Mosa (A.S.)’, ‘Hazrat Isa (A.S.)’, ‘Hazrat Adam (A.S.)’}
- ages = {25, 30, 15, 20, 22, 33}
- Questions
- Access Elements of the prophets and ages by Iterating through the Elements of a Set
- Consider the following two Sets and answer the questions given below
Your Turn Tasks
Your Turn Task 1
- Task
- Consider two Sets (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Access Elements of the prophets and ages by Iterating through the Elements of a Set
Operations on Sets
Operation 1 – Creation
- Methods to Create a Sets
- I have already presented methods to create a Set in the previous Section i.e.,
- Sets Initialization
Operation 2 – Insertion
- Methods to Add Element(s) in a Set
- Sets are unordered
- Unordered means when we display the elements of a set, it will come out in a random order
- In Sha Allah, in next Slides, I will present a method to add element(s) in a Set
- Method 1 – Add New Element(s) in a Set using add() Function
- add() Function
- Function Name
- add()
- Syntax
set.add(obj)
- Purpose
- The main purpose of add() Function is to
- add data value(s) in a Set
- The main purpose of add() Function is to
Add a New Element in a Set using add() Function
- Example 1 - Add a New Element in a Set using add() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element in a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to add a new element in a set using add() function
'''
try:
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai'}
# Before Adding Elements in Set
print("Before Adding Elements in Set \n", car_companies)
# Add a New Element using add() Function
car_companies.add('Kia')
# After Adding Elements in Set
print("After Adding Elements in Set \n", car_companies)
except:
print("Error Occurred")
Before Adding Elements in Set
{'Suzuki', 'Hyundai', 'Changan', 'Honda', 'Toyota'}
After Adding Elements in Set
{'Suzuki', 'Hyundai', 'Changan', 'Honda', 'Kia', 'Toyota'}
- Example 2 - Add a New Element in a Set using add() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element in a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to add a new element in a set using add() function
'''
try:
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
# Before Adding Elements in Set
print("Before Adding Elements in Set \n", prime_numbers)
# Add a New Element using add() Function
prime_numbers.add(21)
# After Adding Elements in Set
print("After Adding Elements in Set \n", prime_numbers)
except:
print("Error Occurred")
Before Adding Elements in Set
{2, 3, 5, 7, 11, 13, 17, 19}
After Adding Elements in Set
{2, 3, 5, 7, 11, 13, 17, 19, 21}
- Example 3 - Add a New Element in a Set using add() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element in a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to add a new element in a set using add() function
'''
try:
vowels = { 'a','e','o','u'}
# Before Adding Elements in Set
print("Before Adding Elements in Set \n", vowels)
# Add a New Element using add() Function
vowels.add('i')
# After Adding Elements in Set
print("After Adding Elements in Set \n", vowels)
except:
print("Error Occurred")
Before Adding Elements in Set
{'u', 'a', 'e', 'o'}
After Adding Elements in Set
{'e', 'a', 'o', 'u', 'i'}
TODO and Your Turn
Todo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
- Task
- Consider the following two Sets and answer the questions given below
- prophets = {‘Hazrat Muhammad (PBUH)’, ‘Hazrat Ibrahim (A.S.)’, ‘Hazrat Mosa (A.S.)’, ‘Hazrat Isa (A.S.)’, ‘Hazrat Adam (A.S.)’}
- ages = {25, 30, 15, 20, 22, 33}
- Questions
- Insert Elements to prophets and ages Sets using add() Function (as shown in this Chapter)
- Consider the following two Sets and answer the questions given below
Your Turn Tasks
Your Turn Task 1
- Task
- Consider two Sets (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Insert Elements to prophets and ages Sets using add() Function (as shown in this Chapter)
Operation 3 – Traverse
- Methods to Traverse a Set
- I have already presented methods to traverse a Set in the previous Section i.e.,
- Access Element(s) of a Set
Operation 4 – Searching
- Methods to Search Element(s) from a Set
- In Sha Allah, in next Slides, I will present a method to search element(s) from a Set
- Method 1: Search a Specific Element from a Set
- Method 2: Search Multiple Elements from a Set
Method 1: Search a Specific Element from a Set
- Example 1 - Search a Specific Element from a Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search a Specific Element from a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to search a specific element from a set
'''
# Approach I – Using in Operator to Search Element in a Set
try:
# Initialize a Set
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
print("---Approach I – Using in Operator to Search Element in a Set---")
element = 'Honda'
# Using in operator to Search Element in a Set
if element in car_companies:
print(element + " is in Set")
else:
print(element + " is not in Set")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Element in a Set
try:
# Initialize a Set
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
print("---Approach II – Using not in Operator to Search Element in a Set---")
element = 'Toyota'
# Using not in operator to Search Element in a Set
if element not in car_companies:
print(element + " is not in Set")
else:
print(element + " is in Set")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Element in a Set---
Honda is in Set
-------------------------------------------
---Approach II – Using not in Operator to Search Element in a Set---
Toyota is in Set
- Example 2 - Search a Specific Element from a Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search a Specific Element from a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to search a specific element from a set
'''
# Approach I – Using in Operator to Search Element in a Set
try:
# Initialize a Set
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
print("---Approach I – Using in Operator to Search Element in a Set---")
element = "100"
# Using in operator to Search Element in a Set
if element in prime_numbers:
print(element + " is in Set")
else:
print(element + " is not in Set")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Element in a Set
try:
# Initialize a Set
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
print("---Approach II – Using not in Operator to Search Element in a Set---")
element = "60"
# Using not in operator to Search Element in a Set
if element not in prime_numbers:
print(element + " is not in Set")
else:
print(element + " is in Set")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Element in a Set---
100 is not in Set
-------------------------------------------
---Approach II – Using not in Operator to Search Element in a Set---
60 is not in Set
- Example 3 - Search a Specific Element from a Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search a Specific Element from a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to search a specific element from a set
'''
# Approach I – Using in Operator to Search Element in a Set
try:
# Initialize a Set
vowels = {'a','e','i','o','u'}
print("---Approach I – Using in Operator to Search Element in a Set---")
element = 'u'
# Using in operator to Search Element in a Set
if element in vowels:
print(element + " is in Set")
else:
print(element + " is not in Set")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Element in a Set
try:
# Initialize a Set
vowels = {'a','e','i','o','u'}
print("---Approach II – Using not in Operator to Search Element in a Set---")
element = 's'
# Using not in operator to Search Element in a Set
if element not in vowels:
print(element + " is not in Set")
else:
print(element + " is in Set")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Element in a Set---
u is in Set
-------------------------------------------
---Approach II – Using not in Operator to Search Element in a Set---
s is not in Set
Method 2: Search Multiple Elements from a Set
- Example 1 - Search Multiple Elements from a Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search Multiple Elements from a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to search multiple elements from a set
'''
# Approach I – Using in Operator to search multiple elements from a set
try:
# Initialize a Set
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
print("---Approach I – Using in Operator to Search Elements in a Set---")
# Using in operator to Search Elements in a Set
if 'Honda' in car_companies or 'Toyota' in car_companies:
print("Car is in Set")
else:
print("Car is not in Set")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Multiple Elements from a Set
try:
# Initialize a Set
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
print("---Approach II – Using not in Operator to Search Elements in a Set---")
# Using not in operator to Search Elements in a Set
if 'Yamaha' not in car_companies and 'Toyota' not in car_companies:
print("Car is not in Set")
else:
print("Car is in Set")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Elements in a Set---
Car is in Set
-------------------------------------------
---Approach II – Using not in Operator to Search Elements in a Set---
Car is in Set
- Example 2 - Search Multiple Elements from a Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_nam = Search Multiple Elements from a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to search multiple elements from a set
'''
# Approach I – Using in Operator to search multiple elements from a set
try:
# Initialize a Set
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
print("---Approach I – Using in Operator to Search Elements in a Set---")
# Using in operator to Search Elements in a Set
if "21" in prime_numbers or "65" in prime_numbers:
print("Number is in Set")
else:
print("Number is not in Set")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Multiple Elements from a Set
try:
# Initialize a Set
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
print("---Approach II – Using not in Operator to Search Elements in a Set---")
# Using not in operator to Search Elements in a Set
if "45" in prime_numbers or "13" in prime_numbers:
print("Number is not in Set")
else:
print("Number is in Set")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Elements in a Set---
Number is not in Set
-------------------------------------------
---Approach II – Using not in Operator to Search Elements in a Set---
Number is in Set
- Example 3 - Search Multiple Elements from a Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search Multiple Elements from a Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to search multiple elements from a set
'''
# Approach I – Using in Operator to search multiple elements from a set
try:
# Initialize a Set
vowels = {'a','e','i','o','u'}
print("---Approach I – Using in Operator to Search Elements in a Set---")
# Using in operator to Search Elements in a Set
if 'a' in vowels or 't' in vowels:
print("alphabet is in Set")
else:
print("alphabet is not in Set")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Multiple Elements from a Set
try:
# Initialize a Set
vowels = {'a','e','i','o','u'}
print("---Approach II – Using not in Operator to Search Elements in a Set---")
# Using not in operator to Search Elements in a Set
if 'b' in vowels or 'm' in vowels:
print("alphabet is in Set")
else:
print("alphabet is not in Set")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Elements in a Set---
alphabet is in Set
-------------------------------------------
---Approach II – Using not in Operator to Search Elements in a Set---
alphabet is not in Set
TODO and Your Turn
Todo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
- Task
- Consider the following two Sets and answer the questions given below
- prophets = {‘Hazrat Muhammad (PBUH)’, ‘Hazrat Ibrahim (A.S.)’, ‘Hazrat Mosa (A.S.)’, ‘Hazrat Isa (A.S.)’, ‘Hazrat Adam (A.S.)’}
- ages = {25, 30, 15, 20, 22, 33}
- Consider the following two Sets and answer the questions given below
- Questions
- Search Elements from Sets using following approaches (as shown in this Chapter)
- Method 1: Search a Specific Element from a Set
- Method 2: Search Multiple Elements from a Set
- Search Elements from Sets using following approaches (as shown in this Chapter)
Your Turn Tasks
Your Turn Task 1
- Task
- Consider two Sets (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Search Elements from Sets using following approaches (as shown in this Chapter)
- Method 1: Search a Specific Element from a Set
- Method 2: Search Multiple Elements from a Set
- Search Elements from Sets using following approaches (as shown in this Chapter)
Operation 5 – Sorting
- Methods to Sorting a Set
- In Sha Allah, in next Slides, I will present three methods of sorting a Set
- Method 1: Sort a Set Alphabetically
- Method 2: Sort a Set in Ascending Order
- Method 3: Sort a Set in Descending Order
- sorted() Function
- Function Name
- sorted()
- Syntax
set.sorted([func])
OR
set.sort(key, reverse)
- Purpose
- The main purpose of sorted() function is to
- Alphabetically sort a Set
- The main purpose of sorted() function is to
Sort a Set Alphabetically
- Example 1 – Sort a Set Alphabetically – Using sorted() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Set Alphabetically
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to sort a Set alphabetically
'''
try:
# Initialize a Set
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
print('---Original Set before using sorted() Function---')
print(car_companies)
# Using sorted() Function to sort a Set Alphabetically
sorted_order = sorted(car_companies)
print('---New Set after using sorted() Function---')
print(sorted_order)
except:
print('Error Occurred')
---Original Set before using sorted() Function---
{'Suzuki', 'Hyundai', 'Honda', 'Changan', 'Toyota', 'Kia'}
---New Set after using sorted() Function---
['Changan', 'Honda', 'Hyundai', 'Kia', 'Suzuki', 'Toyota']
- Example 2 – Sort a Set Alphabetically – Using sorted() Function
''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Set Alphabetically
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to sort a Set alphabetically
'''
try:
# Initialize a Set
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
print('---Original Set before using sorted() Function---')
print(prime_numbers)
# Using sorted() Function to sort a Set Alphabetically
sorted_order = sorted(prime_numbers)
print('---New Set after using sorted() Function---')
print(sorted_order)
except:
print('Error Occurred')
---Original Set before using sorted() Function---
{2, 3, 5, 7, 11, 13, 17, 19}
---New Set after using sorted() Function---
[2, 3, 5, 7, 11, 13, 17, 19]
- Example 3 – Sort a Set Alphabetically – Using sorted() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Set Alphabetically
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to sort a Set alphabetically
'''
try:
# Initialize a Set
vowels = { 'a','e','i','o','u'}
print('---Original Set before using sorted() Function---')
print(vowels)
# Using sorted() Function to sort a Set Alphabetically
sorted_order = sorted(vowels)
print('---New Set after using sorted() Function---')
print(sorted_order)
except:
print('Error Occurred')
---Original Set before using sorted() Function---
{'a', 'u', 'i', 'o', 'e'}
---New Set after using sorted() Function---
['a', 'e', 'i', 'o', 'u']
Sort a Set in Ascending Order
- Example 1 – Sort a Set in Ascending Order – Using sorted() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Set in Ascending Order
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to sort a Set in ascending order
'''
try:
# Initialize a Set
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
print('---Original Set before using sorted() Function---')
print(car_companies)
# Using sorted() Function to sort a Set in Ascending Order
sorted_order = sorted(car_companies, reverse = False)
print('---New Set after using sorted() Function---')
print(sorted_order)
except:
print('Error Occurred')
---Original Set before using sorted() Function---
{'Kia', 'Changan', 'Suzuki', 'Hyundai', 'Honda', 'Toyota'}
---New Set after using sorted() Function---
['Changan', 'Honda', 'Hyundai', 'Kia', 'Suzuki', 'Toyota']
- Example 2 – Sort a Set in Ascending Order – Using sorted() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Set in Ascending Order
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to sort a Set in ascending order
'''
try:
# Initialize a Set
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
print('---Original Set before using sorted() Function---')
print(prime_numbers)
# Using sorted() Function to sort a Set in Ascending Order
sorted_order = sorted(prime_numbers, reverse = False)
print('---New Set after using sorted() Function---')
print(sorted_order)
except:
print('Error Occurred')
---Original Set before using sorted() Function---
{2, 3, 5, 7, 11, 13, 17, 19}
---New Set after using sorted() Function---
[2, 3, 5, 7, 11, 13, 17, 19]
- Example 3 – Sort a Set in Ascending Order – Using sorted() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Set in Ascending Order
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to sort a Set in ascending order
'''
try:
# Initialize a Set
vowels = { 'a','e','i','o','u'}
print('---Original Set before using sorted() Function---')
print(vowels)
# Using sorted() Function to sort a Set in Ascending Order
sorted_order = sorted(vowels, reverse = False)
print('---New Set after using sorted() Function---')
print(sorted_order)
except:
print('Error Occurred')
---Original Set before using sorted() Function---
{'o', 'e', 'u', 'i', 'a'}
---New Set after using sorted() Function---
['a', 'e', 'i', 'o', 'u']
Sort a Set in Descending Order
- Example 1 – Sort a Set in Descending Order – Using sorted() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Set in Descending Order
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to sort a Set in descending order
'''
try:
# Initialize a Set
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
print('---Original Set before using sorted() Function---')
print(car_companies)
# Using sorted() Function to sort a Set in Descending Order
sorted_order = sorted(car_companies, reverse = True)
print('---New Set after using sorted() Function---')
print(sorted_order)
except:
print('Error Occurred')
---Original Set before using sorted() Function---
{'Kia', 'Hyundai', 'Suzuki', 'Changan', 'Toyota', 'Honda'}
---New Set after using sorted() Function---
['Toyota', 'Suzuki', 'Kia', 'Hyundai', 'Honda', 'Changan']
- Example 2 – Sort a Set in Descending Order – Using sorted() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Set in Descending Order
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to sort a Set in descending order
'''
try:
# Initialize a Set
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
print('---Original Set before using sorted() Function---')
print(prime_numbers)
# Using sorted() Function to sort a Set in Descending Order
sorted_order = sorted(prime_numbers, reverse = True)
print('---New Set after using sorted() Function---')
print(sorted_order)
except:
print('Error Occurred')
---Original Set before using sorted() Function---
{2, 3, 5, 7, 11, 13, 17, 19}
---New Set after using sorted() Function---
[19, 17, 13, 11, 7, 5, 3, 2]
- Example 3 – Sort a Set in Descending Order – Using sorted() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Set in Descending Order
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to sort a Set in descending order
'''
try:
# Initialize a Set
vowels = { 'a','e','i','o','u'}
print('---Original Set before using sorted() Function---')
print(vowels)
# Using sorted() Function to sort a Set in Descending Order
sorted_order = sorted(vowels, reverse = True)
print('---New Set after using sorted() Function---')
print(sorted_order)
except:
print('Error Occurred')
---Original Set before using sorted() Function---
{'i', 'o', 'u', 'e', 'a'}
---New Set after using sorted() Function---
['u', 'o', 'i', 'e', 'a']
TODO and Your Turn
Todo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
- Task
- Consider the following two Sets and answer the questions given below
- prophets = {‘Hazrat Muhammad (PBUH)’, ‘Hazrat Ibrahim (A.S.)’, ‘Hazrat Mosa (A.S.)’, ‘Hazrat Isa (A.S.)’, ‘Hazrat Adam (A.S.)’}
- ages = {25, 30, 15, 20, 22, 33}
- Questions
- Sort Elements from Sets using following approaches (as shown in this Chapter)
- Method 1: Sort a Set Alphabetically
- Method 2: Sort a Set in Ascending Order
- Method 3: Sort a Set in Descending Order
- Sort Elements from Sets using following approaches (as shown in this Chapter)
- Consider the following two Sets and answer the questions given below
Your Turn Tasks
Your Turn Task 1
- Task
- Consider two Sets (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Sort Elements from Sets using following approaches (as shown in this Chapter)
- Method 1: Sort a Set Alphabetically
- Method 2: Sort a Set in Ascending Order
- Method 3: Sort a Set in Descending Order
- Sort Elements from Sets using following approaches (as shown in this Chapter)
Operation 6 – Merge
- Methods of Merging Sets
- In Sha Allah, in next Slides, I will present two methods to merge / join Sets
- Method 1: Join Sets using update() Function
- Method 2: Join Sets using union() Function
- update() Function
- Function Name
- update()
- Syntax
set.update([func])
- Purpose
- The main purpose of update() function is to
- Update / Join Data Value(s) in a Sets
- The main purpose of update() function is to
- union() Function
- Function Name
- union()
- Syntax
set.union([func])
- Purpose
- The main purpose of union() function is to
- Join Data Value(s) in a Sets
- The main purpose of union() function is to
Method 1: Join Sets using update() Function
- Example 1 – Join Sets using update() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Join Sets using update() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to join sets using update() function
'''
try:
# Initialize Sets
set_even_numbers = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
set_odd_numbers = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
print('---Original Set before Merging---')
print(set_even_numbers)
print(set_odd_numbers)
print('---New Set after Merging---')
# Using update() Function to Concatenate Sets
set_even_numbers.update(set_odd_numbers)
print(set_even_numbers)
except:
print("Error Occurred")
---Original Set before Merging---
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
---New Set after Merging---
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
Method 2: Join Sets using union() Function
- Example 1 – Join Sets using union() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Join Sets using union() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to join sets using union() function
'''
try:
# Initialize Sets
set_even_numbers = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
set_odd_numbers = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
print('---Original Set before Merging---')
print(set_even_numbers)
print(set_odd_numbers)
print('---New Set after Merging---')
# Using union() Function to Concatenate Sets
natural_numbers = set_even_numbers.union(set_odd_numbers)
print(natural_numbers)
except:
print("Error Occurred")
---Original Set before Merging---
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
---New Set after Merging---
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
TODO and Your Turn
Todo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
- Task
- Consider the following two Sets and answer the questions given below
- prophets = {‘Hazrat Muhammad (PBUH)’, ‘Hazrat Ibrahim (A.S.)’, ‘Hazrat Mosa (A.S.)’, ‘Hazrat Isa (A.S.)’, ‘Hazrat Adam (A.S.)’}
- ages = {25, 30, 15, 20, 22, 33}
- Consider the following two Sets and answer the questions given below
- Questions
- Merge Elements from Sets using following approaches (as shown in this Chapter)
- Method 1: Join Sets using update() Function
- Method 2: Join Sets using union() Function
- Merge Elements from Sets using following approaches (as shown in this Chapter)
Your Turn Tasks
Your Turn Task 1
- Task
- Consider two Sets (similar to the ones given in TODO Task 1) and answer the questions given below
- Question
- Merge Elements from Sets using following approaches (as shown in this Chapter)
- Method 1: Join Sets using update() Function
- Method 2: Join Sets using union() Function
- Merge Elements from Sets using following approaches (as shown in this Chapter)
Operation 7 – Updation
- Methods of Updating a Set
- In Sha Allah, in next Slides, I will present a method to update a Set
- Method 1: Update Set using update() Function
- Example 1 - Update Set using update() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Update Set using update() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to update set using update() function
'''
# Approach I – Update two Sets using update() Function
try:
print('---Approach I - Update two Sets using update() Function---')
# Initialize Sets
set_even_numbers = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
set_odd_numbers = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
print('---Original Sets before Updating---')
print(set_even_numbers)
print(set_odd_numbers)
# Update Sets using update() Function
print('---New Set after Updating---')
set_even_numbers.update(set_odd_numbers)
print(set_even_numbers)
except:
print("Error Occurred")
print("----------------------------------------------------------")
# Approach II – Update more than two Sets using update() Function
try:
print('---Approach II - Update more than two Sets using update() Function---')
# Initialize Sets
set_even_numbers = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
set_odd_numbers = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
set_whole_number = {0}
print('---Original Sets before Updating---')
print(set_even_numbers)
print(set_odd_numbers)
# Update Sets using update() Function
print('---New Set after Updating---')
set_even_numbers.update(set_odd_numbers, set_whole_number)
print(set_even_numbers)
except:
print("Error Occurred")
---Approach I - Update two Sets using update() Function---
---Original Sets before Updating---
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
---New Set after Updating---
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
----------------------------------------------------------
---Approach II - Update more than two Sets using update() Function---
---Original Sets before Updating---
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
---New Set after Updating---
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
TODO and Your Turn
Todo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
- Consider the following two Sets and answer the questions given below
- prophets = {‘Hazrat Muhammad (PBUH)’, ‘Hazrat Ibrahim (A.S.)’, ‘Hazrat Mosa (A.S.)’, ‘Hazrat Isa (A.S.)’, ‘Hazrat Adam (A.S.)’}
- ages = {25, 30, 15, 20, 22, 33}
- Questions
- Update Elements from Sets using following approaches (as shown in this Chapter)
- Method 1: Update Set using update() Function
- Update Elements from Sets using following approaches (as shown in this Chapter)
Your Turn Tasks
Your Turn Task 1
- Task
- Consider two Sets (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Update Elements from Sets using following approaches (as shown in this Chapter)
- Method 1: Update Set using update() Function
- Update Elements from Sets using following approaches (as shown in this Chapter)
Operation 8 – Deletion
- Removing elements from a set
- In Sha Allah, in this Example, we will show how to delete a Set using following four approaches:
- Approach 1: Discard() built-in Function
- Approach 2: Pop() built-in Function
- Approach 3: Clear() built-in Function
- Approach 4: Remove() built-in Function
- discard() Function
- Function Name
- discard()
- Syntax
set.discard([func])
- Purpose
- The main purpose of discard() function is to
- Remove a specific element from the set
- The main purpose of discard() function is to
- pop() Function
- Function Name
- pop()
- Syntax
set.pop()
- Purpose
- The main purpose of pop() function is to
- Remove and returns a random element from the set
- The main purpose of pop() function is to
- clear() Function
- Function Name
- clear()
- Syntax
set.clear()
- Purpose
- The main purpose of clear() function is to
- Remove all elements from the set
- The main purpose of clear() function is to
- remove() Function
- Function Name
- remove()
- Syntax
set.remove([func])
- Purpose
- The main purpose of remove() function is to
- Remove a specific element from the Set. If the specified element is not found, raise an error
- The main purpose of remove() function is to
Remove Specific Elements from Set using discard() Function
- Example 1 – Remove Specific Elements from Set using discard() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific Elements from Set using discard() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to Remove Specific Elements from Set using discard() Function
'''
try:
# Initialize a Set
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
print('---Original Set before using discard() Function---')
print(car_companies)
# Remove Specific Element from Set using discard() Function
print('---New Set after using discard() Function---')
car_companies.discard('Suzuki')
print(car_companies)
except:
print("Error Occurred")
---Original Set before using discard() Function---
{'Changan', 'Hyundai', 'Kia', 'Suzuki', 'Honda', 'Toyota'}
---New Set after using discard() Function---
{'Changan', 'Hyundai', 'Kia', 'Honda', 'Toyota'}
- Example 2 – Remove Specific Elements from Set using discard() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific Elements from Set using discard() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to Remove Specific Elements from Set using discard() Function
'''
try:
# Initialize a Set
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
print('---Original Set before using discard() Function---')
print(prime_numbers)
# Remove Specific Element from Set using discard() Function
print('---New Set after using discard() Function---')
prime_numbers.discard(13)
print(prime_numbers)
except:
print("Error Occurred")
---Original Set before using discard() Function---
{2, 3, 5, 7, 11, 13, 17, 19}
---New Set after using discard() Function---
{2, 3, 5, 7, 11, 17, 19}
- Example 3 – Remove Specific Elements from Set using discard() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific Elements from Set using discard() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to Remove Specific Elements from Set using discard() Function
'''
try:
# Initialize a Set
vowels = { 'a','e','i','o','u'}
print('---Original Set before using discard() Function---')
print(vowels)
# Remove Specific Element from Set using discard() Function
print('---New Set after using discard() Function---')
vowels.discard('a')
print(vowels)
except:
print("Error Occurred")
---Original Set before using remove() Function---
{'e', 'i', 'u', 'o', 'a'}
---New Set after using remove() Function---
{'e', 'i', 'u', 'o'}
Remove Random Elements from Set using pop() Function
- Example 1 – Remove Random Elements from Set using pop() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Random Elements from Set using pop() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to Remove Random Elements from Set using pop() Function
'''
try:
# Initialize a Set
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
print('---Original Set before using pop() Function---')
print(car_companies)
# Remove Random Element from Set using pop() Function
print('---New Set after using pop() Function---')
car_companies.pop()
print(car_companies)
except:
print("Error Occurred")
---Original Set before using pop() Function---
{'Suzuki', 'Toyota', 'Hyundai', 'Kia', 'Changan', 'Honda'}
---New Set after using pop() Function---
{'Toyota', 'Hyundai', 'Kia', 'Changan', 'Honda'}
- Example 2 – Remove Random Elements from Set using pop() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Random Elements from Set using pop() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to Remove Random Elements from Set using pop() Function
'''
try:
# Initialize a Set
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
print('---Original Set before using pop() Function---')
print(prime_numbers)
# Remove Random Element from Set using pop() Function
print('---New Set after using pop() Function---')
prime_numbers.pop()
print(prime_numbers)
except:
print("Error Occurred")
---Original Set before using pop() Function---
{2, 3, 5, 7, 11, 13, 17, 19}
---New Set after using pop() Function---
{3, 5, 7, 11, 13, 17, 19}
- Example 3 – Remove Random Elements from Set using pop() Function
''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Random Elements from Set using pop() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to Remove Random Elements from Set using pop() Function
'''
try:
# Initialize a Set
vowels = { 'a','e','i','o','u'}
print('---Original Set before using pop() Function---')
print(vowels)
# Remove Random Element from Set using pop() Function
print('---New Set after using pop() Function---')
vowels.pop()
print(vowels)
except:
print("Error Occurred")
---Original Set before using pop() Function---
{'e', 'i', 'o', 'a', 'u'}
---New Set after using pop() Function---
{'i', 'o', 'a', 'u'}
Remove Set using clear() Function
- Example 1 – Remove Set - Using clear() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Set using clear() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to remove set using clear() function
'''
try:
# Initialize a Set
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
print('---Original Set before using clear() Function---')
print(car_companies)
# Remove Set using clear() Function
print('---New Set after using clear() Function---')
car_companies.clear()
print(car_companies)
except:
print("Error Occurred")
---Original Set before using clear() Function---
{'Honda', 'Hyundai', 'Kia', 'Toyota', 'Changan', 'Suzuki'}
---New Set after using clear() Function---
set()
- Example 2 – Remove Set - Using clear() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Set using clear() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to remove set using clear() function
'''
try:
# Initialize a Set
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
print('---Original Set before using clear() Function---')
print(prime_numbers)
# Remove Set using clear() Function
print('---New Set after using clear() Function---')
prime_numbers.clear()
print(prime_numbers)
except:
print("Error Occurred")
---Original Set before using clear() Function---
{2, 3, 5, 7, 11, 13, 17, 19}
---New Set after using clear() Function---
set()
- Example 3 – Remove Set - Using clear() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Set using clear() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to remove set using clear() function
'''
try:
# Initialize a Set
vowels = { 'a','e','i','o','u'}
print('---Original Set before using clear() Function---')
print(vowels)
# Remove Set using clear() Function
print('---New Set after using clear() Function---')
vowels.clear()
print(vowels)
except:
print("Error Occurred")
---Original Set before using clear() Function---
{'o', 'i', 'a', 'e', 'u'}
---New Set after using clear() Function---
set()
Remove Specific Elements from Set using remove() Function
- Example 1 – Remove Specific Elements from Set using remove() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific Elements from Set using remove() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to Remove Specific Elements from Set using remove() Function
'''
try:
# Initialize a Set
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
print('---Original Set before using remove() Function---')
print(car_companies)
# Remove Specific Element from Set using remove() Function
print('---New Set after using remove() Function---')
car_companies.remove('Suzuki')
print(car_companies)
except:
print("Error Occurred")
---Original Set before using remove() Function---
{'Changan', 'Hyundai', 'Kia', 'Toyota', 'Honda', 'Suzuki'}
---New Set after using remove() Function---
{'Changan', 'Hyundai', 'Kia', 'Toyota', 'Honda'}
- Example 2 – Remove Specific Elements from Set using remove() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific Elements from Set using remove() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to Remove Specific Elements from Set using remove() Function
'''
try:
# Initialize a Set
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
print('---Original Set before using remove() Function---')
print(prime_numbers)
# Remove Specific Element from Set using remove() Function
print('---New Set after using remove() Function---')
prime_numbers.remove(5)
print(prime_numbers)
except:
print("Error Occurred")
---Original Set before using remove() Function---
{2, 3, 5, 7, 11, 13, 17, 19}
---New Set after using remove() Function---
{2, 3, 7, 11, 13, 17, 19}
- Example 3 – Remove Specific Elements from Set using remove() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific Elements from Set using remove() Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to Remove Specific Elements from Set using remove() Function
'''
try:
# Initialize a Set
vowels = { 'a','e','i','o','u'}
print('---Original Set before using remove() Function---')
print(vowels)
# Remove Specific Element from Set using remove() Function
print('---New Set after using remove() Function---')
vowels.remove('i')
print(vowels)
except:
print("Error Occurred")
---Original Set before using remove() Function---
{'e', 'o', 'i', 'u', 'a'}
---New Set after using remove() Function---
{'e', 'o', 'u', 'a'}
TODO and Your Turn
Todo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
- Consider the following two Sets and answer the questions given below
- prophets = {‘Hazrat Muhammad (PBUH)’, ‘Hazrat Ibrahim (A.S.)’, ‘Hazrat Mosa (A.S.)’, ‘Hazrat Isa (A.S.)’, ‘Hazrat Adam (A.S.)’}
- ages = {25, 30, 15, 20, 22, 33}
- Questions
- Delete Elements from Sets using following approaches (as shown in this Chapter)
- Approach 1: Discard() built-in Function
- Approach 2: Pop() built-in Function
- Approach 3: Clear() built-in Function
- Approach 4: Remove() built-in Function
- Delete Elements from Sets using following approaches (as shown in this Chapter)
Your Turn Tasks
Your Turn Task 1
- Task
- Consider two Sets (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Delete Elements from Sets using following approaches (as shown in this Chapter)
- Approach 1: Discard() built-in Function
- Approach 2: Pop() built-in Function
- Approach 3: Clear() built-in Function
- Approach 4: Remove() built-in Function
- Delete Elements from Sets using following approaches (as shown in this Chapter)
Nested Sets
- Nested Sets
- Question
- Can you have nested sets in Python?
- Answer
- Nested sets cannot contain mutable values including sets.
- Solution
- Frozenset
- A frozenset is very similar to a set except that a frozenset is immutable.
- Frozenset
Frozen Sets
- Frozen Sets
- Definition
- A frozen set in Python is like a combination of tuples and sets.
- Frozen sets cannot be added or removed once created
- forzenset() takes input as any iterable object and converts them into an immutable object. The order of elements is not guaranteed to be preserved
- Purpose
- Frozenset object are immutable they are mainly used as key in dictionary or elements of other sets
- They are used to carry out mathematical set operations
- Characteristics
- A frozen set has the following characteristics:
- No Duplication
- Unordered
- Immutable
- A frozen set has the following characteristics:
- Syntax
- The Syntax to declare a Sets is as follows:
frozenset(iterable)
- Example 1 – Initialize a Frozen Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Frozen Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to initialize a frozen set and display its content (data values) on the Output Screen
'''
try:
# Initialize a Set
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
# Convert Set to Frozen Set
frozensets = frozenset(car_companies)
# Display Data Values of a Frozenset
print("Data Values of Frozenset:", frozensets)
# Display Data-Type of a Frozenset using type() Function
print("Data-Type of Frozenset:", type(frozensets))
# Display Length of a Frozenset using len() Function
print("Length of Frozenset:", len(frozensets))
except:
print("Error Occurred")
Data Values of Frozenset: frozenset({'Toyota', 'Honda', 'Suzuki', 'Kia', 'Changan', 'Hyundai'})
Data-Type of Frozenset:
Length of Frozenset: 6
- Example 2 – Initialize a Frozen Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Frozen Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to initialize a frozen set and display its content (data values) on the Output Screen
'''
try:
# Initialize a Set
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
# Convert Set to Frozen Set
frozensets = frozenset(prime_numbers)
# Display Data Values of a Frozenset
print("Data Values of Frozenset:", frozensets)
# Display Data-Type of a Frozenset using type() Function
print("Data-Type of Frozenset:", type(frozensets))
# Display Length of a Frozenset using len() Function
print("Length of Frozenset:", len(frozensets))
except:
print("Error Occurred")
Data Values of Frozenset: frozenset({2, 3, 5, 7, 11, 13, 17, 19})
Data-Type of Frozenset:
Length of Frozenset: 8
- Example 3 – Initialize a Frozen Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Frozen Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 29-Jun-2022
end_data = 3-Jul-2022
'''
'''
Purpose of Program
The main purpose of the program is to initialize a frozen set and display its content (data values) on the Output Screen
'''
try:
# Initialize a Set
vowels = { 'a', 'e', 'i', 'o', 'u'}
# Convert Set to Frozen Set
frozensets = frozenset(vowels)
# Display Data Values of a Frozenset
print("Data Values of Frozenset:", frozensets)
# Display Data-Type of a Frozenset using type() Function
print("Data-Type of Frozenset:", type(frozensets))
# Display Length of a Frozenset using len() Function
print("Length of Frozenset:", len(frozensets))
except:
print("Error Occurred")
Data Values of Frozenset: frozenset({'a', 'o', 'u', 'i', 'e'})
Data-Type of Frozenset:
Length of Frozenset: 5
Access Element(s) of a Frozen Set
- Access Element(s) of a Frozen Set
- We cannot access individual values in a frozen set. We can only access all the elements together by looping through the frozen set
- Example 1 - Access Element(s) of a Frozen Set
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a Frozen Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to access element(s) of a Frozen Set
'''
try:
car_companies = {'Changan', 'Suzuki', 'Honda', 'Toyota', 'Hyundai', 'Kia'}
# Convert Set to Frozen Set
frozensets = frozenset(car_companies)
# Access Element(s) of a Frozen Set by Iterating through Elements Altogether
print("---Access Element(s) of a Frozen Set by Iterating through Elements---")
for elements in frozensets:
print(elements)
except:
print("Error Occurred")
---Access Element(s) of a Frozen Set by Iterating through Elements---
Honda
Hyundai
Suzuki
Changan
Kia
Toyota
- Example 2 - Access Element(s) of a Set
''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a Frozen Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to access element(s) of a Frozen Set
'''
try:
prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19}
# Convert Set to Frozen Set
frozensets = frozenset(prime_numbers)
# Access Element(s) of a Frozen Set by Iterating through Elements Altogether
print("---Access Element(s) of a Frozen Set by Iterating through Elements---")
for elements in frozensets:
print(elements)
except:
print("Error Occurred")
---Access Element(s) of a Frozen Set by Iterating through Elements---
2
3
5
7
11
13
17
19
- Example 3 - Access Element(s) of a Set
''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a Frozen Set
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to access element(s) of a Frozen Set
'''
try:
vowels = { 'a','e','i','o','u'}
# Convert Set to Frozen Set
frozensets = frozenset(vowels)
# Access Element(s) of a Frozen Set by Iterating through Elements Altogether
print("---Access Element(s) of a Frozen Set by Iterating through Elements---")
for elements in frozensets:
print(elements)
except:
print("Error Occurred")
---Access Element(s) of a Frozen Set by Iterating through Elements---
e
i
u
a
o
Operations on Frozen Sets
- Operations of Frozen Sets
- Since frozenset is immutable, there are no methods available to alter its elements. So add(), remove(), update(), pop() etc. functions are not defined for frozenset
TODO and Your Turn
Todo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
- Task
- Consider the following two Sets and answer the questions given below
- prophets = {‘Hazrat Muhammad (PBUH)’, ‘Hazrat Ibrahim (A.S.)’, ‘Hazrat Mosa (A.S.)’, ‘Hazrat Isa (A.S.)’, ‘Hazrat Adam (A.S.)’}
- ages = {25, 30, 15, 20, 22, 33}
- Consider the following two Sets and answer the questions given below
- Questions
- Create and Access Elements from Sets using frozensset() Function (as shown in this Chapter)
Your Turn Tasks
Your Turn Task 1
- Task
- Consider two Sets (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Create and Access Elements from Sets using frozensset() Function (as shown in this Chapter)
Pass Set to a Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Pass Set to a Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to pass set to a Function and display the result on the Output Screen
'''
def set_of_characters(characters):
print("---Set of Special Characters---")
for character in characters:
print(character)
if __name__ == "__main__": # main() Function
try:
# Initializing a Set
characters = {'*','@','#','$','&'}
# Function Call
set_of_characters(characters)
except ValueError:
print("Error! Enter Valid Values")
---Set of Special Characters---
@
*
$
&
#
TODO and Your Turn
Todo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
- Task
- Consider the Sets and answer the questions given below
- marks = {90, 80, 75, 80, 62, 53}
- Consider the Sets and answer the questions given below
- Questions
- Pass the given Sets to a Function (as shown in this Chapter)
Your Turn Tasks
Your Turn Task 1
- Task
- Consider two Sets (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Pass the given Sets to a Function (as shown in this Chapter)
Pass Frozen Set to a Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Pass Frozen Set to a Function
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_data = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to pass frozen set to a function and display the result on the Output Screen
'''
def set_of_characters(characters):
print("---Set of Special Characters---")
for character in characters:
print(character)
if __name__ == "__main__": # main() Function
try:
# Initializing a Set
characters = {'*','@','#','$','&'}
# Convert Set to Frozen Set
frozensets = frozenset(characters)
# Function Call
set_of_characters(frozensets)
except ValueError:
print("Error! Enter Valid Values")
---Set of Special Characters---
$
*
#
&
@
TODO and Your Turn
Todo Tasks
Your Turn Tasks
Todo Tasks
TODO Task 1
- Task
- Consider the Set and answer the questions given below
- marks = {90, 80, 75, 80, 62, 53}
- Consider the Set and answer the questions given below
- Questions
- Pass the given Frozen Sets to a Function (as shown in this Chapter)
Your Turn Tasks
Your Turn Task 1
- Task
- Consider two Sets (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Pass the given Frozen Sets to a Function (as shown in this Chapter)
Chapter Summary
- Chapter Summary
In this Chapter, I presented the following main concepts:
- Sets
- Definition
- The set—an unordered and unindexed collection of unique and immutable objects that supports operations corresponding to mathematical set theory. An item appears only once in a set, no matter how many times it is added
- Definition
- Set Declaration
- A Set can be declared using two methods
- Method 1: Use {}
- Method 2: Use set() Function
- A Set can be declared using two methods
- Initializing a Set
- Following two Methods are used for set initialization
- Method 1: Building a Complete Set
- Method 2: Building a Set Incrementally
- Following two Methods are used for set initialization
- Access Element(s) of a Set
- We cannot access individual values in a set. We can only access all the elements together by looping through the set
- Operations on Set
- Operation 1 – Creation
- Operation 2 – Insertion
- Operation 3 – Traversal
- Operation 4 – Searching
- Operation 5 – Sorting
- Operation 6 – Merging
- Operation 7 – Updation
- Operation 8 – Deletion
- Nested Sets
- Question
- Can you have nested sets in Python?
- Answer
- Nested sets cannot contain mutable values including sets.
- Solution
- Frozenset
- A frozenset is very similar to a set except that a frozenset is immutable.
- Frozenset
- Question
- Frozen Sets
- Definition
- A frozen set in Python is like a combination of tuples and sets.
- Frozen sets cannot be added or removed once created
- forzenset() takes input as any iterable object and converts them into an immutable object. The order of elements is not guaranteed to be preserved
- Definition
- Access Element(s) of a Frozen Set
- We cannot access individual values in a frozen set. We can only access all the elements together by looping through the frozen set
- Operations of Frozen Sets
- Since frozenset is immutable, there are no methods available to alter its elements. So add(), remove(), update(), pop() etc. functions are not defined for frozenset
In Next Chapter
- In Next Chapter
- In Sha Allah, in the next Chapter, I will present a detailed discussion on
- File Handling in Python