Chapter 33 - Tuples
- Authors
- Ms. Samavi Salman
- Dr. Rao Muhammad Adeel Nawab
- Supporting Material
Quick Recap
- Quick Recap – Dictionaries in Python
In previous Chapter, I presented
- Dictionary
- Definition 1
- In Python, Dictionary is a data structure which stores data as a set of unique identifiers, each of which have an associated value
- This Data Pairing is called key-value pair
- key
- The unique identifier is called the key for an item of data
- value
- A value is either the data being identified or the location of that data
- key
- Definition 2
- A Dictionary is a collection of key-value pairs
- Note
- In each key-value pair, the kay is mapped to its associated value.
- Data Type – key
- A Dictionary key can be almost any Python Object, but
- Mostly Strings and Numbers are used keys
- A Dictionary key can be almost any Python Object, but
- Data Type – value
- A Dictionary value can be any arbitrary Python Object
- Dictionary Declaration
- A Dictionary value can be any arbitrary Python Object
- A Dictionary can be declared using two methods
- Method 1: Use {}
- Method 2: Use dict() Function
- Declaring an Empty Dictionary
- Method 1: Use {}
- Method 2: Use dict() Built in Function
- Initializing a Dictionary
- Method 1: Building a Complete Dictionary
- Method 2: Building a Dictionary Incrementally
- Access value of an Individual Dictionary Item
- Accessing Elements of a Dictionary
- Method 1 – Access Element(s) of a Dictionary using key Name inside []
- Method 2 – Access Element(s) of a Dictionary using get() built-in Functions
- Access List of keys in a Dictionary
- Access List of keys in a Dictionary using key() built-in Function
- Accessing Elements of a Dictionary
- Access List of keys in a Dictionary using key() built-in Function
- Access List of values in a Dictionary
- Access List of values in a Dictionary using values() built-in Function
- Operations on Dictionary
- Operation 1 – Creation
- Operation 2 – Insertion
- Operation 3 – Traversal
- Operation 4 – Searching
- Operation 5 – Sorting
- Operation 6 – Merging
- Operation 7 – Updation
- Operation 8 – Deletion
- Dictionary Comprehension
- Dictionary comprehension is a simple method for (conditionally) transforming items of one dictionary into another dictionary without using loops
- Nested Dictionary
- A Nested Dictionary is a Dictionary of Dictionaries
Tuples
- 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
- Purpose
- Tuples are used to store multiple items in a single variable
- Tuple is one of 4 built-in data types in Python used to store collections of data
- A tuple is a collection which is ordered and unchangeable
- Importance
- Program execution is faster when manipulating a tuple
- Strengths
- Tuples are fined size in nature i.e. we can’t add/delete elements to/from a tuple
- We can search any element in a tuple
- Tuples are faster than lists, because they have a constant set of values
- Tuples can be used as dictionary keys, because they contain immutable values like strings, numbers, etc.
- Example
- Store a list of employee names
- Store a list of ice cream flavors stocked at an ice cream shop
- Syntax
- A Tuple can be declared using two methods
- Method 1: Use ()
- Method 2: Use tuple() Built in Function
- In Sha Allah, in next Slides, I will present both methods in detail
- Syntax - Method 1 (Use ())
- The Syntax to declare a Tuple using () is as follows
TUPLE_NAME = (
,
,
,
.
.
.
,
,
)
- Syntax - Method 2 (Use tuple() Built in Function)
- The Syntax to declare a Tuple using tuple() Built in Function is as follows
TUPLE_NAME = tuple(
,
,
,
.
.
.
,
,
)
Declaring an Empty Tuple
- Declaring an Empty Tuple
- In Sha Allah, in the next Slides, we will show how to declare an Empty Tuple using the following two Methods
- Method 1: Use ()
- Method 2: Use tuple() Built in Function
- Declaring an Empty Tuple - Method 1: Use ()
- To initialize an Empty Tuple, we will provide zero elements in the ()
- Example 1 - Declaring an Empty Tuple
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Declare an Empty Tuple
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 tuple using () and tuple() Function
'''
# Declare an Empty Tuple - Method 1: Use ()
try:
print("---Declare an Empty Tuple - Method 1: Use ()---")
# Tuple Declaration
layers_of_jannah_1 = ()
# Display Data Values of a Tuple
print("Data Values of layers_of_jannah_1 Tuple:", layers_of_jannah_1)
# Display Data-Type of a Tuple using type() Function
print("Data-Type of layers_of_jannah_1 Tuple:", type(layers_of_jannah_1))
# Display Length of a Tuple using len() Function
print("Length of layers_of_jannah_1 Tuple:", len(layers_of_jannah_1))
except:
print("Error Occurred")
print("---------------------------------------------------------------")
# Declare an Empty Tuple - Method 2: Use tuple()
try:
print("---Declare an Empty Tuple - Method 2: Use tuple()---")
layers_of_jannah_2 = tuple()
# Display Data Values of a Tuple
print("Data Values of layers_of_jannah_1 Tuple:", layers_of_jannah_2)
# Display Data-Type of a Tuple using type() Function
print("Data-Type of layers_of_jannah_2 Tuple:", type(layers_of_jannah_2))
# Display Length of a Tuple using len() Function
print("Length of layers_of_jannah_2 Tuple:", len(layers_of_jannah_2))
except:
print("Error Occurred")
---Declare an Empty Tuple - Method 1: Use ()---
Data Values of layers_of_jannah_1 Tuple: ()
Data-Type of layers_of_jannah_1 Tuple:
Length of layers_of_jannah_1 Tuple: 0
---------------------------------------------------------------
---Declare an Empty Tuple - Method 2: Use tuple()---
Data Values of layers_of_jannah_1 Tuple: ()
Data-Type of layers_of_jannah_2 Tuple:
Length of layers_of_jannah_2 Tuple: 0
- Example 2 - Declaring an Empty Tuple
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Declare an Empty Tuple
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 tuple using () and tuple() Function
'''
# Declare an Empty Tuple - Method 1: Use ()
try:
print("---Declare an Empty Tuple - Method 1: Use ()---")
# Tuple Declaration
pillars_of_islam_1 = ()
# Display Data Values of a Tuple
print("Data Values of pillars_of_islam_1 Tuple:", pillars_of_islam_1)
# Display Data-Type of a Tuple using type() Function
print("Data-Type of pillars_of_islam_1 Tuple:", type(pillars_of_islam_1))
# Display Length of a Tuple using len() Function
print("Length of pillars_of_islam_1 Tuple:", len(pillars_of_islam_1))
except:
print("Error Occurred")
print("---------------------------------------------------------------")
# Declare an Empty Tuple - Method 2: Use tuple()
try:
print("---Declare an Empty Tuple - Method 2: Use tuple()---")
pillars_of_islam_2 = tuple()
# Display Data Values of a Tuple
print("Data Values of pillars_of_islam_2 Tuple:", pillars_of_islam_2)
# Display Data-Type of a Tuple using type() Function
print("Data-Type of pillars_of_islam_2 Tuple:", type(pillars_of_islam_2))
# Display Length of a Tuple using len() Function
print("Length of pillars_of_islam_2 Tuple:", len(pillars_of_islam_2))
except:
print("Error Occurred")
---Declare an Empty Tuple - Method 1: Use ()---
Data Values of pillars_of_islam_1 Tuple: ()
Data-Type of pillars_of_islam_1 Tuple:
Length of pillars_of_islam_1 Tuple: 0
---------------------------------------------------------------
---Declare an Empty Tuple - Method 2: Use tuple()---
Data Values of pillars_of_islam_2 Tuple: ()
Data-Type of pillars_of_islam_2 Tuple:
Length of pillars_of_islam_2 Tuple: 0
- Example 3 - Declaring an Empty Tuple
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Declare an Empty Tuple
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 tuple using () and tuple() Function
'''
# Declare an Empty Tuple - Method 1: Use ()
try:
print("---Declare an Empty Tuple - Method 1: Use ()---")
# Tuple Declaration
pillars_of_human_engineering_1 = ()
# Display Data Values of a Tuple
print(pillars_of_human_engineering_1)
# Display Data-Type of a Tuple using type() Function
print("Data-Type of pillars_of_human_engineering_1 Tuple:")
print(type(pillars_of_human_engineering_1))
# Display Length of a Tuple using len() Function
print("Length of pillars_of_human_engineering_1 Tuple:")
print(len(pillars_of_human_engineering_1))
except:
print("Error Occurred")
print("---------------------------------------------------------------")
# Declare an Empty Tuple - Method 2: Use tuple()
try:
print("---Declare an Empty Tuple - Method 2: Use tuple()---")
pillars_of_human_engineering_2 = tuple()
# Display Data Values of a Tuple
print("Data Values of pillars_of_human_engineering_2 Tuple:")
print(pillars_of_human_engineering_2)
# Display Data-Type of a Tuple using type() Function
print("Data-Type of pillars_of_human_engineering_2 Tuple:")
print(type(pillars_of_human_engineering_2))
# Display Length of a Tuple using len() Function
print("Length of pillars_of_human_engineering_2 Tuple:")
print(len(pillars_of_human_engineering_2))
except:
print("Error Occurred")
---Declare an Empty Tuple - Method 1: Use ()---
()
Data-Type of pillars_of_human_engineering_1 Tuple:
Length of pillars_of_human_engineering_1 Tuple:
0
---------------------------------------------------------------
---Declare an Empty Tuple - Method 2: Use tuple()---
Data Values of pillars_of_human_engineering_2 Tuple:
()
Data-Type of pillars_of_human_engineering_2 Tuple:
Length of pillars_of_human_engineering_2 Tuple:
0
Initializing a Tuple
- Initializing a Tuple
- In Sha Allah, in the next Slides, I will show how to initialize a Tuple using the following two Methods
- Method 1: Building a Complete Tuple
- Method 2: Building a Tuple Incrementally
- Initializing a Tuple
- To demonstrate how we can initialize a Tuple using the two Methods discussed above, consider the following Real-world Scenario
Real-world Scenario |
· Sahaba Karam R.A. (The Great Companions of Our Beloved Prophet Hazrat Muhammad S.A.W.W. ) are role model for us in all walks of life. It is because of the great sacrifices of Shaba Karam R.A. that we are Muslims today, Alhumdulilah. To spread Islam in all corners of the world, Sahaba Karam R.A. left their homes and most importantly the company of Beloved Prophet Hazrat Muhammad S.A.W.W. There only mission was to save people from hell and enter into paradise. For this mission, Sahaba Karam R.A. spent whole of their lives preaching Islam in different countries of the world and were even buried there |
Your Job – As a Software Developer |
· Real-world Task o Search for at least 5 – 10 Sahaba Karam R.A. who are buried in different countries of the world (other than their homeland i.e., Madina Sharif) · Programming Task o Initialize a Tuple which maps the location (place of grave of Sahabi R.A.) with the name of the Sahabi R.A. |
Initializing a Tuple - Method 1: Building a Complete Tuple
- Initializing a Tuple - Method 1: Building a Complete Tuple
- In Sha Allah, we will initialize a complete Tuple using two Approaches
- Approach 1: Use ()
- Approach 2: Use tuple() Built in Function
- Example 1 - Initializing a Tuple - Method 1: Building a Complete Tuple
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Tuple
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 tuple and display its content (data values) on the Output Screen
'''
# Initialize a Tuple - Method 1: Use ()
try:
print("---Initialize a Tuple - Method 1: Use ()---")
# Tuple Initialization
layers_of_jannah_1 = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
# Display Data Values of a Tuple
print("Data Values of layers_of_jannah_1 Tuple:", layers_of_jannah_1)
# Display Data-Type of a Tuple using type() Function
print("Data-Type of layers_of_jannah_1 Tuple:", type(layers_of_jannah_1))
# Display Length of a Tuple using len() Function
print("Length of layers_of_jannah_1 Tuple:", len(layers_of_jannah_1))
except:
print("Error Occurred")
print("---------------------------------------------------------------")
# Initialize a Tuple - Method 2: Use tuple()
try:
print("---Initialize a Tuple - Method 2: Use tuple()---")
# Tuple Initialization
layers_of_jannah_2 = tuple(('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan'))
# Display Data Values of a Tuple
print("Data Values of layers_of_jannah_1 Tuple:", layers_of_jannah_2)
# Display Data-Type of a Tuple using type() Function
print("Data-Type of layers_of_jannah_2 Tuple:", type(layers_of_jannah_2))
# Display Length of a Tuple using len() Function
print("Length of layers_of_jannah_2 Tuple:", len(layers_of_jannah_2))
except:
print("Error Occurred")
--- Initialize a Tuple - Method 1: Use ()---
Data Values of layers_of_jannah_1 Tuple: ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
Data-Type of layers_of_jannah_1 Tuple:
Length of layers_of_jannah_1 Tuple: 8
---------------------------------------------------------------
--- Initialize a Tuple - Method 2: Use tuple()---
Data Values of layers_of_jannah_1 Tuple: ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
Data-Type of layers_of_jannah_2 Tuple:
Length of layers_of_jannah_2 Tuple: 8
- Example 2 - Initializing a Tuple - Method 1: Building a Complete Tuple
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Tuple
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 tuple and display its content (data values) on the Output Screen
'''
# Initialize a Tuple - Method 1: Use ()
try:
print("---Initialize a Tuple - Method 1: Use ()---")
# Tuple Initialization
pillars_of_islam_1 = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
# Display Data Values of a Tuple
print("Data Values of pillars_of_islam_1 Tuple:", pillars_of_islam_1)
# Display Data-Type of a Tuple using type() Function
print("Data-Type of pillars_of_islam_1 Tuple:", type(pillars_of_islam_1))
# Display Length of a Tuple using len() Function
print("Length of pillars_of_islam_1 Tuple:", len(pillars_of_islam_1))
except:
print("Error Occurred")
print("---------------------------------------------------------------")
# Initialize a Tuple - Method 2: Use tuple()
try:
print("---Initialize a Tuple - Method 2: Use tuple()---")
# Tuple Initialization
pillars_of_islam_2 = tuple(('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)'))
# Display Data Values of a Tuple
print("Data Values of pillars_of_islam_2 Tuple:", pillars_of_islam_2)
# Display Data-Type of a Tuple using type() Function
print("Data-Type of pillars_of_islam_2 Tuple:", type(pillars_of_islam_2))
# Display Length of a Tuple using len() Function
print("Length of pillars_of_islam_2 Tuple:", len(pillars_of_islam_2))
except:
print("Error Occurred")
--- Initialize a Tuple - Method 1: Use ()---
Data Values of pillars_of_islam_1 Tuple: ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
Data-Type of pillars_of_islam_1 Tuple:
Length of pillars_of_islam_1 Tuple: 5
---------------------------------------------------------------
--- Initialize a Tuple - Method 2: Use tuple()---
Data Values of pillars_of_islam_2 Tuple: ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
Data-Type of pillars_of_islam_2 Tuple:
Length of pillars_of_islam_2 Tuple: 5
- Example 3 - Initializing a Tuple - Method 1: Building a Complete Tuple
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Tuple
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 tuple and display its content (data values) on the Output Screen
'''
# Initialize a Tuple - Method 1: Use ()
try:
print("---Initialize a Tuple - Method 1: Use ()---")
# Tuple Initialization
pillars_of_human_engineering_1 = ('Truth', 'Honesty', 'Justice')
# Display Data Values of a Tuple
print(pillars_of_human_engineering_1)
# Display Data-Type of a Tuple using type() Function
print("Data-Type of pillars_of_human_engineering_1 Tuple:")
print(type(pillars_of_human_engineering_1))
# Display Length of a Tuple using len() Function
print("Length of pillars_of_human_engineering_1 Tuple:")
print(len(pillars_of_human_engineering_1))
except:
print("Error Occurred")
print("---------------------------------------------------------------")
# Initialize a Tuple - Method 2: Use tuple()
try:
print("---Initialize a Tuple - Method 2: Use tuple()---")
# Tuple Initialization
pillars_of_human_engineering_2 = tuple(('Truth', 'Honesty', 'Justice'))
# Display Data Values of a Tuple
print("Data Values of pillars_of_human_engineering_2 Tuple:")
print(pillars_of_human_engineering_2)
# Display Data-Type of a Tuple using type() Function
print("Data-Type of pillars_of_human_engineering_2 Tuple:")
print(type(pillars_of_human_engineering_2))
# Display Length of a Tuple using len() Function
print("Length of pillars_of_human_engineering_2 Tuple:")
print(len(pillars_of_human_engineering_2))
except:
print("Error Occurred")
--- Initialize a Tuple - Method 1: Use ()---
('Truth', 'Honesty', 'Justice')
Data-Type of pillars_of_human_engineering_1 Tuple:
Length of pillars_of_human_engineering_1 Tuple:
3
---------------------------------------------------------------
--- Initialize a Tuple - Method 2: Use tuple()---
Data Values of pillars_of_human_engineering_2 Tuple:
('Truth', 'Honesty', 'Justice')
Data-Type of pillars_of_human_engineering_2 Tuple:
Length of pillars_of_human_engineering_2 Tuple:
3
Method 2: Building a Tuple Incrementally
- Initializing a Tuple - Method 2: Building a Tuple Incrementally
- In Sha Allah, to Build a Tuple Incrementally (using () Approach), we will follow a Two Step Process
- Step 01: Declare an Empty Tuple using ()
- Step 02: Add Elements in the Tuple (created in Step 1) as they become available
- Create Tuple with one Element
- Having one element within parentheses is not enough. We will need a trailing comma to indicate that it is, in fact, a tuple
- Example 1 - Initializing a Tuple - Method 2: Building a Tuple Incrementally
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Tuple
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 tuple incrementally and display its content (data values) on the Output Screen
'''
# Initialize a Tuple - Method 1: Use ()
try:
print("---Initialize a Tuple - Method 1: Use ()---")
# Tuple Initialization
layers_of_jannah_1 = ('0') * 8
# Before Adding Elements in Tuple
print("Before Adding Elements in Tuple \n", layers_of_jannah_1)
# Converting Tuple into List for Value Assignment
layers_of_jannah_1 = list(layers_of_jannah_1)
# Building a Tuple Incrementally
layers_of_jannah_1[0] = 'Jannat-al-Maawa'
layers_of_jannah_1[1] = 'Jannat-al-Firdous'
layers_of_jannah_1[2] = 'Jannat-ul-Maqaam'
layers_of_jannah_1[3] = 'Jannat-al-Naeem'
layers_of_jannah_1[4] = 'Jannat-al-Kasif'
layers_of_jannah_1[5] = 'Dar-al-Salam'
layers_of_jannah_1[6] = 'Dar-al-Khuld'
layers_of_jannah_1[7] = 'Jannat-al-Adan'
# After Adding Elements in Tuple
print("After Adding Elements in Tuple \n", layers_of_jannah_1)
except Exception as ex:
print(ex)
print("---------------------------------------------------------------")
# Initialize a Tuple - Method 2: Use tuple()
try:
print("---Initialize a Tuple - Method 2: Use tuple()---")
# Tuple Initialization
layers_of_jannah_2 = tuple(('0') * 8)
# Before Adding Elements in Tuple
print("Before Adding Elements in Tuple \n", layers_of_jannah_2)
# Converting Tuple into List for Value Assignment
layers_of_jannah_2 = list(layers_of_jannah_2)
# Building a Tuple Incrementally
layers_of_jannah_2[0] = 'Jannat-al-Maawa'
layers_of_jannah_2[1] = 'Jannat-al-Firdous'
layers_of_jannah_2[2] = 'Jannat-ul-Maqaam'
layers_of_jannah_2[3] = 'Jannat-al-Naeem'
layers_of_jannah_2[4] = 'Jannat-al-Kasif'
layers_of_jannah_2[5] = 'Dar-al-Salam'
layers_of_jannah_2[6] = 'Dar-al-Khuld'
layers_of_jannah_2[7] = 'Jannat-al-Adan'
# After Adding Elements in Tuple
print("After Adding Elements in Tuple \n", layers_of_jannah_2)
except Exception as ex:
print(ex)
---Initialize a Tuple - Method 1: Use ()---
Before Adding Elements in Tuple
00000000
After Adding Elements in Tuple
['Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan']
---------------------------------------------------------------
---Initialize a Tuple - Method 2: Use tuple()---
Before Adding Elements in Tuple
('0', '0', '0', '0', '0', '0', '0', '0')
After Adding Elements in Tuple
['Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan']
- Example 2 - Initializing a Tuple - Method 2: Building a Tuple Incrementally
''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Tuple
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 tuple incrementally and display its content (data values) on the Output Screen
'''
# Initialize a Tuple - Method 1: Use ()
try:
print("---Initialize a Tuple - Method 1: Use ()---")
# Tuple Initialization
pillars_of_islam_1 = ('0') * 5
# Before Adding Elements in Tuple
print("Before Adding Elements in Tuple \n", pillars_of_islam_1)
# Converting Tuple into List for Value Assignment
pillars_of_islam_1 = list(pillars_of_islam_1)
# Building a Tuple Incrementally
pillars_of_islam_1[0] = 'Shahada'
pillars_of_islam_1[1] = 'Prayer (salat)'
pillars_of_islam_1[2] = 'Alms (zakat)'
pillars_of_islam_1[3] = 'Fasting (sawm)'
pillars_of_islam_1[4] = 'Pilgrimage (hajj)'
# After Adding Elements in Tuple
print("After Adding Elements in Tuple \n", pillars_of_islam_1)
except Exception as ex:
print(ex)
print("---------------------------------------------------------------")
# Initialize a Tuple - Method 2: Use tuple()
try:
print("---Initialize a Tuple - Method 2: Use tuple()---")
# Tuple Initialization
pillars_of_islam_2 = tuple(('0') * 5)
# Before Adding Elements in Tuple
print("Before Adding Elements in Tuple \n", pillars_of_islam_2)
# Converting Tuple into List for Value Assignment
pillars_of_islam_2 = list(pillars_of_islam_2)
# Building a Tuple Incrementally
pillars_of_islam_2[0] = 'Shahada'
pillars_of_islam_2[1] = 'Prayer (salat)'
pillars_of_islam_2[2] = 'Alms (zakat)'
pillars_of_islam_2[3] = 'Fasting (sawm)'
pillars_of_islam_2[4] = 'Pilgrimage (hajj)'
# After Adding Elements in Tuple
print("After Adding Elements in Tuple \n", pillars_of_islam_2)
except Exception as ex:
print(ex)
---Initialize a Tuple - Method 1: Use ()---
Before Adding Elements in Tuple
00000
After Adding Elements in Tuple
['Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)']
---------------------------------------------------------------
---Initialize a Tuple - Method 2: Use tuple()---
Before Adding Elements in Tuple
('0', '0', '0', '0', '0')
After Adding Elements in Tuple
['Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)']
- Example 3 - Initializing a Tuple - Method 2: Building a Tuple Incrementally
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a Tuple
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 tuple incrementally and display its content (data values) on the Output Screen
'''
# Initialize a Tuple - Method 1: Use ()
try:
print("---Initialize a Tuple - Method 1: Use ()---")
# Tuple Initialization
pillars_of_human_engineering_1 = ('0') * 3
# Before Adding Elements in Tuple
print("Before Adding Elements in Tuple \n", pillars_of_human_engineering_1)
# Converting Tuple into List for Value Assignment
pillars_of_human_engineering_1 = list(pillars_of_human_engineering_1)
# Building a Tuple Incrementally
pillars_of_human_engineering_1[0] = 'Truth'
pillars_of_human_engineering_1[1] = 'Honesty'
pillars_of_human_engineering_1[2] = 'Justice'
# After Adding Elements in Tuple
print("After Adding Elements in Tuple \n", pillars_of_human_engineering_1)
except Exception as ex:
print(ex)
print("---------------------------------------------------------------")
# Initialize a Tuple - Method 2: Use tuple()
try:
print("---Initialize a Tuple - Method 2: Use tuple()---")
# Tuple Initialization
pillars_of_human_engineering_2 = tuple(('0') * 3)
# Before Adding Elements in Tuple
print("Before Adding Elements in Tuple \n", pillars_of_human_engineering_2)
# Converting Tuple into List for Value Assignment
pillars_of_human_engineering_2 = list(pillars_of_human_engineering_2)
# Building a Tuple Incrementally
pillars_of_human_engineering_2[0] = 'Truth'
pillars_of_human_engineering_2[1] = 'Honesty'
pillars_of_human_engineering_2[2] = 'Justice'
# After Adding Elements in Tuple
print("After Adding Elements in Tuple \n", pillars_of_human_engineering_2)
except Exception as ex:
print(ex)
---Initialize a Tuple - Method 1: Use ()---
Before Adding Elements in Tuple
000
After Adding Elements in Tuple
['Truth', 'Honesty', 'Justice']
---------------------------------------------------------------
---Initialize a Tuple - Method 2: Use tuple()---
Before Adding Elements in Tuple
('0', '0', '0')
After Adding Elements in Tuple
['Truth', 'Honesty', 'Justice']
Access Elements from a Tuple
- Accessing Elements of a Tuple
- In Sha Allah, in next Slides, I will present 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
Method 1 - Access Element(s) of a Tuple using Indexing
- Example 1 – Access Elements of Tuple using Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a Tuple using Indexing
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 tuple using indexing
'''
try:
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
# Access Element(s) of a Tuple
print("---Access Element(s) of a Tuple---")
print("Value at Index 0 of layers_of_jannah[0] is:", layers_of_jannah[0])
print("Value at Index 1 of layers_of_jannah[1] is:", layers_of_jannah[1])
print("Value at Index 2 of layers_of_jannah[2] is:", layers_of_jannah[2])
print("Value at Index 3 of layers_of_jannah[3] is:", layers_of_jannah[3])
print("Value at Index 4 of layers_of_jannah[4] is:", layers_of_jannah[4])
print("Value at Index 5 of layers_of_jannah[5] is:", layers_of_jannah[5])
print("Value at Index 6 of layers_of_jannah[6] is:", layers_of_jannah[6])
print("Value at Index 7 of layers_of_jannah[7] is:", layers_of_jannah[7])
except:
print("Error Occurred")
---Access Element(s) of a Tuple---
Value at Index 0 of layers_of_jannah[0] is: Jannat-al-Maawa
Value at Index 1 of layers_of_jannah[1] is: Jannat-al-Firdous
Value at Index 2 of layers_of_jannah[2] is: Jannat-ul-Maqaam
Value at Index 3 of layers_of_jannah[3] is: Jannat-al-Naeem
Value at Index 4 of layers_of_jannah[4] is: Jannat-al-Kasif
Value at Index 5 of layers_of_jannah[5] is: Dar-al-Salam
Value at Index 6 of layers_of_jannah[6] is: Dar-al-Khuld
Value at Index 7 of layers_of_jannah[7] is: Jannat-al-Adan
- Example 2 – Access Elements of Tuple using Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a Tuple using Indexing
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 tuple using indexing
'''
try:
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
# Access Element(s) of a Tuple
print("---Access Element(s) of a Tuple---")
print("Value at Index 0 of pillars_of_islam[0] is:", pillars_of_islam[0])
print("Value at Index 1 of pillars_of_islam[1] is:", pillars_of_islam[1])
print("Value at Index 2 of pillars_of_islam[2] is:", pillars_of_islam[2])
print("Value at Index 3 of pillars_of_islam[3] is:", pillars_of_islam[3])
print("Value at Index 4 of pillars_of_islam[4] is:", pillars_of_islam[4])
except:
print("Error Occurred")
---Access Element(s) of a Tuple---
Value at Index 0 of pillars_of_islam[0] is: Shahada
Value at Index 1 of pillars_of_islam[1] is: Prayer (salat)
Value at Index 2 of pillars_of_islam[2] is: Alms (zakat)
Value at Index 3 of pillars_of_islam[3] is: Fasting (sawm)
Value at Index 4 of pillars_of_islam[4] is: Pilgrimage (hajj)
- Example 3 – Access Elements of Tuple using Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a Tuple using Indexing
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 tuple using indexing
'''
try:
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
# Access Element(s) of a Tuple
print("---Access Element(s) of a Tuple---")
print("Value at Index 0 of pillars_of_human_engineering[0] is:", pillars_of_human_engineering[0])
print("Value at Index 1 of pillars_of_human_engineering[1] is:", pillars_of_human_engineering[1])
print("Value at Index 2 of pillars_of_human_engineering[2] is:", pillars_of_human_engineering[2])
except:
print("Error Occurred")
Value at Index 0 is: Truth
Value at Index 1 is: Honesty
Value at Index 2 is: Justice
Method 2 - Access Element(s) of a Tuple using Negative Indexing
- Example 1 – Access Elements of Tuple using Negative Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a Tuple using Negative Indexing
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 tuple using negative indexing
'''
try:
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
# Access Element(s) of a Tuple
print("---Access Element(s) of a Tuple---")
print("Value at Index -1 of layers_of_jannah[-1] is:", layers_of_jannah[-1])
print("Value at Index -2 of layers_of_jannah[-2] is:", layers_of_jannah[-2])
print("Value at Index -3 of layers_of_jannah[-3] is:", layers_of_jannah[-3])
print("Value at Index -4 of layers_of_jannah[-4] is:", layers_of_jannah[-4])
print("Value at Index -5 of layers_of_jannah[-5] is:", layers_of_jannah[-5])
print("Value at Index -6 of layers_of_jannah[-6] is:", layers_of_jannah[-6])
print("Value at Index -7 of layers_of_jannah[-7] is:", layers_of_jannah[-7])
print("Value at Index -8 of layers_of_jannah[-8] is:", layers_of_jannah[-8])
except:
print("Error Occurred")
---Access Element(s) of a Tuple---
Value at Index -1 of layers_of_jannah[-1] is: Jannat-al-Adan
Value at Index -2 of layers_of_jannah[-2] is: Dar-al-Khuld
Value at Index -3 of layers_of_jannah[-3] is: Dar-al-Salam
Value at Index -4 of layers_of_jannah[-4] is: Jannat-al-Kasif
Value at Index -5 of layers_of_jannah[-5] is: Jannat-al-Naeem
Value at Index -6 of layers_of_jannah[-6] is: Jannat-ul-Maqaam
Value at Index -7 of layers_of_jannah[-7] is: Jannat-al-Firdous
Value at Index -8 of layers_of_jannah[-8] is: Jannat-al-Maawa
- Example 2 – Access Elements of Tuple using Negative Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a Tuple using Negative Indexing
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 tuple using negative indexing
'''
try:
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
# Access Element(s) of a Tuple
print("---Access Element(s) of a Tuple---")
print("Value at Index -1 of pillars_of_islam[-1] is:", pillars_of_islam[-1])
print("Value at Index -2 of pillars_of_islam[-2] is:", pillars_of_islam[-2])
print("Value at Index -3 of pillars_of_islam[-3] is:", pillars_of_islam[-3])
print("Value at Index -4 of pillars_of_islam[-4] is:", pillars_of_islam[-4])
print("Value at Index -5 of pillars_of_islam[-5] is:", pillars_of_islam[-5])
except:
print("Error Occurred")
---Access Element(s) of a Tuple---
Value at Index -1 of pillars_of_islam[-1] is: Pilgrimage (hajj)
Value at Index -2 of pillars_of_islam[-2] is: Fasting (sawm)
Value at Index -3 of pillars_of_islam[-3] is: Alms (zakat)
Value at Index -4 of pillars_of_islam[-4] is: Prayer (salat)
Value at Index -5 of pillars_of_islam[-5] is: Shahada
- Example 3 – Access Elements of Tuple using Negative Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a Tuple using Negative Indexing
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 tuple using negative indexing
'''
try:
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
# Access Element(s) of a Tuple
print("---Access Element(s) of a Tuple---")
print("Value at Index -1 of pillars_of_human_engineering[-1] is:", pillars_of_human_engineering[-1])
print("Value at Index -2 of pillars_of_human_engineering[-2] is:", pillars_of_human_engineering[-2])
print("Value at Index -3 of pillars_of_human_engineering[-3] is:", pillars_of_human_engineering[-3])
except:
print("Error Occurred")
---Access Element(s) of a Tuple---
Value at Index -1 of pillars_of_human_engineering[-1] is: Justice
Value at Index -2 of pillars_of_human_engineering[-2] is: Honesty
Value at Index -3 of pillars_of_human_engineering[-3] is: Truth
Method 3 - Access Element(s) of a Tuple using Slicing
- Stride
- There is also a feature of tuple slicing called stride. Stride represents how many values after the first item the code should skip over when slicing a tuple.
- Example 1 – Access Elements of Tuple using Slicing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a Tuple using Slicing
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 tuple using slicing
'''
try:
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
# Value of layers_of_jannah before Slicing
print("Value of layers_of_jannah before Slicing is:\n", layers_of_jannah)
# Access Element(s) of a Tuple
print("---Access Element(s) of a Tuple---")
# Access Element(s) of a Tuple using Slicing with Indexing
sliced_tuple_indexing = layers_of_jannah[2:5]
# Access Element(s) of a Tuple using Slicing with Negtive Indexing
sliced_tuple_neg_indexing = layers_of_jannah[-3:]
# Access Element(s) of a Tuple using Slicing with Stride
sliced_tuple_stride = layers_of_jannah[2:5:3]
# Value of layers_of_jannah after Slicing using Indexing
print("Value of layers_of_jannah after Slicing using Indexing is:")
print(sliced_tuple_indexing)
print("--------------------------------------------------------------")
# Value of layers_of_jannah after Slicing using Negtive Indexing
print("Value of layers_of_jannah after Slicing using Negative Indexing is:")
print(sliced_tuple_neg_indexing)
print("--------------------------------------------------------------")
# Value of layers_of_jannah after Slicing using Stride
print("Value of layers_of_jannah after Slicing using Stride is:")
print(sliced_tuple_stride)
except:
print("Error Occurred")
Value of layers_of_jannah before Slicing is:
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
---Access Element(s) of a Tuple---
Value of layers_of_jannah after Slicing using Indexing is:
('Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif')
--------------------------------------------------------------
Value of layers_of_jannah after Slicing using Negative Indexing is:
('Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
--------------------------------------------------------------
Value of layers_of_jannah after Slicing using Stride is:
('Jannat-ul-Maqaam',)
- Example 2 – Access Elements of Tuple using Slicing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a Tuple using Slicing
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 tuple using slicing
'''
try:
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
# Value of pillars_of_islam before Slicing
print("Value of pillars_of_islam before Slicing is:\n", pillars_of_islam)
# Access Element(s) of a Tuple
print("---Access Element(s) of a Tuple---")
sliced_tuple_indexing = pillars_of_islam[2:4]
sliced_tuple_neg_indexing = pillars_of_islam[-3:-5]
sliced_tuple_stride = pillars_of_islam[0:6:3]
# Value of pillars_of_islam after Slicing using Indexing
print("Value of pillars_of_islam after Slicing using Indexing is:")
print(sliced_tuple_indexing)
print("-----------------------------------------")
# Value of pillars_of_islam after Slicing using Negtive Indexing
print("Value of pillars_of_islam after Slicing using Negative Indexing is:")
print(sliced_tuple_neg_indexing)
print("-----------------------------------------")
# Value of pillars_of_islam after Slicing using Stride
print("Value of pillars_of_islam after Slicing using Stride is:")
print(sliced_tuple_stride)
except:
print("Error Occurred")
Value of pillars_of_islam before Slicing is:
('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
---Access Element(s) of a Tuple---
Value of pillars_of_islam after Slicing using Indexing is:
('Alms (zakat)', 'Fasting (sawm)')
-----------------------------------------
Value of pillars_of_islam after Slicing using Negative Indexing is:
()
-----------------------------------------
Value of pillars_of_islam after Slicing using Stride is:
('Shahada', 'Fasting (sawm)')
- Example 3 – Access Elements of Tuple using Slicing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a Tuple using Slicing
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 tuple using slicing
'''
try:
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
# Value of pillars_of_human_engineering before Slicing
print("Value of pillars_of_human_engineering before Slicing is:\n", pillars_of_human_engineering)
# Access Element(s) of a Tuple
print("---Access Element(s) of a Tuple---")
sliced_tuple_indexing = pillars_of_human_engineering[1:2]
sliced_tuple_neg_indexing = pillars_of_human_engineering[-1:]
sliced_tuple_stride = pillars_of_human_engineering[:]
# Value of pillars_of_human_engineering after Slicing using Indexing
print("Value of pillars_of_human_engineering after Slicing using Indexing is:")
print(sliced_tuple_indexing)
print("-----------------------------------------")
# Value of pillars_of_human_engineering after Slicing using Negtive Indexing
print("Value of pillars_of_human_engineering after Slicing using Negative Indexing is:")
print(sliced_tuple_neg_indexing)
print("-----------------------------------------")
# Value of pillars_of_human_engineering after Slicing using Stride
print("Value of pillars_of_human_engineering after Slicing using Stride is:")
print(sliced_tuple_stride)
except:
print("Error Occurred")
Value of pillars_of_human_engineering before Slicing is:
('Truth', 'Honesty', 'Justice')
---Access Element(s) of a Tuple---
Value of pillars_of_human_engineering after Slicing using Indexing is:
('Honesty',)
-----------------------------------------
Value of pillars_of_human_engineering after Slicing using Negative Indexing is:
('Justice',)
-----------------------------------------
Value of pillars_of_human_engineering after Slicing using Stride is:
('Truth', 'Honesty', 'Justice')
Operations on Tuple
Operation 1 – Creation
- Methods to Create a Tuple
- I have already presented methods to create a Tuple in the previous Section e.,
- Tuple Initialization
Operation 2 – Insertion
- Methods to Add Element(s) in a Tuple
- Tuples are immutable, and thus it is not possible to append elements in a Tuple directly. So, in order to add Element(s) in a Tuple, convert Tuple to a List using list() Function
- In Sha Allah, in next Slides, I will present three methods to add element(s) in a Tuple
- Method 1 – Add New Element(s) at the Start of a Tuple
- Method 2 – Add New Element(s) at the End of a Tuple
- Method 3 – Add New Element(s) at a Desired Location of a Tuple
- list() Function
- Function Name
- list()
- Definition
- The list() method creates a list object
- Purpose
- The main purpose of list() built-in Function is to
- The list() function creates a list object
- A list object is a collection which is ordered and changeable
- The list() function creates a list object
- The main purpose of list() built-in Function is to
- Syntax
tuple.list(iterable)
Add a New Element at the Start of a Tuple
- Add a New Element at the Start of a Tuple
- In Sha Allah, in the next Slides, we will show how to Add New Element(s) at Start of a Tuple using
- insert() Built-in Function
- Example 1 – Add a New Element at the Start of a Tuple – Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element at the Start of a Tuple
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 at the start of a Tuple on the output screen
'''
try:
print("---Add a Single Element at Start of a Tuple---")
# Tuple Initialization
layers_of_jannah = ('Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print("---Original Tuple before Adding Element at Start of a Tuple---")
print(layers_of_jannah)
# Data Type of i.e., layers_of_jannah before converting to List
print("---Data Type of i.e., layers_of_jannah before converting to List---")
print(type(layers_of_jannah))
# Converting a Tuple into a List and store list in "list_layers_of_jannah"
list_layers_of_jannah = list(layers_of_jannah)
# Data Type of i.e., layers_of_jannah after converting to List
print("---Data Type of i.e., layers_of_jannah after converting to List---")
print(type(list_layers_of_jannah))
# Add Data Value at Start of a Tuple using insert() Function
list_layers_of_jannah.insert(0, ('Jannat-al-Maawa'))
# Display Updated Tuple after Adding Element at Start of a Tuple
print("---Updated Tuple after Adding Element at Start of a Tuple---")
print(list_layers_of_jannah)
print("-------------------------------------------------------------")
print("---Again Add a Single Element at Start of a Tuple---")
print("---Original Tuple before Adding Element at Start of a Tuple---")
print(list_layers_of_jannah)
# Add Data Value at Start of a Tuple using insert() Function
list_layers_of_jannah.insert(1, ('Jannat-al-Firdous'))
# Display Updated Tuple after Adding Element at Start of a Tuple
print("---Updated Tuple after Adding Element at Start of a Tuple---")
print(list_layers_of_jannah)
except:
print("Error Occurred")
---Add a Single Element at Start of a Tuple---
---Original Tuple before Adding Element at Start of a Tuple---
('Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
---Data Type of i.e., layers_of_jannah before converting to List---
---Data Type of i.e., layers_of_jannah after converting to List---
---Updated Tuple after Adding Element at Start of a Tuple---
['Jannat-al-Maawa', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan']
-------------------------------------------------------------
---Again Add a Single Element at Start of a Tuple---
---Original Tuple before Adding Element at Start of a Tuple---
['Jannat-al-Maawa', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan']
---Updated Tuple after Adding Element at Start of a Tuple---
['Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan']
- Example 2 – Add a New Element at the Start of a Tuple – Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element at the Start of a Tuple
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 at the start of a Tuple on the output screen
'''
try:
print("---Add a Single Element at Start of a Tuple---")
# Tuple Initialization
pillars_of_islam = ('Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)')
print("---Original Tuple before Adding Element at Start of a Tuple---")
print(pillars_of_islam)
# Data Type of i.e., pillars_of_islam before converting to List
print("---Data Type of i.e., pillars_of_islam before converting to List---")
print(type(pillars_of_islam))
# Converting a Tuple into a List and store list in "list_pillars_of_islam"
list_pillars_of_islam = list(pillars_of_islam)
# Data Type of i.e., pillars_of_islam after converting to List
print("---Data Type of i.e., pillars_of_islam after converting to List---")
print(type(list_pillars_of_islam))
# Add Data Value at Start of a Tuple using insert() Function
list_pillars_of_islam.insert(0, ('Shahada'))
# Display Updated Tuple after Adding Element at Start of a Tuple
print("---Updated Tuple after Adding Element at Start of a Tuple---")
print(list_pillars_of_islam)
print("-------------------------------------------------------------")
print("---Again Add a Single Element at Start of a Tuple---")
print("---Original Tuple before Adding Element at Start of a Tuple---")
print(list_pillars_of_islam)
# Add Data Value at Start of a Tuple using insert() Function
list_pillars_of_islam.insert(4, ('Pilgrimage (hajj)'))
# Display Updated Tuple after Adding Element at Start of a Tuple
print("---Updated Tuple after Adding Element at Start of a Tuple---")
print(list_pillars_of_islam)
except:
print("Error Occurred")
---Add a Single Element at Start of a Tuple---
---Original Tuple before Adding Element at Start of a Tuple---
('Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)')
---Data Type of i.e., pillars_of_islam before converting to List---
---Data Type of i.e., pillars_of_islam after converting to List---
---Updated Tuple after Adding Element at Start of a Tuple---
['Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)']
-------------------------------------------------------------
---Again Add a Single Element at Start of a Tuple---
---Original Tuple before Adding Element at Start of a Tuple---
['Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)']
---Updated Tuple after Adding Element at Start of a Tuple---
['Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)']
- Example 3 – Add a New Element at the Start of a Tuple – Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element at the Start of a Tuple
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 at the start of a Tuple on the output screen
'''
try:
print("---Add a Single Element at Start of a Tuple---")
# Tuple Initialization
pillars_of_human_engineering = ('Truth',)
print("---Original Tuple before Adding Element at Start of a Tuple---")
print(pillars_of_human_engineering)
# Data Type of i.e., pillars_of_human_engineering before converting to List
print("---Data Type of i.e., pillars_of_human_engineering before converting to List---")
print(type(pillars_of_human_engineering))
# Converting a Tuple into a List and store list in "list_pillars_of_human_engineering"
list_pillars_of_human_engineering = list(pillars_of_human_engineering)
# Data Type of i.e., pillars_of_human_engineering after converting to List
print("---Data Type of i.e., pillars_of_human_engineering after converting to List---")
print(type(list_pillars_of_human_engineering))
# Add Data Value at Start of a Tuple using insert() Function
list_pillars_of_human_engineering.insert(1, ('Honesty'))
# Display Updated Tuple after Adding Element at Start of a Tuple
print("---Updated Tuple after Adding Element at Start of a Tuple---")
print(list_pillars_of_human_engineering)
print("-------------------------------------------------------------")
print("---Again Add a Single Element at Start of a Tuple---")
print("---Original Tuple before Adding Element at Start of a Tuple---")
print(list_pillars_of_human_engineering)
# Add Data Value at Start of a Tuple using insert() Function
list_pillars_of_human_engineering.insert(2, ('Justice'))
# Display Updated Tuple after Adding Element at Start of a Tuple
print("---Updated Tuple after Adding Element at Start of a Tuple---")
print(list_pillars_of_human_engineering)
except:
print("Error Occurred")
---Add a Single Element at Start of a Tuple---
---Original Tuple before Adding Element at Start of a Tuple---
('Truth',)
---Data Type of i.e., pillars_of_human_engineering before converting to List---
---Data Type of i.e., pillars_of_human_engineering after converting to List---
---Updated Tuple after Adding Element at Start of a Tuple---
['Truth', 'Honesty']
-------------------------------------------------------------
---Again Add a Single Element at Start of a Tuple---
---Original Tuple before Adding Element at Start of a Tuple---
['Truth', 'Honesty']
---Updated Tuple after Adding Element at Start of a Tuple---
['Truth', 'Honesty', 'Justice']
Add Multiple Elements at the Start of a Tuple
- Example 1 – Add Multiple Elements at the Start of a Tuple – Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add Multiple Elements at the Start of a Tuple
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 multiple elements at the start of a Tuple
'''
try:
print("---Add a Multiple Element(s) at Start of a Tuple---")
# Tuple Initialization
pillars_of_human_engineering = ('Truth',)
print("---Original Tuple before Adding Element(s) at Start of a Tuple---")
print(pillars_of_human_engineering)
# Data Type of i.e., pillars_of_human_engineering before converting to List
print("---Data Type of i.e., pillars_of_human_engineering before converting to List---")
print(type(pillars_of_human_engineering))
# Converting a Tuple into a List and store list in "list_pillars_of_human_engineering"
list_pillars_of_human_engineering = list(pillars_of_human_engineering)
# Data Type of i.e., pillars_of_human_engineering after converting to List
print("---Data Type of i.e., pillars_of_human_engineering after converting to List---")
print(type(list_pillars_of_human_engineering))
# Add Data Value at Start of a Tuple using insert() Function
list_pillars_of_human_engineering.insert(1, ('Honesty'))
list_pillars_of_human_engineering.insert(2, ('Justice'))
# Display Updated Tuple after Adding Element(s) at Start of a Tuple
print("---Updated Tuple after Adding Element(s) at Start of a Tuple---")
print(list_pillars_of_human_engineering)
except:
print("Error Occurred")
---Add a Multiple Element(s) at Start of a Tuple---
---Original Tuple before Adding Element(s) at Start of a Tuple---
('Truth',)
---Data Type of i.e., pillars_of_human_engineering before converting to List---
---Data Type of i.e., pillars_of_human_engineering after converting to List---
---Updated Tuple after Adding Element(s) at Start of a Tuple---
['Truth', 'Honesty', 'Justice']
- Example 2 – Add Multiple Elements at the Start of a Tuple – Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add Multiple Elements at the Start of a Tuple
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 multiple elements at the start of a Tuple
'''
try:
print("---Add a Multiple Element(s) at Start of a Tuple---")
# Tuple Initialization
pillars_of_islam = ('Alms (zakat)', 'Fasting (sawm)','Pilgrimage (hajj)')
print("---Original Tuple before Adding Element(s) at Start of a Tuple---")
print(pillars_of_islam)
# Data Type of i.e., pillars_of_islam before converting to List
print("---Data Type of i.e., pillars_of_islam before converting to List---")
print(type(pillars_of_islam))
# Converting a Tuple into a List and store list in "list_pillars_of_islam"
list_pillars_of_islam = list(pillars_of_islam)
# Data Type of i.e., pillars_of_islam after converting to List
print("---Data Type of i.e., pillars_of_islam after converting to List---")
print(type(list_pillars_of_islam))
# Add Data Value at Start of a Tuple using insert() Function
list_pillars_of_islam.insert(0, ('Shahada'))
list_pillars_of_islam.insert(1, ('Prayer (salat)'))
# Display Updated Tuple after Adding Element(s) at Start of a Tuple
print("---Updated Tuple after Adding Element(s) at Start of a Tuple---")
print(list_pillars_of_islam)
except:
print("Error Occurred")
---Add a Multiple Element(s) at Start of a Tuple---
---Original Tuple before Adding Element(s) at Start of a Tuple---
('Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
---Data Type of i.e., pillars_of_islam before converting to List---
---Data Type of i.e., pillars_of_islam after converting to List---
---Updated Tuple after Adding Element(s) at Start of a Tuple---
['Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)']
- Example 3 – Add Multiple Elements at the Start of a Tuple – Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add Multiple Elements at the Start of a Tuple
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 multiple elements at the start of a Tuple
'''
try:
print("---Add a Multiple Element(s) at Start of a Tuple---")
# Tuple Initialization
layers_of_jannah = ('Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print("---Original Tuple before Adding Element(s) at Start of a Tuple---")
print(layers_of_jannah)
# Data Type of i.e., layers_of_jannah before converting to List
print("---Data Type of i.e., layers_of_jannah before converting to List---")
print(type(layers_of_jannah))
# Converting a Tuple into a List and store list in "list_layers_of_jannah"
list_layers_of_jannah = list(layers_of_jannah)
# Data Type of i.e., layers_of_jannah after converting to List
print("---Data Type of i.e., layers_of_jannah after converting to List---")
print(type(list_layers_of_jannah))
# Add Data Value at Start of a Tuple using insert() Function
list_layers_of_jannah.insert(0, ('Jannat-al-Maawa'))
list_layers_of_jannah.insert(1, ('Jannat-al-Firdous'))
# Display Updated Tuple after Adding Element(s) at Start of a Tuple
print("---Updated Tuple after Adding Element(s) at Start of a Tuple---")
print(list_layers_of_jannah)
except:
print("Error Occurred")
---Add a Multiple Element(s) at Start of a Tuple---
---Original Tuple before Adding Element(s) at Start of a Tuple---
('Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
---Data Type of i.e., layers_of_jannah before converting to List---
---Data Type of i.e., layers_of_jannah after converting to List---
---Updated Tuple after Adding Element(s) at Start of a Tuple---
['Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan']
Add a New Element at the End of a Tuple
- Add a New Element at the End of a Tuple
- In Sha Allah, in the next Slides, we will show how to Add New Element(s) at End of a Tuple using
- append() Built-in Function
- Example 1 – Add a New Element at the End of a Tuple – Using append() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element at the End of a Tuple
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 at the end of a Tuple
'''
try:
print("---Add a Single Element at End of a Tuple---")
# Tuple Initialization
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld')
print("---Original Tuple before Adding Element at End of a Tuple---")
print(layers_of_jannah)
# Data Type of i.e., layers_of_jannah before converting to List
print("---Data Type of i.e., layers_of_jannah before converting to List---")
print(type(layers_of_jannah))
# Converting a Tuple into a List and store list in "list_layers_of_jannah"
list_layers_of_jannah = list(layers_of_jannah)
# Data Type of i.e., layers_of_jannah after converting to List
print("---Data Type of i.e., layers_of_jannah after converting to List---")
print(type(layers_of_jannah))
# Add Data Value at End of a Tuple using append() Function
list_layers_of_jannah.append(('Jannat-al-Adan'))
# Display Updated Tuple after Adding Element at End of a Tuple
print("---Updated Tuple after Adding Element at End of a Tuple---")
print(list_layers_of_jannah)
except:
print("Error Occurred")
---Add a Single Element at End of a Tuple---
---Original Tuple before Adding Element at End of a Tuple---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld')
---Data Type of i.e., layers_of_jannah before converting to List---
---Data Type of i.e., layers_of_jannah after converting to List---
---Updated Tuple after Adding Element at End of a Tuple---
['Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan']
- Example 2 – Add a New Element at the End of a Tuple – Using append() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element at the End of a Tuple
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 at the end of a Tuple
'''
try:
print("---Add a Single Element at End of a Tuple---")
# Tuple Initialization
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)')
print("---Original Tuple before Adding Element at End of a Tuple---")
print(pillars_of_islam)
# Data Type of i.e., pillars_of_islam before converting to List
print("---Data Type of i.e., pillars_of_islam before converting to List---")
print(type(pillars_of_islam))
# Converting a Tuple into a List and store list in "list_pillars_of_islam"
list_pillars_of_islam = list(pillars_of_islam)
# Data Type of i.e., pillars_of_islam after converting to List
print("---Data Type of i.e., pillars_of_islam after converting to List---")
print(type(list_pillars_of_islam))
# Add Data Value at End of a Tuple using append() Function
list_pillars_of_islam.append(('Pilgrimage (hajj)'))
# Display Updated Tuple after Adding Element at End of a Tuple
print("---Updated Tuple after Adding Element at End of a Tuple---")
print(list_pillars_of_islam)
except:
print("Error Occurred")
---Add a Single Element at End of a Tuple---
---Original Tuple before Adding Element at End of a Tuple---
('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)')
---Data Type of i.e., pillars_of_islam before converting to List---
---Data Type of i.e., pillars_of_islam after converting to List---
---Updated Tuple after Adding Element at End of a Tuple---
['Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)']
- Example 3 – Add a New Element at the End of a Tuple – Using append() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element at the End of a Tuple
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 at the end of a Tuple
'''
try:
print("---Add a Single Element at End of a Tuple---")
# Tuple Initialization
pillars_of_human_engineering = ('Truth', 'Honesty')
print("---Original Tuple before Adding Element at End of a Tuple---")
print(pillars_of_human_engineering)
# Data Type of i.e., pillars_of_human_engineering before converting to List
print("---Data Type of i.e., pillars_of_human_engineering before converting to List---")
print(type(pillars_of_human_engineering))
# Converting a Tuple into a List and store list in "list_pillars_of_human_engineering"
list_pillars_of_human_engineering = list(pillars_of_human_engineering)
# Data Type of i.e., pillars_of_islam after converting to List
print("---Data Type of i.e., pillars_of_human_engineering after converting to List---")
print(type(list_pillars_of_human_engineering))
# Add Data Value at the End of a Tuple using append() Function
list_pillars_of_human_engineering.append(('Justice'))
# Display Updated Tuple after Adding Element at End of a Tuple
print("---Updated Tuple after Adding Element at End of a Tuple---")
print(list_pillars_of_human_engineering)
except:
print("Error Occurred")
---Add a Single Element at End of a Tuple---
---Original Tuple before Adding Element at End of a Tuple---
('Truth', 'Honesty')
---Data Type of i.e., pillars_of_human_engineering before converting to List---
---Data Type of i.e., pillars_of_human_engineering after converting to List---
---Updated Tuple after Adding Element at End of a Tuple---
['Truth', 'Honesty', 'Justice']
Add Multiple Elements at the End of a Tuple
- Example 1 – Add Multiple Elements at the End of a Tuple – Using append() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add Multiple Elements at the End of a Tuple
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 multiple elements at the end of a Tuple
'''
try:
print("---Add a Multiple Element(s) at End of a Tuple---")
# Tuple Initialization
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam')
print("---Original Tuple before Adding Element(s) at End of a Tuple---")
print(layers_of_jannah)
# Data Type of i.e., layers_of_jannah before converting to List
print("---Data Type of i.e., layers_of_jannah before converting to List---")
print(type(layers_of_jannah))
# Converting a Tuple into a List and store list in "list_layers_of_jannah"
list_layers_of_jannah = list(layers_of_jannah)
# Data Type of i.e., layers_of_jannah after converting to List
print("---Data Type of i.e., layers_of_jannah after converting to List---")
print(type(list_layers_of_jannah))
# Add Data Value at End of a Tuple using append() Function
list_layers_of_jannah.append(('Jannat-al-Adan'))
list_layers_of_jannah.append(('Dar-al-Khuld'))
# Display Updated Tuple after Adding Elemen(s) at End of a Tuple
print("---Updated Tuple after Adding Element(s) at End of a Tuple---")
print(list_layers_of_jannah)
except:
print("Error Occurred")
---Add a Multiple Element(s) at End of a Tuple---
---Original Tuple before Adding Element(s) at End of a Tuple---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam')
---Data Type of i.e., layers_of_jannah before converting to List---
---Data Type of i.e., layers_of_jannah after converting to List---
---Updated Tuple after Adding Element(s) at End of a Tuple---
['Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Jannat-al-Adan', 'Dar-al-Khuld']
- Example 2 – Add Multiple Elements at the End of a Tuple – Using append() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add Multiple Elements at the End of a Tuple
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 multiple elements at the end of a Tuple
'''
try:
print("---Add a Multiple Element(s) at End of a Tuple---")
# Tuple Initialization
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)')
print("---Original Tuple before Adding Element(s) at End of a Tuple---")
print(pillars_of_islam)
# Data Type of i.e., pillars_of_islam before converting to List
print("---Data Type of i.e., pillars_of_islam before converting to List---")
print(type(pillars_of_islam))
# Converting a Tuple into a List and store list in "list_pillars_of_islam"
list_pillars_of_islam = list(pillars_of_islam)
# Data Type of i.e., pillars_of_islam after converting to List
print("---Data Type of i.e., pillars_of_islam after converting to List---")
print(type(list_pillars_of_islam))
# Add Data Value at End of a Tuple using append() Function
list_pillars_of_islam.append(('Fasting (sawm)'))
list_pillars_of_islam.append(('Pilgrimage (hajj)'))
# Display Updated Tuple after Adding Element(s) at End of a Tuple
print("---Updated Tuple after Adding Element(s) at End of a Tuple---")
print(list_pillars_of_islam)
except:
print("Error Occurred")
---Add a Multiple Element(s) at End of a Tuple---
---Original Tuple before Adding Element(s) at End of a Tuple---
('Shahada', 'Prayer (salat)', 'Alms (zakat)')
---Data Type of i.e., pillars_of_islam before converting to List---
---Data Type of i.e., pillars_of_islam after converting to List---
---Updated Tuple after Adding Element(s) at End of a Tuple---
['Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)']
- Example 3 – Add Multiple Elements at the End of a Tuple – Using append() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add Multiple Elements at the End of a Tuple
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 multiple elements at the end of a Tuple
'''
try:
print("---Add a Multiple Elements at End of a Tuple---")
# Tuple Initialization
pillars_of_human_engineering = ('Truth',)
print("---Original Tuple before Adding Elements at End of a Tuple---")
print(pillars_of_human_engineering)
# Data Type of i.e., pillars_of_human_engineering before converting to List
print("---Data Type of i.e., pillars_of_human_engineering before converting to List---")
print(type(pillars_of_human_engineering))
# Converting a Tuple into a List and store list in "list_pillars_of_human_engineering"
list_pillars_of_human_engineering = list(pillars_of_human_engineering)
# Data Type of i.e., pillars_of_islam after converting to List
print("---Data Type of i.e., pillars_of_human_engineering after converting to List---")
print(type(list_pillars_of_human_engineering))
# Add Data Value at the End of a Tuple using append() Function
list_pillars_of_human_engineering.append(('Honesty'))
list_pillars_of_human_engineering.append(('Justice'))
# Display Updated Tuple after Adding Element(s) at End of a Tuple
print("---Updated Tuple after Adding Element(s) at End of a Tuple---")
print(list_pillars_of_human_engineering)
except:
print("Error Occurred")
---Add a Multiple Elements at End of a Tuple---
---Original Tuple before Adding Elements at End of a Tuple---
('Truth',)
---Data Type of i.e., pillars_of_human_engineering before converting to List---
---Data Type of i.e., pillars_of_human_engineering after converting to List---
---Updated Tuple after Adding Element(s) at End of a Tuple---
['Truth', 'Honesty', 'Justice']
Add New Element(s) at a Desired Location of a Tuple
- Add New Element(s) at a Desired Location of a Tuple
- In Sha Allah, in the next Slides, we will show how to add new element to the Desired Location of a Tuple using
- insert() Built-in Function
- Example 1 – Add a New Element at a Desired Location of a Tuple - Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element at a Desired Location of a Tuple
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 at a desired location of a Tuple on the output screen
'''
try:
print("---Add a New Element at a Desired Location of a Tuple---")
# Tuple Initialization
pillars_of_human_engineering = ('Honesty',)
print("---Original Tuple before Adding Element(s)---")
print(pillars_of_human_engineering)
# Data Type of i.e., pillars_of_human_engineering before converting to List
print("---Data Type of i.e., pillars_of_human_engineering before converting to List---")
print(type(pillars_of_human_engineering))
# Converting a Tuple into a List and store list in "list_pillars_of_human_engineering"
list_pillars_of_human_engineering = list(pillars_of_human_engineering)
# Data Type of i.e., pillars_of_human_engineering after converting to List
print("---Data Type of i.e., pillars_of_human_engineering after converting to List---")
print(type(list_pillars_of_human_engineering))
# Add Data Value at Desired Location of a Tuple using insert() Function
list_pillars_of_human_engineering.insert(0, ('Truth'))
list_pillars_of_human_engineering.insert(2, ('Justice'))
# Display Updated Tuple after Adding Element(s)
print("---Updated Tuple after Adding Element(s)---")
print(list_pillars_of_human_engineering)
except:
print("Error Occurred")
---Add a New Element at a Desired Location of a Tuple---
---Original Tuple before Adding Element(s)---
('Honesty',)
---Data Type of i.e., pillars_of_human_engineering before converting to List---
---Data Type of i.e., pillars_of_human_engineering after converting to List---
---Updated Tuple after Adding Element(s)---
['Truth', 'Honesty', 'Justice']
- Example 2 – Add a New Element at a Desired Location of a Tuple - Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element at a Desired Location of a Tuple
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 at a desired location of a tuple
'''
try:
print("---Add a New Element at a Desired Location of a Tuple---")
# Tuple Initialization
pillars_of_islam = ('Shahada', 'Fasting (sawm)', 'Pilgrimage (hajj)')
print("---Original Tuple before Adding Element(s)---")
print(pillars_of_islam)
# Data Type of i.e., pillars_of_islam before converting to List
print("---Data Type of i.e., pillars_of_islam before converting to List---")
print(type(pillars_of_islam))
# Converting a Tuple into a List and store list in "list_pillars_of_islam"
list_pillars_of_islam = list(pillars_of_islam)
# Data Type of i.e., pillars_of_islam after converting to List
print("---Data Type of i.e., pillars_of_islam after converting to List---")
print(type(list_pillars_of_islam))
# Add Data Value at Desired Location of a Tuple using insert() Function
list_pillars_of_islam.insert(1, ('Prayer (salat)'))
list_pillars_of_islam.insert(3, ('Alms (zakat)'))
# Display Updated Tuple after Adding Element(s)
print("---Updated Tuple after Adding Element(s)---")
print(list_pillars_of_islam)
except:
print("Error Occurred")
---Add a New Element at a Desired Location of a Tuple---
---Original Tuple before Adding Element(s)---
('Shahada', 'Fasting (sawm)', 'Pilgrimage (hajj)')
---Data Type of i.e., pillars_of_islam before converting to List---
---Data Type of i.e., pillars_of_islam after converting to List---
---Updated Tuple after Adding Element(s)---
['Shahada', 'Prayer (salat)', 'Fasting (sawm)', 'Alms (zakat)', 'Pilgrimage (hajj)']
- Example 3 – Add a New Element at a Desired Location of a Tuple - Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element at a Desired Location of a Tuple
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 at a desired location of a tuple
'''
try:
print("---Add a New Element at a Desired Location of a Tuple---")
# Tuple Initialization
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Adan')
print("---Original Tuple before Adding Element(s)---")
print(layers_of_jannah)
# Data Type of i.e., layers_of_jannah before converting to List
print("---Data Type of i.e., layers_of_jannah before converting to List---")
print(type(layers_of_jannah))
# Converting a Tuple into a List and store list in "list_layers_of_jannah"
list_layers_of_jannah = list(layers_of_jannah)
# Data Type of i.e., layers_of_jannah after converting to List
print("---Data Type of i.e., layers_of_jannah after converting to List---")
print(type(list_layers_of_jannah))
# Add Data Value at Desired Location of a Tuple using insert() Function
list_layers_of_jannah.insert(4, ('Jannat-al-Kasif'))
list_layers_of_jannah.insert(5, ('Dar-al-Salam'))
list_layers_of_jannah.insert(6, ('Dar-al-Khuld'))
# Display Updated Tuple after Adding Element(s)
print("---Updated Tuple after Adding Element(s)---")
print(list_layers_of_jannah)
except:
print("Error Occurred")
---Add a New Element at a Desired Location of a Tuple---
---Original Tuple before Adding Element(s)---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Adan')
---Data Type of i.e., layers_of_jannah before converting to List---
---Data Type of i.e., layers_of_jannah after converting to List---
---Updated Tuple after Adding Element(s)---
['Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan']
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Tuples 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 Tuples and answer the questions given below
- Questions
- Insert Elements to prophets and ages Tuples using following methods (as shown in this Chapter)
- Method 1 – Add a New Element at the Start of a Tuple
- Method 2 – Add a New Element at the End of a Tuple
- Method 3 – Add a New Element at a Desired Location of a Tuple
- Insert Elements to prophets and ages Tuples using following methods (as shown in this Chapter)
Your Turn Task 1
- Task
- Consider two Tuples (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Insert Elements to selected Tuples using following methods (as shown in this Chapter)
- Method 1 – Add a New Element at the Start of a Tuple
- Method 2 – Add a New Element at the End of a Tuple
- Method 3 – Add a New Element at a Desired Location of a Tuple
- Insert Elements to selected Tuples using following methods (as shown in this Chapter)
Operation 3 – Traverse
- Methods to Traverse a Tuple
- In Sha Allah, in next Slides, I will present two methods to traverse a Tuple
- Method 1: Traversing an Entire Tuple
- Method 2: Traversing a Tuple (Element by Element)
Traversing an Entire Tuple
- Example 1 - Traversing an Entire Tuple
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Traversing an Entire Tuple
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 traverse an entire Tuple and display the result on the Output Screen
'''
try:
# Tuple Initialization
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
# Traverse an entire Tuple
print(layers_of_jannah)
except:
print('Error Occurred')
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
- Example 2 - Traversing an Entire Tuple
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Traversing an Entire Tuple
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 traverse an entire Tuple and display the result on the Output Screen
'''
try:
# Tuple Initialization
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
# Traverse an entire Tuple
print(pillars_of_islam)
except:
print('Error Occurred')
('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
- Example 3 - Traversing an Entire Tuple
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Traversing an Entire Tuple
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 traverse an entire Tuple and display the result on the Output Screen
'''
try:
# Tuple Initialization
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
# Traverse an entire Tuple
print(pillars_of_human_engineering)
except:
print('Error Occurred')
('Truth', 'Honesty', 'Justice')
Traversing a Tuple (Element by Element)
- Example 1 - Traversing a Tuple (Element by Element)
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Traversing a Tuple (Element by Element)
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 traverse a Tuple (element by element) and display the result on the Output Screen
'''
try:
# Approach I - Iterate over a Tuple (element by element) using for loop
# Tuple Initialization
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
# Iterate over a Tuple (element by element) using for loop
print(" ---Approach I - Iterate over a Tuple (element by element) using for loop---")
print("---Traverse a Tuple (Element by Element) using for Loop---")
for element in layers_of_jannah:
print(element)
except:
print('Error Occurred')
print("-------------------------------------------------------------------")
try:
# Approach II - Iterate over a Tuple (element by element) using Indexing
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
# Iterate over a Tuple (element by element) using for loop with Indexing
print(" ---Approach II - Iterate over a Tuple (element by element) using Indexing---")
print("---Traverse a Tuple (Element by Element) using for Loop with Indexing---")
for element in range(0, len(layers_of_jannah)):
print("caliphs_of_islam[",element,"]: " + layers_of_jannah[element])
except:
print('Error Occurred')
print("---------------------------------------------------------------------------")
try:
# Approach III - Iterate over a Tuple (element by element) using Slicing
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
counter = 0
print("---Approach III - Iterate over a Tuple (element by element) using Slicing---")
# Iterate over a Tuple (element by element) using for loop with Slicing
print("---Traverse a Tuple (Element by Element) using for Loop with Slicing---")
# Slicing Operator => string[starting index : ending index : step value]
for element in layers_of_jannah[0 : len(layers_of_jannah): 1]:
counter = counter + 1
print("layers_of_jannah[",counter,"]: " + element)
except:
print('Error Occurred')
print("---------------------------------------------------------------------------")
try:
# ---Approach IV - Iterate over a Tuple (element by element) for loop using enumerate() Function
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
counter = 0
print("---Approach IV - Iterate over a Tuple (element by element) using enumerate() Function---")
# Iterate over a Tuple (element by element) using for loop using enumerate() Function
print("---Traverse a Tuple (Element by Element) using for Loop with enumerate() Function---")
# Iterate over a Tuple (element by element) using for loop using enumerate() Function
for count, element in enumerate(layers_of_jannah):
print (count," ",element)
except:
print('Error Occurred')
---Approach I - Iterate over a Tuple (element by element) using for loop---
---Traverse a Tuple (Element by Element) using for Loop---
Jannat-al-Maawa
Jannat-al-Firdous
Jannat-ul-Maqaam
Jannat-al-Naeem
Jannat-al-Kasif
Dar-al-Salam
Dar-al-Khuld
Jannat-al-Adan
-------------------------------------------------------------------
---Approach II - Iterate over a Tuple (element by element) using Indexing---
---Traverse a Tuple (Element by Element) using for Loop with Indexing---
caliphs_of_islam[ 0 ]: Jannat-al-Maawa
caliphs_of_islam[ 1 ]: Jannat-al-Firdous
caliphs_of_islam[ 2 ]: Jannat-ul-Maqaam
caliphs_of_islam[ 3 ]: Jannat-al-Naeem
caliphs_of_islam[ 4 ]: Jannat-al-Kasif
caliphs_of_islam[ 5 ]: Dar-al-Salam
caliphs_of_islam[ 6 ]: Dar-al-Khuld
caliphs_of_islam[ 7 ]: Jannat-al-Adan
---------------------------------------------------------------------------
---Approach III - Iterate over a Tuple (element by element) using Slicing---
---Traverse a Tuple (Element by Element) using for Loop with Slicing---
layers_of_jannah[ 1 ]: Jannat-al-Maawa
layers_of_jannah[ 2 ]: Jannat-al-Firdous
layers_of_jannah[ 3 ]: Jannat-ul-Maqaam
layers_of_jannah[ 4 ]: Jannat-al-Naeem
layers_of_jannah[ 5 ]: Jannat-al-Kasif
layers_of_jannah[ 6 ]: Dar-al-Salam
layers_of_jannah[ 7 ]: Dar-al-Khuld
layers_of_jannah[ 8 ]: Jannat-al-Adan
---------------------------------------------------------------------------
---Approach IV - Iterate over a Tuple (element by element) using enumerate() Function---
---Traverse a Tuple (Element by Element) using for Loop with enumerate() Function---
0 Jannat-al-Maawa
1 Jannat-al-Firdous
2 Jannat-ul-Maqaam
3 Jannat-al-Naeem
4 Jannat-al-Kasif
5 Dar-al-Salam
6 Dar-al-Khuld
7 Jannat-al-Adan
- Example 2 - Traversing a Tuple (Element by Element)
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Traversing a Tuple (Element by Element)
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 traverse a Tuple (element by element) and display the result on the Output Screen
'''
try:
# Approach I - Iterate over a Tuple (element by element) using for loop
# Tuple Initialization
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
# Iterate over a Tuple (element by element) using for loop
print(" ---Approach I - Iterate over a Tuple (element by element) using for loop---")
print("---Traverse a Tuple (Element by Element) using for Loop---")
for element in pillars_of_islam:
print(element)
except:
print('Error Occurred')
print("-------------------------------------------------------------------")
try:
# Approach II - Iterate over a Tuple (element by element) using Indexing
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
# Iterate over a Tuple (element by element) using for loop with Indexing
print(" ---Approach II - Iterate over a Tuple (element by element) using Indexing---")
print("---Traverse a Tuple (Element by Element) using for Loop with Indexing---")
for element in range(0, len(pillars_of_islam)):
print("pillars_of_islam[",element,"]: " + pillars_of_islam[element])
except:
print('Error Occurred')
print("---------------------------------------------------------------------------")
try:
# Approach III - Iterate over a Tuple (element by element) using Slicing
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
counter = 0
print("---Approach III - Iterate over a Tuple (element by element) using Slicing---")
# Iterate over a Tuple (element by element) using for loop with Slicing
print("---Traverse a Tuple (Element by Element) using for Loop with Slicing---")
# Slicing Operator => string[starting index : ending index : step value]
for element in layers_of_jannah[0 : len(pillars_of_islam): 1]:
counter = counter + 1
print("layers_of_jannah[",counter,"]: " + element)
except:
print('Error Occurred')
print("---------------------------------------------------------------------------")
try:
# Approach IV - Iterate over a Tuple (element by element) for loop using enumerate() Function
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
counter = 0
print("---Approach IV - Iterate over a Tuple (element by element) using enumerate() Function---")
# Iterate over a Tuple (element by element) using for loop using enumerate() Function
print("---Traverse a Tuple (Element by Element) using for Loop with enumerate() Function---")
# Iterate over a Tuple (element by element) using for loop using enumerate() Function
for count, element in enumerate(pillars_of_islam):
print (count," ",element)
except:
print('Error Occurred')
---Approach I - Iterate over a Tuple (element by element) using for loop---
---Traverse a Tuple (Element by Element) using for Loop---
Shahada
Prayer (salat)
Alms (zakat)
Fasting (sawm)
Pilgrimage (hajj)
-------------------------------------------------------------------
---Approach II - Iterate over a Tuple (element by element) using Indexing---
---Traverse a Tuple (Element by Element) using for Loop with Indexing---
pillars_of_islam[ 0 ]: Shahada
pillars_of_islam[ 1 ]: Prayer (salat)
pillars_of_islam[ 2 ]: Alms (zakat)
pillars_of_islam[ 3 ]: Fasting (sawm)
pillars_of_islam[ 4 ]: Pilgrimage (hajj)
---------------------------------------------------------------------------
---Approach III - Iterate over a Tuple (element by element) using Slicing---
---Traverse a Tuple (Element by Element) using for Loop with Slicing---
Error Occurred
---------------------------------------------------------------------------
---Approach IV - Iterate over a Tuple (element by element) using enumerate() Function---
---Traverse a Tuple (Element by Element) using for Loop with enumerate() Function---
0 Shahada
1 Prayer (salat)
2 Alms (zakat)
3 Fasting (sawm)
4 Pilgrimage (hajj)
- Example 3 - Traversing a Tuple (Element by Element)
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Traversing a Tuple (Element by Element)
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 traverse a Tuple (element by element) and display the result on the Output Screen
'''
try:
# Approach I - Iterate over a Tuple (element by element) using for loop
# Tuple Initialization
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
# Iterate over a Tuple (element by element) using for loop
print(" ---Approach I - Iterate over a Tuple (element by element) using for loop---")
print("---Traverse a Tuple (Element by Element) using for Loop---")
for element in pillars_of_human_engineering:
print(element)
except:
print('Error Occurred')
print("-------------------------------------------------------------------")
try:
# Approach II - Iterate over a Tuple (element by element) using Indexing
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
# Iterate over a Tuple (element by element) using for loop with Indexing
print(" ---Approach II - Iterate over a Tuple (element by element) using Indexing---")
print("---Traverse a Tuple (Element by Element) using for Loop with Indexing---")
for element in range(0, len(pillars_of_human_engineering)):
print("pillars_of_islam[",element,"]: " + pillars_of_human_engineering[element])
except:
print('Error Occurred')
print("---------------------------------------------------------------------------")
try:
# Approach III - Iterate over a Tuple (element by element) using Slicing
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
counter = 0
print("---Approach III - Iterate over a Tuple (element by element) using Slicing---")
# Iterate over a Tuple (element by element) using for loop with Slicing
print("---Traverse a Tuple (Element by Element) using for Loop with Slicing---")
# Slicing Operator => string[starting index : ending index : step value]
for element in pillars_of_human_engineering[0 : len(pillars_of_human_engineering): 1]:
counter = counter + 1
print("layers_of_jannah[",counter,"]: " + element)
except:
print('Error Occurred')
print("---------------------------------------------------------------------------")
try:
# Approach IV - Iterate over a Tuple (element by element) for loop using enumerate() Function
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
counter = 0
print("---Approach IV - Iterate over a Tuple (element by element) using enumerate() Function---")
# Iterate over a Tuple (element by element) using for loop using enumerate() Function
print("---Traverse a Tuple (Element by Element) using for Loop with enumerate() Function---")
# Iterate over a Tuple (element by element) using for loop using enumerate() Function
for count, element in enumerate(pillars_of_human_engineering):
print (count," ",element)
except:
print('Error Occurred')
---Approach I - Iterate over a Tuple (element by element) using for loop---
---Traverse a Tuple (Element by Element) using for Loop---
Truth
Honesty
Justice
-------------------------------------------------------------------
---Approach II - Iterate over a Tuple (element by element) using Indexing---
---Traverse a Tuple (Element by Element) using for Loop with Indexing---
pillars_of_islam[ 0 ]: Truth
pillars_of_islam[ 1 ]: Honesty
pillars_of_islam[ 2 ]: Justice
---------------------------------------------------------------------------
---Approach III - Iterate over a Tuple (element by element) using Slicing---
---Traverse a Tuple (Element by Element) using for Loop with Slicing---
layers_of_jannah[ 1 ]: Truth
layers_of_jannah[ 2 ]: Honesty
layers_of_jannah[ 3 ]: Justice
---------------------------------------------------------------------------
---Approach IV - Iterate over a Tuple (element by element) using enumerate() Function---
---Traverse a Tuple (Element by Element) using for Loop with enumerate() Function---
0 Truth
1 Honesty
2 Justice
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Tuples 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 Tuples and answer the questions given below
- Questions
- Traverse selected Tuples using following methods (as shown in this Chapter)
- Method 1: Traversing an Entire Tuple
- Method 2: Traversing a Tuple (Element by Element)
- Traverse selected Tuples using following methods (as shown in this Chapter)
Your Turn Task 1
- Task
- Consider two Tuples (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Traverse selected Tuples using following methods (as shown in this Chapter)
- Method 1: Traversing an Entire Tuple
- Method 2: Traversing a Tuple (Element by Element)
- Traverse selected Tuples using following methods (as shown in this Chapter)
Operation 4 – Searching
- Methods to Search Element(s) from a Tuple
- In Sha Allah, in next Slides, I will present a method to search element(s) from a Tuple
- Method 1: Search a Specific Element from a Tuple
- Example 1 – Search a Specific Element from a Tuple – Using in / not in Operator
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search a Specific Element from a Tuple
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 search a specific element from a Tuple
'''
# Approach I – Using in Operator to Search Element in a Tuple
try:
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print("---Approach I – Using in Operator to Search Element in a Tuple---")
element = 'Jannat-al-Kasif'
# Using in operator to Search Element in a Tuple
if element in layers_of_jannah:
print(element + " is in Tuple")
else:
print(element + " is not in Tuple")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Element in a Tuple
try:
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print("---Approach II – Using not in Operator to Search Element in a Tuple---")
element = 'Jannat-ul-Baqi'
# Using not in operator to Search Element in a Tuple
if element not in layers_of_jannah:
print(element + " is not in Tuple")
else:
print(element + " is in Tuple")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Element in a Tuple---
Jannat-al-Kasif is in Tuple
-------------------------------------------
---Approach II – Using not in Operator to Search Element in a Tuple---
Jannat-ul-Baqi is not in Tuple
- Example 2 – Search a Specific Element from a Tuple – Using in / not in Operator
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search a Specific Element from a Tuple
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 search a specific element from a Tuple
'''
# Approach I – Using in Operator to Search Element in a Tuple
try:
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
print("---Approach I – Using in Operator to Search Element in a Tuple---")
element = 'Alms (zakat)'
# Using in operator to Search Element in a Tuple
if element in pillars_of_islam:
print(element + " is in Tuple")
else:
print(element + " is not in Tuple")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Element in a Tuple
try:
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
print("---Approach II – Using not in Operator to Search Element in a Tuple---")
element = '(sawm)'
# Using not in operator to Search Element in a Tuple
if element not in pillars_of_islam:
print(element + " is not in Tuple")
else:
print(element + " is in Tuple")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Element in a Tuple---
Alms (zakat) is in Tuple
-------------------------------------------
---Approach II – Using not in Operator to Search Element in a Tuple---
(sawm) is not in Tuple
- Example 3 – Search a Specific Element from a Tuple – Using in / not in Operator
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search a Specific Element from a Tuple
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 search a specific element from a Tuple
'''
# Approach I – Using in Operator to Search Element in a Tuple
try:
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print("---Approach I – Using in Operator to Search Element in a Tuple---")
element = 'Honesty'
# Using in operator to Search Element in a Tuple
if element in pillars_of_human_engineering:
print(element + " is in Tuple")
else:
print(element + " is not in Tuple")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Element in a Tuple
try:
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print("---Approach II – Using not in Operator to Search Element in a Tuple---")
element = 'Justice'
# Using not in operator to Search Element in a Tuple
if element not in pillars_of_human_engineering:
print(element + " is not in Tuple")
else:
print(element + " is in Tuple")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Element in a Tuple---
Honesty is in Tuple
-------------------------------------------
---Approach II – Using not in Operator to Search Element in a Tuple---
Justice is in Tuple
Search Specific Elements from a Tuple
- Example 1 – Search Specific Elements from a Tuple – Using in / not in
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search Specific Elements from a Tuple
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 search specific elements from a Tuple and display the result on the Output Screen
'''
# Approach I – Using in Operator to Search Element in a Tuple
try:
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print("---Approach I – Using in Operator to Search Element in a Tuple---")
element1 = 'Dar-al-Khuld'
element2 = 'Jannat-ul-Baqi'
# Using in operator to Search Element in a Tuple
if (element1 in layers_of_jannah) and (element2 in layers_of_jannah):
print("Elements are in Tuple")
else:
print("Elements are not in Tuple")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Element in a Tuple
try:
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print("---Approach II – Using not in Operator to Search Element in a Tuple---")
element1 = 'Jannat-al-Naeem'
element2 = 'Dar-al-Salam'
# Using not in operator to Search Element in a Tuple
if (element1 not in layers_of_jannah) and (element2 not in layers_of_jannah):
print("Elements are not in Tuple")
else:
print("Elements are in Tuple")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Element in a Tuple---
Elements are not in Tuple
-------------------------------------------
---Approach II – Using not in Operator to Search Element in a Tuple---
Elements are in Tuple
- Example 2 – Search Specific Elements from a Tuple – Using in / not in Operator
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search Specific Elements from a Tuple
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 search a specific elements from a Tuple and display the result on the Output Screen
'''
# Approach I – Using in Operator to Search Element in a Tuple
try:
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
print("---Approach I – Using in Operator to Search Element in a Tuple---")
element1 = 'Shahada'
element2 = 'Zakat'
# Using in operator to Search Element in a Tuple
if (element1 in pillars_of_islam) or (element2 in pillars_of_islam):
print("Elements are in Tuple")
else:
print("Elements are not in Tuple")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Element in a Tuple
try:
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
print("---Approach II – Using not in Operator to Search Element in a Tuple---")
element1 = 'Prayer (salat)'
element2 = 'Pilgrimage (hajj)'
# Using not in operator to Search Element in a Tuple
if (element1 not in pillars_of_islam) and (element2 not in pillars_of_islam):
print("Elements are not in Tuple")
else:
print("Elements are in Tuple")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Element in a Tuple---
Elements are in Tuple
-------------------------------------------
---Approach II – Using not in Operator to Search Element in a Tuple---
Elements are in Tuple
- Example 3 – Search Specific Elements from a Tuple – Using in / not in Operator
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search Specific Elements from a Tuple
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 search a specific elements from a Tuple and display the result on the Output Screen
'''
# Approach I – Using in Operator to Search Element in a Tuple
try:
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print("---Approach I – Using in Operator to Search Element in a Tuple---")
element1 = 'Trust'
element2 = 'Truth'
# Using in operator to Search Element in a Tuple
if (element1 in pillars_of_human_engineering) or (element2 in pillars_of_human_engineering):
print("Elements are in Tuple")
else:
print("Elements are not in Tuple")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Element in a Tuple
try:
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print("---Approach II – Using not in Operator to Search Element in a Tuple---")
element1 = 'Hardwork'
element2 = 'Honesty'
# Using not in operator to Search Element in a Tuple
if (element1 not in pillars_of_human_engineering) or (element2 not in pillars_of_human_engineering):
print("Elements are not in Tuple")
else:
print("Elements are in Tuple")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Element in a Tuple---
Elements are in Tuple
-------------------------------------------
---Approach II – Using not in Operator to Search Element in a Tuple---
Elements are not in Tuple
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Tuples 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 Tuples and answer the questions given below
- Questions
- Search from selected Tuples using following methods (as shown in this Chapter)
- Method 1: Search Specific Character from a Tuple
Your Turn Task 1
- Task
- Consider two Tuples (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Search from selected Tuples using following methods (as shown in this Chapter)
- Method 1: Search Specific Character from a Tuple
- Search from selected Tuples using following methods (as shown in this Chapter)
Operation 5 – Sorting
- Methods to Sorting a Tuple
- In Sha Allah, in next Slides, I will present three methods of sorting a Tuple
- Method 1: Sort a Tuple Alphabetically
- Method 2: Sort a Tuple in Ascending Order
- Method 3: Sort a Tuple in Descending Order
- sorted() Function
- Function Name
- sort()
- Syntax
tuple.sort([func])
OR
tuple.sort(key, reverse)
- Purpose
- The main purpose of sort() function is to
- Alphabetically sort a Tuple
- The main purpose of sort() function is to
Sort a Tuple Alphabetically
- Example 1 – Sort a Tuple Alphabetically – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Tuple 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 Tuple alphabetically
'''
try:
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print('---Original Tuple before using sort() Function---')
print(layers_of_jannah)
# Convert Tuple to List using list() Function
layers_of_jannah = list(layers_of_jannah)
# Using sort() Function to sort a Tuple Alphabetically
layers_of_jannah.sort()
print('---New Tuple after using sort() Function---')
print(layers_of_jannah)
except:
print('Error Occurred')
---Original Tuple before using sort() Function---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
---New Tuple after using sort() Function---
['Dar-al-Khuld', 'Dar-al-Salam', 'Jannat-al-Adan', 'Jannat-al-Firdous', 'Jannat-al-Kasif', 'Jannat-al-Maawa', 'Jannat-al-Naeem', 'Jannat-ul-Maqaam']
- Example 2 – Sort a Tuple Alphabetically – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Tuple 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 Tuple alphabetically
'''
try:
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
print('---Original Tuple before using sort() Function---')
print(pillars_of_islam)
# Convert Tuple to List using list() Function
pillars_of_islam = list(pillars_of_islam)
# Using sort() Function to sort a Tuple Alphabetically
pillars_of_islam.sort()
print('---New Tuple after using sort() Function---')
print(pillars_of_islam)
except:
print('Error Occurred')
---Original Tuple before using sort() Function---
('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
---New Tuple after using sort() Function---
['Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)', 'Prayer (salat)', 'Shahada']
- Example 3 – Sort a Tuple Alphabetically – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Tuple 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 Tuple alphabetically
'''
try:
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print('---Original Tuple before using sort() Function---')
print(pillars_of_human_engineering)
# Convert Tuple to List using list() Function
pillars_of_human_engineering = list(pillars_of_human_engineering)
# Using sort() Function to sort a Tuple Alphabetically
pillars_of_human_engineering.sort()
print('---New Tuple after using sort() Function---')
print(pillars_of_human_engineering)
except:
print('Error Occurred')
---Original Tuple before using sort() Function---
('Truth', 'Honesty', 'Justice')
---New Tuple after using sort() Function---
['Honesty', 'Justice', 'Truth']
Sort a Tuple in Ascending Order
- Example 1 – Sort a Tuple in Ascending Order – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Tuple 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 Tuple in ascending order
'''
try:
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print('---Original Tuple before using sort() Function---')
print(layers_of_jannah)
# Convert Tuple to List using list() Function
layers_of_jannah = list(layers_of_jannah)
# Using sort() Function to sort a Tuple in Ascending Order
layers_of_jannah.sort(reverse = False)
print('---New Tuple after using sort() Function---')
print(layers_of_jannah)
except:
print('Error Occurred')
---Original Tuple before using sort() Function---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
---New Tuple after using sort() Function---
['Dar-al-Khuld', 'Dar-al-Salam', 'Jannat-al-Adan', 'Jannat-al-Firdous', 'Jannat-al-Kasif', 'Jannat-al-Maawa', 'Jannat-al-Naeem', 'Jannat-ul-Maqaam']
- Example 2 – Sort a Tuple in Ascending Order – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Tuple 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 Tuple in ascending order
'''
try:
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
print('---Original Tuple before using sort() Function---')
print(pillars_of_islam)
# Convert Tuple to List using list() Function
pillars_of_islam = list(pillars_of_islam)
# Using sort() Function to sort a Tuple in Ascending Order
pillars_of_islam.sort(reverse = False)
print('---New Tuple after using sort() Function---')
print(pillars_of_islam)
except:
print('Error Occurred')
---Original Tuple before using sort() Function---
('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
---New Tuple after using sort() Function---
['Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)', 'Prayer (salat)', 'Shahada']
- Example 3 – Sort a Tuple in Ascending Order – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Tuple 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 Tuple in ascending order
'''
try:
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print('---Original Tuple before using sort() Function---')
print(pillars_of_human_engineering)
# Convert Tuple to List using list() Function
pillars_of_human_engineering = list(pillars_of_human_engineering)
# Using sort() Function to sort a Tuple in Ascending Order
pillars_of_human_engineering.sort(reverse = False)
print('---New Tuple after using sort() Function---')
print(pillars_of_human_engineering)
except:
print('Error Occurred')
---Original Tuple before using sort() Function---
('Truth', 'Honesty', 'Justice')
---New Tuple after using sort() Function---
['Honesty', 'Justice', 'Truth']
Sort a Tuple in Descending Order
- Example 1 – Sort a Tuple in Descending Order – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Tuple 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 Tuple in descending order
'''
try:
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print('---Original Tuple before using sort() Function---')
print(layers_of_jannah)
# Convert Tuple to List using list() Function
layers_of_jannah = list(layers_of_jannah)
# Using sort() Function to sort a Tuple in Descending Order
layers_of_jannah.sort(reverse = True)
print('---New Tuple after using sort() Function---')
print(layers_of_jannah)
except:
print('Error Occurred')
---Original Tuple before using sort() Function---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
---New Tuple after using sort() Function---
['Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Maawa', 'Jannat-al-Kasif', 'Jannat-al-Firdous', 'Jannat-al-Adan', 'Dar-al-Salam', 'Dar-al-Khuld']
- Example 2 – Sort a Tuple in Descending Order – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Tuple 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 Tuple in descending order
'''
try:
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
print('---Original Tuple before using sort() Function---')
print(pillars_of_islam)
# Convert Tuple to List using list() Function
pillars_of_islam = list(pillars_of_islam)
# Using sort() Function to sort a Tuple in Descending Order
pillars_of_islam.sort(reverse = True)
print('---New Tuple after using sort() Function---')
print(pillars_of_islam)
except:
print('Error Occurred')
---Original Tuple before using sort() Function---
('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
---New Tuple after using sort() Function---
['Shahada', 'Prayer (salat)', 'Pilgrimage (hajj)', 'Fasting (sawm)', 'Alms (zakat)']
- Example 3 – Sort a Tuple in Descending Order – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a Tuple 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 Tuple in descending order
'''
try:
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print('---Original Tuple before using sort() Function---')
print(pillars_of_human_engineering)
# Convert Tuple to List using list() Function
pillars_of_human_engineering = list(pillars_of_human_engineering)
# Using sort() Function to sort a Tuple in Descending Order
pillars_of_human_engineering.sort(reverse = True)
print('---New Tuple after using sort() Function---')
print(pillars_of_human_engineering)
except:
print('Error Occurred')
---Original Tuple before using sort() Function---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
---New Tuple after using sort() Function---
['Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Maawa', 'Jannat-al-Kasif', 'Jannat-al-Firdous', 'Jannat-al-Adan', 'Dar-al-Salam', 'Dar-al-Khuld']
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Tuples and answer the questions given below
- (‘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 Tuples and answer the questions given below
- Questions
- Sort Elements of the selected Tuples using following methods (as shown in this Chapter)
- Method 1: Sort a Tuple Alphabetically
- Method 2: Sort a Tuple in Ascending Order
- Method 3: Sort a Tuple in Descending Order
- Sort Elements of the selected Tuples using following methods (as shown in this Chapter)
Your Turn Task 1
- Task
- Consider two Tuples (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Sort Elements of the selected Tuples using following methods (as shown in this Chapter)
- Method 1: Sort a Tuple Alphabetically
- Method 2: Sort a Tuple in Ascending Order
- Method 3: Sort a Tuple in Descending Order
- Sort Elements of the selected Tuples using following methods (as shown in this Chapter)
Operation 6 – Merging
- Methods of Merging Tuples
- In Sha Allah, in next Slides, I will present four methods to merge / concatenate Tuples
- Method 1: Concatenate Tuples using + Operator
- Method 2: Concatenate Tuples using * Operator
- Method 3: Merge Tuples using append() Function
- Method 4: Merge Tuples using extend() Function
- extend() Function
- Function Name
- extend()
- Syntax
Tuple.extend(seq)
- Purpose
- The main purpose of extend() function is to
- appends the contents of seq to Tuple
- The main purpose of extend() function is to
Concatenate Tuples using + Operator
- Example 1 – Concatenate Tuples - Using + Operator
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Concatenate Tuples using + Operator
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 concatenate Tuples using + operator
'''
try:
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
jannah_meaning = ('Home','Garden','Essence place to stay','Living in Wealth','Garden of the revealer','Home of Peace','everlasting','Gardens of Everlasting Bliss')
print('---Original Tuples before Merging---')
print(layers_of_jannah)
print(jannah_meaning)
# Using + Operator to Concatenate Tuples
print('---New Tuple after Merging---')
for count in range(len(layers_of_jannah)):
merge_layers = layers_of_jannah[count] + " - " + str(jannah_meaning[count])
print(merge_layers)
except:
print("Error Occurred")
---Original Tuples before Merging---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
('Home', 'Garden', 'Essence place to stay', 'Living in Wealth', 'Garden of the revealer', 'Home of Peace', 'everlasting', 'Gardens of Everlasting Bliss')
---New Tuple after Merging---
Jannat-al-Maawa - Home
Jannat-al-Firdous - Garden
Jannat-ul-Maqaam - Essence place to stay
Jannat-al-Naeem - Living in Wealth
Jannat-al-Kasif - Garden of the revealer
Dar-al-Salam - Home of Peace
Dar-al-Khuld - everlasting
Jannat-al-Adan - Gardens of Everlasting Bliss
Concatenate Tuples using * Operator
- Example 1 – Concatenate Tuples - Using * Operator
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Concatenate Tuples using * Operator
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 concatenate Tuples using * operator
'''
try:
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
jannah_meaning = ('Home','Garden','Essence place to stay','Living in Wealth','Garden of the revealer','Home of Peace','everlasting','Gardens of Everlasting Bliss')
print('---Original Tuples before Merging---')
print(layers_of_jannah)
print(jannah_meaning)
# Using * Operator to Concatenate Tuples
print('---New Tuple after Merging---')
for count in range(len(layers_of_jannah)):
merge_layers = [*(layers_of_jannah[count], jannah_meaning[count])]
print(merge_layers)
except:
print("Error Occurred")
---Original Tuples before Merging---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
('Home', 'Garden', 'Essence place to stay', 'Living in Wealth', 'Garden of the revealer', 'Home of Peace', 'everlasting', 'Gardens of Everlasting Bliss')
---New Tuple after Merging---
['Jannat-al-Maawa', 'Home']
['Jannat-al-Firdous', 'Garden']
['Jannat-ul-Maqaam', 'Essence place to stay']
['Jannat-al-Naeem', 'Living in Wealth']
['Jannat-al-Kasif', 'Garden of the revealer']
['Dar-al-Salam', 'Home of Peace']
['Dar-al-Khuld', 'everlasting']
['Jannat-al-Adan', 'Gardens of Everlasting Bliss']
Merge Tuples using append() Function
- Example 1 – Merge Tuples - Using append() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Merge Tuples using append() 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 merge Tuples using append() function
'''
try:
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
jannah_meaning = ('Home','Garden','Essence place to stay','Living in Wealth','Garden of the revealer','Home of Peace','everlasting','Gardens of Everlasting Bliss')
print('---Original Tuples before Merging---')
print(layers_of_jannah)
print(jannah_meaning)
# Convert Tuple to List using list
layers_of_jannah = list(layers_of_jannah)
# Using append() Function to Concatenate Tuples
print('---New Tuple after Merging---')
for element in jannah_meaning:
layers_of_jannah.append(element)
print(layers_of_jannah)
except:
print("Error Occurred")
---Original Tuples before Merging---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
('Home', 'Garden', 'Essence place to stay', 'Living in Wealth', 'Garden of the revealer', 'Home of Peace', 'everlasting', 'Gardens of Everlasting Bliss')
---New Tuple after Merging---
['Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan', 'Home', 'Garden', 'Essence place to stay', 'Living in Wealth', 'Garden of the revealer', 'Home of Peace', 'everlasting', 'Gardens of Everlasting Bliss']
Merge Tuples using extend() Function
- Example 1 – Merge Tuples - Using extend() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Merge Tuples using extend() 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 merge Tuples using extend()
'''
try:
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
jannah_meaning = ('Home','Garden','Essence place to stay','Living in Wealth','Garden of the revealer','Home of Peace','everlasting','Gardens of Everlasting Bliss')
print('---Original Tuple before Merging---')
print(layers_of_jannah)
print(jannah_meaning)
# Convert Tuple to List using list
layers_of_jannah = list(layers_of_jannah)
# Using extend() Function to Concatenate Tuples
print('---New Tuple after Merging---')
layers_of_jannah.extend(jannah_meaning)
print(layers_of_jannah)
except:
print("Error Occurred")
---Original Tuple before Merging---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
('Home', 'Garden', 'Essence place to stay', 'Living in Wealth', 'Garden of the revealer', 'Home of Peace', 'everlasting', 'Gardens of Everlasting Bliss')
---New Tuple after Merging---
['Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan', 'Home', 'Garden', 'Essence place to stay', 'Living in Wealth', 'Garden of the revealer', 'Home of Peace', 'everlasting', 'Gardens of Everlasting Bliss']
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Tuples and answer the questions given below
- (‘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 Tuples and answer the questions given below
- Questions
- Merge Elements of the selected Tuples using following methods (as shown in this Chapter)
- Method 1: Concatenate Tuples using + Operator
- Method 2: Concatenate Tuples using * Operator
- Method 3: Merge Tuples using append() Function
- Method 4: Merge Tuples using extend() Function
- Merge Elements of the selected Tuples using following methods (as shown in this Chapter)
Your Turn Task 1
- Task
- Consider two Tuples (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Merge Elements of the selected Tuples using following methods (as shown in this Chapter)
- Method 1: Concatenate Tuples using + Operator
- Method 2: Concatenate Tuples using * Operator
- Method 3: Merge Tuples using append() Function
- Method 4: Merge Tuples using extend() Function
- Merge Elements of the selected Tuples using following methods (as shown in this Chapter)
Operation 7 – Updation
- Methods of Updating a Tuple
- In Sha Allah, in next Slides, I will present two methods to update a Tuple
- Method 1: Update Entire Tuple using Slicing
- Method 2: Update Tuple Element by Element using Indexing
Update Tuple Element by Element using Slicing
- Update Entire Tuple using Slicing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Update Entire Tuple using Slicing
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 entire Tuple using Slicing
'''
try:
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer', 'Alms', 'Fasting', 'Pilgrimage')
print('---Original Tuple before Updating---')
print(pillars_of_islam)
# Convert Tuple to List using list
pillars_of_islam = list(pillars_of_islam)
# Update Entire Tuple using Slicing
print('---New Tuple after Updating---')
pillars_of_islam[0:5] = ['Shahadat', 'Salat', 'Zakat', 'Sawm', 'Hajj']
print(pillars_of_islam)
except:
print("Error Occurred")
---Original Tuple before Updating---
('Shahada', 'Prayer', 'Alms', 'Fasting', 'Pilgrimage')
---New Tuple after Updating---
['Shahadat', 'Salat', 'Zakat', 'Sawm', 'Hajj']
- Example 2 – Update Entire Tuple - Using Slicing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Update Entire Tuple using Slicing
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 entire Tuple using Slicing
'''
try:
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print('---Original Tuple before Updating---')
print(pillars_of_human_engineering)
# Convert Tuple to List using list
pillars_of_human_engineering = list(pillars_of_human_engineering)
# Update Entire Tuple using Slicing
print('---New Tuple after Updating---')
pillars_of_human_engineering[0:3] = ['Truthfulness', 'Integrity', 'Equity']
print(pillars_of_human_engineering)
except:
print("Error Occurred")
---Original Tuple before Updating---
('Truth', 'Honesty', 'Justice')
---New Tuple after Updating---
['Truthfulness', 'Integrity', 'Equity']
Update Tuple Element by Element using Indexing
- Example 1 – Update Tuple Element by Element - Using Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Update Tuple Element by Element using Indexing
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 Tuple element by element using indexing
'''
try:
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer', 'Alms', 'Fasting', 'Pilgrimage')
print('---Original Tuple before Updating---')
print(pillars_of_islam)
# Convert Tuple to List using list
pillars_of_islam = list(pillars_of_islam)
# Update Entire Tuple using Indexing
print('---New Tuple after Updating---')
pillars_of_islam[0] = 'Shahadat'
pillars_of_islam[1] = 'Salat'
pillars_of_islam[2] = 'Zakat'
pillars_of_islam[3] = 'Sawm'
pillars_of_islam[4] = 'Hajj'
print(pillars_of_islam)
except:
print("Error Occurred")
---Original Tuple before Updating---
('Shahada', 'Prayer', 'Alms', 'Fasting', 'Pilgrimage')
---New Tuple after Updating---
['Shahadat', 'Salat', 'Zakat', 'Sawm', 'Hajj']
- Example 2 – Update Tuple Element by Element - Using Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Update Tuple Element by Element using Indexing
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 Tuple element by element using indexing
'''
try:
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print('---Original Tuple before Updating---')
print(pillars_of_human_engineering)
# Convert Tuple to List using list
pillars_of_human_engineering = list(pillars_of_human_engineering)
# Update Entire Tuple using Slicing
print('---New Tuple after Updating---')
pillars_of_human_engineering[0] = 'Truthfulness'
pillars_of_human_engineering[1] = 'Integrity'
pillars_of_human_engineering[2] = 'Equity'
print(pillars_of_human_engineering)
except:
print("Error Occurred")
---Original Tuple before Updating---
('Truth', 'Honesty', 'Justice')
---New Tuple after Updating---
['Truthfulness', 'Integrity', 'Equity']
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Tuples and answer the questions given below
- (‘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 Tuples and answer the questions given below
- Questions
- Update Elements of the selected Tuples using following methods (as shown in this Chapter)
- Method 1: Update Entire Tuple using Slicing
- Method 2: Update Tuple Element by Element using Indexing
- Update Elements of the selected Tuples using following methods (as shown in this Chapter)
Your Turn Task 1
- Task
- Consider two Strings (similar to the ones given in the TODO Task) and answer the questions given below.
- Questions
- Update Elements of the selected Tuples using following methods (as shown in this Chapter)
- Method 1: Update Entire Tuple using Slicing
- Method 2: Update Tuple Element by Element using Indexing
- Update Elements of the selected Tuples using following methods (as shown in this Chapter)
Operation 8 – Deletion
- Methods to Delete Character from a Tuple
- In Sha Allah, in next Slides, I will present four methods to delete a Tuple
- Method 1: Delete Entire Tuple using del Function
- Method 2: Remove All Elements of a Tuple using clear() Function
- Method 3: Remove Specific Element from a Tuple using remove() Function
- Method 4: Remove an Element from a Tuple using pop() Function
- del Function
- Function Name
- del
- Syntax
del Tuple
- Purpose
- The main purpose of del Function is to
- delete a single element, multiple elements, or the entire Tuple
- The main purpose of del Function is to
- clear() Function
- Function Name
- clear()
- Syntax
tuple.clear()
- Purpose
- The main purpose of clear() Function is to
- removes all items from the Tuple
- The main purpose of clear() Function is to
- clear() Function
- Function Name
- clear()
- Syntax
tuple.clear()
- Purpose
- The main purpose of clear() Function is to
- removes all items from the Tuple
- The main purpose of clear() Function is to
- remove() Function
- Function Name
- remove()
- Syntax
Tuple.remove(obj)
- Purpose
- The main purpose of del Function is to
- search for the given element in a Tuple and removes the first matching element
- The main purpose of del Function is to
- pop() Function
- Function Name
- pop()
- Syntax
Tuple.pop(obj = Tuple[-1])
- Purpose
- The main purpose of del Function is to
- removes an item at the specified index from the Tuple
- The main purpose of del Function is to
Delete Entire Tuple using del Function
- Example 1 – Delete Entire Tuple - Using del
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Delete Entire Tuple - Using del
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 delete entire Tuple using del
'''
# Approach I – Delete an Entire Tuple using del Function
try:
print("---Approach I – Delete an Entire Tuple using del Function---")
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print('---Original Tuple before using del Function---')
print(layers_of_jannah)
# Delete Entire Tuple using del Function
print('---New Tuple after using del Function---')
del layers_of_jannah
print(layers_of_jannah)
except Exception as ex:
print(ex)
print("-------------------------------------------")
# Approach II – Delete a Single Element from a Tuple using del Function
try:
print("---Approach II – Delete a Single Element from a Tuple using del Function---")
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print('---Original Tuple before using del Function---')
print(layers_of_jannah)
# Convert Tuple to List
layers_of_jannah = list(layers_of_jannah)
# Delete a Single Element using del
print('---New Tuple after using del Function---')
del layers_of_jannah[0]
print(layers_of_jannah)
except:
print("Error Occurred")
print("-------------------------------------------")
# Approach III – Delete Multiple Elements from a Tuple using del Function
try:
print("Approach III – Delete Multiple Elements from a Tuple using del Function")
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print('---Original Tuple before using del Function---')
print(layers_of_jannah)
# Convert Tuple to List
layers_of_jannah = list(layers_of_jannah)
# Delete a Multiple Elements using del
print('---New Tuple after using del Function---')
del layers_of_jannah [1:5]
print(layers_of_jannah)
except:
print("Error Occurred")
---Approach I – Delete an Entire Tuple using del Function---
---Original Tuple before using del Function---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
---New Tuple after using del Function---
name 'layers_of_jannah' is not defined
-------------------------------------------
---Approach II – Delete a Single Element from a Tuple using del Function---
---Original Tuple before using del Function---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
---New Tuple after using del Function---
['Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan']
-------------------------------------------
Approach III – Delete Multiple Elements from a Tuple using del Function
---Original Tuple before using del Function---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
---New Tuple after using del Function---
['Jannat-al-Maawa', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan']
- Example 2 – Delete Entire Tuple - Using del
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Delete Entire Tuple - Using del
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 delete entire Tuple using del
'''
# Approach I – Delete an Entire Tuple using del Function
try:
print("---Approach I – Delete an Entire Tuple using del Function---")
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
print('---Original Tuple before using del Function---')
print(pillars_of_islam)
# Delete Entire Tuple using del Function
print('---New Tuple after using del Function---')
del pillars_of_islam
print(pillars_of_islam)
except Exception as ex:
print(ex)
print("-------------------------------------------")
# Approach II – Delete a Single Element from a Tuple using del Function
try:
print("---Approach II – Delete a Single Element from a Tuple using del Function---")
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
print('---Original Tuple before using del Function---')
print(pillars_of_islam)
# Convert Tuple to List
pillars_of_islam = list(pillars_of_islam)
# Delete a Single Element using del
print('---New Tuple after using del Function---')
del pillars_of_islam[0]
print(pillars_of_islam)
except:
print("Error Occurred")
print("-------------------------------------------")
# Approach III – Delete Multiple Elements from a Tuple using del Function
try:
print("Approach III – Delete Multiple Elements from a Tuple using del Function")
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
print('---Original Tuple before using del Function---')
print(pillars_of_islam)
# Convert Tuple to List
pillars_of_islam = list(pillars_of_islam)
# Delete a Multiple Elements using del
print('---New Tuple after using del Function---')
del pillars_of_islam[1:5]
print(pillars_of_islam)
except:
print("Error Occurred")
---Approach I – Delete an Entire Tuple using del Function---
---Original Tuple before using del Function---
('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
---New Tuple after using del Function---
name 'pillars_of_islam' is not defined
-------------------------------------------
---Approach II – Delete a Single Element from a Tuple using del Function---
---Original Tuple before using del Function---
('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
---New Tuple after using del Function---
['Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)']
-------------------------------------------
Approach III – Delete Multiple Elements from a Tuple using del Function
---Original Tuple before using del Function---
('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
---New Tuple after using del Function---
['Shahada']
- Example 3 – Delete Entire Tuple - Using del
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Delete Entire Tuple - Using del
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 delete entire Tuple using del
'''
# Approach I – Delete an Entire Tuple using del Function
try:
print("---Approach I – Delete an Entire Tuple using del Function---")
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print('---Original Tuple before using del Function---')
print(pillars_of_human_engineering)
# Delete Entire Tuple using del Function
print('---New Tuple after using del Function---')
del pillars_of_human_engineering
print(pillars_of_human_engineering)
except Exception as ex:
print(ex)
print("-------------------------------------------")
# Approach II – Delete a Single Element from a Tuple using del Function
try:
print("---Approach II – Delete a Single Element from a Tuple using del Function---")
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print('---Original Tuple before using del Function---')
print(pillars_of_human_engineering)
# Convert Tuple to List
pillars_of_human_engineering = list(pillars_of_human_engineering)
# Delete a Single Element using del
print('---New Tuple after using del Function---')
del pillars_of_human_engineering[0]
print(pillars_of_human_engineering)
except:
print("Error Occurred")
print("-------------------------------------------")
# Approach III – Delete Multiple Elements from a Tuple using del Function
try:
print("Approach III – Delete Multiple Elements from a Tuple using del Function")
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print('---Original Tuple before using del Function---')
print(pillars_of_human_engineering)
# Convert Tuple to List
pillars_of_human_engineering = list(pillars_of_human_engineering)
# Delete a Multiple Elements using del
print('---New Tuple after using del Function---')
del pillars_of_human_engineering[1:3]
print(pillars_of_human_engineering)
except:
print("Error Occurred")
---Approach I – Delete an Entire Tuple using del Function---
---Original Tuple before using del Function---
('Truth', 'Honesty', 'Justice')
---New Tuple after using del Function---
name 'pillars_of_human_engineering' is not defined
-------------------------------------------
---Approach II – Delete a Single Element from a Tuple using del Function---
---Original Tuple before using del Function---
('Truth', 'Honesty', 'Justice')
---New Tuple after using del Function---
['Honesty', 'Justice']
-------------------------------------------
Approach III – Delete Multiple Elements from a Tuple using del Function
---Original Tuple before using del Function---
('Truth', 'Honesty', 'Justice')
---New Tuple after using del Function---
['Truth']
Remove Entire Tuple using clear() Function
- Example 1 – Remove Entire Tuple - Using clear() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Entire Tuple
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 entire Tuple
'''
try:
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print('---Original Tuple before using clear() Function---')
print(layers_of_jannah)
# Convert Tuple to List
layers_of_jannah = list(layers_of_jannah)
# Remove Entire Tuple using clear() Function
print('---New Tuple after using clear() Function---')
layers_of_jannah.clear()
print(layers_of_jannah)
except:
print("Error Occurred")
---Original Tuple before using clear() Function---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
---New Tuple after using clear() Function---
[]
- Example 2 – Remove Entire Tuple - Using clear() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Entire Tuple 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 entire Tuple
'''
try:
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
print('---Original Tuple before using clear() Function---')
print(pillars_of_islam)
# Convert Tuple to List
pillars_of_islam = list(pillars_of_islam)
# Remove Entire Tuple using clear() Function
print('---New Tuple after using clear() Function---')
pillars_of_islam.clear()
print(pillars_of_islam)
except:
print("Error Occurred")
---Original Tuple before using clear() Function---
('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
---New Tuple after using clear() Function---
[]
- Example 3 – Remove Entire Tuple - Using clear() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Entire Tuple 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 entire Tuple
'''
try:
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print('---Original Tuple before using clear() Function---')
print(pillars_of_human_engineering)
# Convert Tuple to List
pillars_of_human_engineering = list(pillars_of_human_engineering)
# Remove Entire Tuple using clear() Function
print('---New Tuple after using clear() Function---')
pillars_of_human_engineering.clear()
print(pillars_of_human_engineering)
except:
print("Error Occurred")
---Original Tuple before using clear() Function---
('Truth', 'Honesty', 'Justice')
---New Tuple after using clear() Function---
[]
Remove Specific Element from a Tuple using remove() Function
- Example 1 – Remove Specific Element from a Tuple - Using remove() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific Element from the Tuple
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 element from a Tuple
'''
try:
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print('---Original Tuple before using remove() Function---')
print(layers_of_jannah)
# Convert Tuple to List
layers_of_jannah = list(layers_of_jannah)
# Remove Specific Element from a Tuple using remove() Function
print('---New Tuple after using remove() Function---')
layers_of_jannah.remove('Dar-al-Salam')
print(layers_of_jannah)
except:
print("Error Occurred")
---Original Tuple before using remove() Function---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
---New Tuple after using remove() Function---
['Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Khuld', 'Jannat-al-Adan']
- Example 2 – Remove Specific Element from a Tuple - Using remove() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific Element from the Tuple
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 element from a Tuple
'''
try:
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
print('---Original Tuple before using remove() Function---')
print(pillars_of_islam)
# Convert Tuple to List
pillars_of_islam = list(pillars_of_islam)
# Remove Specific Element from a Tuple using remove() Function
print('---New Tuple after using remove() Function---')
pillars_of_islam.remove('Shahada')
print(pillars_of_islam)
except:
print("Error Occurred")
---Original Tuple before using remove() Function---
('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
---New Tuple after using remove() Function---
['Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)']
- Example 3 – Remove Specific Element from a Tuple - Using remove() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific Element from the Tuple
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 element from a Tuple
'''
try:
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print('---Original Tuple before using remove() Function---')
print(pillars_of_human_engineering)
# Convert Tuple to List
pillars_of_human_engineering = list(pillars_of_human_engineering)
# Remove Specific Element from a Tuple using remove() Function
print('---New Tuple after using remove() Function---')
pillars_of_human_engineering.remove('Honesty')
print(pillars_of_human_engineering)
except:
print("Error Occurred")
---Original Tuple before using remove() Function---
('Truth', 'Honesty', 'Justice')
---New Tuple after using remove() Function---
['Truth', 'Justice']
Remove Specific / Last Element from a Tuple using pop() Function
- Example 1 – Remove Specific / Last Element from a Tuple - Using pop() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific / Last Element from the Tuple
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 / last element from a Tuple
'''
# Approach I - Remove Specific Element from a Tuple using pop() Function
try:
print('--- Approach I - Remove Specific Element from a Tuple using pop() Function---')
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print('---Original Tuple before using pop() Function---')
print(layers_of_jannah)
# Convert Tuple to List
layers_of_jannah = list(layers_of_jannah)
# Remove Specific Element from a Tuple using pop() Function
print('---New Tuple after using pop() Function---')
layers_of_jannah.pop(3)
print(layers_of_jannah)
except:
print("Error Occurred")
print("-------------------------------------------")
# Approach II - Remove Last Element from a Tuple using pop() Function
try:
print('--- Approach II - Remove Last Element from a Tuple using pop() Function---')
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print('---Original Tuple using pop() Function---')
print(layers_of_jannah)
# Convert Tuple to List
layers_of_jannah = list(layers_of_jannah)
# Remove Last Element from a Tuple using pop() Function
print('---New Tuple after using pop() Function---')
layers_of_jannah.pop()
print(layers_of_jannah)
except:
print("Error Occurred")
--- Approach I - Remove Specific Element from a Tuple using pop() Function---
---Original Tuple before using pop() Function---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
---New Tuple after using pop() Function---
['Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan']
-------------------------------------------
--- Approach II - Remove Last Element from a Tuple using pop() Function---
---Original Tuple using pop() Function---
('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
---New Tuple after using pop() Function---
['Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld']
- Example 2 – Remove Specific / Last Element from a Tuple - Using pop() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific / Last Element from the Tuple
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 / last element from a Tuple
'''
# Approach I - Remove Specific Element from a Tuple using pop() Function
try:
print('--- Approach I - Remove Specific Element from a Tuple using pop() Function---')
# Initialize Tuple
pillars_of_islam = ('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
print('---Original Tuple before using pop() Function---')
print(pillars_of_islam)
# Convert Tuple to List
pillars_of_islam = list(pillars_of_islam)
# Remove Specific Element from a Tuple using pop() Function
print('---New Tuple after using pop() Function---')
pillars_of_islam.pop(3)
print(pillars_of_islam)
except:
print("Error Occurred")
print("-------------------------------------------")
# Approach II - Remove Last Element from a Tuple using pop() Function
try:
print('--- Approach II - Remove Last Element from a Tuple using pop() Function---')
# Initialize Tuple
layers_of_jannah = ('Jannat-al-Maawa', 'Jannat-al-Firdous', 'Jannat-ul-Maqaam', 'Jannat-al-Naeem', 'Jannat-al-Kasif', 'Dar-al-Salam', 'Dar-al-Khuld', 'Jannat-al-Adan')
print('---Original Tuple using pop() Function---')
print(pillars_of_islam)
# Convert Tuple to List
pillars_of_islam = list(pillars_of_islam)
# Remove Last Element from a Tuple using pop() Function
print('---New Tuple after using pop() Function---')
pillars_of_islam.pop()
print(pillars_of_islam)
except:
print("Error Occurred")
--- Approach I - Remove Specific Element from a Tuple using pop() Function---
---Original Tuple before using pop() Function---
('Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Fasting (sawm)', 'Pilgrimage (hajj)')
---New Tuple after using pop() Function---
['Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Pilgrimage (hajj)']
-------------------------------------------
--- Approach II - Remove Last Element from a Tuple using pop() Function---
---Original Tuple using pop() Function---
['Shahada', 'Prayer (salat)', 'Alms (zakat)', 'Pilgrimage (hajj)']
---New Tuple after using pop() Function---
['Shahada', 'Prayer (salat)', 'Alms (zakat)']
- Example 3 – Remove Specific / Last Element from a Tuple - Using pop() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific / Last Element from the Tuple
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 / last element from a Tuple
'''
# Approach I - Remove Specific Element from a Tuple using pop() Function
try:
print('--- Approach I - Remove Specific Element from a Tuple using pop() Function---')
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print('---Original Tuple before using pop() Function---')
print(pillars_of_human_engineering)
# Convert Tuple to List
pillars_of_human_engineering = list(pillars_of_human_engineering)
# Remove Specific Element from a Tuple using pop() Function
print('---New Tuple after using pop() Function---')
pillars_of_human_engineering.pop(3)
print(pillars_of_human_engineering)
except:
print("Error Occurred")
print("-------------------------------------------")
# Approach II - Remove Last Element from a Tuple using pop() Function
try:
print('--- Approach II - Remove Last Element from a Tuple using pop() Function---')
# Initialize Tuple
pillars_of_human_engineering = ('Truth', 'Honesty', 'Justice')
print('---Original Tuple using pop() Function---')
print(pillars_of_human_engineering)
# Convert Tuple to List
pillars_of_human_engineering = list(pillars_of_human_engineering)
# Remove Last Element from a Tuple using pop() Function
print('---New Tuple after using pop() Function---')
pillars_of_human_engineering.pop()
print(pillars_of_human_engineering)
except:
print("Error Occurred")
--- Approach I - Remove Specific Element from a Tuple using pop() Function---
---Original Tuple before using pop() Function---
('Truth', 'Honesty', 'Justice')
---New Tuple after using pop() Function---
Error Occurred
-------------------------------------------
--- Approach II - Remove Last Element from a Tuple using pop() Function---
---Original Tuple using pop() Function---
('Truth', 'Honesty', 'Justice')
---New Tuple after using pop() Function---
['Truth', 'Honesty']
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Tuples and answer the questions given below
- (‘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 Tuples and answer the questions given below
- Questions
- Remove Elements from the selected Tuples using following methods (as shown in this Chapter)
- Method 1: Remove Entire / Element(s) from Tuple using del
- Method 2: Remove Entire Tuple using clear() Function
- Method 3: Remove Specific Element from a Tuple using remove() Function
- Method 4: Remove Last Element from a Tuple using pop() Function
- Remove Elements from the selected Tuples using following methods (as shown in this Chapter)
Your Turn Task 1
- Task
- Consider two Tuples (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Remove Elements from the selected Tuples using following methods (as shown in this Chapter)
- Method 1: Remove Entire / Element(s) from Tuple using del
- Method 2: Remove Entire Tuple using clear() Function
- Method 3: Remove Specific Element from a Tuple using remove() Function
- Method 4: Remove Last Element from a Tuple using pop() Function
- Remove Elements from the selected Tuples using following methods (as shown in this Chapter)
Passing Tuple to a Function
- Passing Tuple to a Function
- In Sha Allah, in the next Slides, I will present how to pass Tuple to a Function
- Example 1 - Passing Tuples to a Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Passing Tuples 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 Tuples to a function and display the result on the Output Screen
'''
def fruits_of_jannah(fruits):
print("---Fruits of Jannah in Quran---")
for fruit in fruits:
print(fruit)
if __name__ == "__main__": # main() Function
try:
# Initializing a Tuple
food = ('date', 'olive', 'pomegranate', 'grape', 'banana', 'fig')
# Function Call
fruits_of_jannah(food)
except ValueError:
print("Error! Enter Valid Values")
---Fruits of Jannah in Quran---
date
olive
pomegranate
grape
banana
fig
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Tuples and answer the questions given below
- (‘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 Tuples and answer the questions given below
- Questions
- Pass Tuples to a function (as shown in this Chapter)
Your Turn Task 1
- Task
- Consider two Tuples (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Pass Tuples to a function (as shown in this Chapter)
Nested Tuple
- Nested Tuple
- Definition
- A Nest Tuple is a Tuple of Tuple(s)
Creating a Nested Tuple
- Syntax - Creating a Nested Tuple
- The syntax to create a Nested Tuple using () is as follows
TUPLE_NAME = (
(, , ),
(, , ),
.
.
.
(, , )
)
- Example 1 - Creating a Nested Tuple
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Create a Nested Tuple
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_date = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to create a nested Tuple
'''
# Initializing a Nested Tuple i.e., Student
try:
students = (
('Name', 'Age', 'Gender'),
'1',('Fatima', '21', 'Female'),
'2',('Zainab', '22', 'Female'),
'3',('Ali', '20', 'Male'),
'4',('Usman', '20', 'Male')
)
print("Tuple of Students for Introduction to Python:")
print(students)
# Data-Type of Tuple i.e., students
print("Data-Type of students:", type(students))
# Length of Tuple i.e., students
print("Length of students:", len(students))
except:
print("Error Occurred")
Tuple of Students for Introduction to Python:
(('Name', 'Age', 'Gender'), '1', ('Fatima', '21', 'Female'), '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4', ('Usman', '20', 'Male'))
Data-Type of students:
Length of students: 9
Access Elements of a Nested Tuple
- Access Elements of a Nested Tuple
- You can access individual items in a Nested Tuple using multiple indexes
- Example 1 - Access Elements of a Nested Tuple – Using Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Elements of Nested Tuple
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_date = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to access elements of a nested Tuple using indexing
'''
# Initializing a Nested Tuple i.e., Student
try:
students = (
('Name', 'Age', 'Gender'),
'1',('Fatima', '21', 'Female'),
'2',('Zainab', '22', 'Female'),
'3',('Ali', '20', 'Male'),
'4',('Usman', '20', 'Male')
)
# Convert Nested Tuple to Nested List
students = list(students)
# Access Elements of Nested Tuple using Indexing
print("Element at index students[0]:",students[0])
print("Element at index students[1]:",students[1])
print("Element at index students[2]:",students[2])
print("Element at index students[3]:",students[3])
print("Element at index students[4]:",students[4])
print("Element at index students[5]:",students[5])
print("Element at index students[6]:",students[6])
print("Element at index students[7]:",students[7])
print("Element at index students[8]:",students[8])
except:
print("Error Occurred")
Memory Representation
Element at index students[0]: ('Name', 'Age', 'Gender')
Element at index students[1]: 1
Element at index students[2]: ('Fatima', '21', 'Female')
Element at index students[3]: 2
Element at index students[4]: ('Zainab', '22', 'Female')
Element at index students[5]: 3
Element at index students[6]: ('Ali', '20', 'Male')
Element at index students[7]: 4
Element at index students[8]: ('Usman', '20', 'Male')
- Example 1 - Access Elements of a Nested Tuple – Using Negative Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Elements of Nested Tuple
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_date = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to access elements of a nested Tuple using indexing
'''
# Initializing a Nested Tuple i.e., Student
try:
students = (
('Name', 'Age', 'Gender'),
'1',('Fatima', '21', 'Female'),
'2',('Zainab', '22', 'Female'),
'3',('Ali', '20', 'Male'),
'4',('Usman', '20', 'Male')
)
# Convert Nested Tuple to Nested List
students = list(students)
# Access Elements of Nested Tuple using Negative Indexing
print("Element at index students[-1]:",students[-1])
print("Element at index students[-2]:",students[-2])
print("Element at index students[-3]:",students[-3])
print("Element at index students[-4]:",students[-4])
print("Element at index students[-5]:",students[-5])
print("Element at index students[-6]:",students[-6])
print("Element at index students[-7]:",students[-7])
print("Element at index students[-8]:",students[-8])
print("Element at index students[-9]:",students[-9])
except:
print("Error Occurred")
Memory Representation
Element at index students[-1]: ('Usman', '20', 'Male')
Element at index students[-2]: 4
Element at index students[-3]: ('Ali', '20', 'Male')
Element at index students[-4]: 3
Element at index students[-5]: ('Zainab', '22', 'Female')
Element at index students[-6]: 2
Element at index students[-7]: ('Fatima', '21', 'Female')
Element at index students[-8]: 1
Element at index students[-9]: ('Name', 'Age', 'Gender')
Operations on Nested Tuple
- Add Elements in a Nested Tuple
- In Sha Allah, in next Slides, I will present three methods to add element(s) in a Nested Tuple
- 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 Elements in a Nested Tuple
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add Elements in a Nested Tuple
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_date = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to add elements in a nested Tuple
'''
# Initializing a Nested Tuple i.e., Student
try:
students = (
('Name', 'Age', 'Gender'),
'1',('Fatima', '21', 'Female'),
'2',('Zainab', '22', 'Female'),
'3',('Ali', '20', 'Male'),
'4',('Usman', '20', 'Male')
)
# Approach I – Add a New Element at the Start of a Tuple
print("---Approach I – Add a New Element at the Start of a Tuple---")
# Display Original Tuple
print("---Original Nested Tuple---")
print(students)
print("------------------------------------------------")
# Convert Nested Tuple to Nested List
students = list(students)
# Display Tuple after Adding a New Element at the Start of a Tuple
print("---New Tuple after using insert() Function---")
# Adding Element at the Start of a Tuple using insert() Function
students.insert(0,'Tuple of Students of Introduction to Python')
print(students)
print("\n*************************************************\n")
# Approach II – Add a New Element at the End of a Tuple
print("---Approach II – Add a New Element at the End of a Tuple---")
# Display Original Tuple
print("---Original Nested Tuple---")
print(students)
print("------------------------------------------------")
# Display Tuple after Adding a New Element at the End of a Tuple
print("---New Tuple after using extend() Function---")
students.extend([6,['Laiba','22','Female','laiba@gmail.com']])
print(students)
print("\n*************************************************\n")
# Approach III – Add a New Element at the Desired Location of a Tuple
print("---Approach III – Add a New Element at the Desired Location of a Tuple---")
# Display Original Tuple
print("---Original Nested Tuple---")
print(students)
print("------------------------------------------------")
# Display Tuple after Adding a New Element at the Desired Location of a Tuple
print("---New Tuple after using insert() Function---")
# Adding Element at the Desired Location of a Tuple
students.insert(4,"[['Mustafa', '26', 'Male']]")
students.insert(5,"3")
print(students)
except:
print("Error Occurred")
---Approach I – Add a New Element at the Start of a Tuple---
---Original Nested Tuple---
(('Name', 'Age', 'Gender'), '1', ('Fatima', '21', 'Female'), '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4', ('Usman', '20', 'Male'))
------------------------------------------------
---New Tuple after using insert() Function---
['Tuple of Students of Introduction to Python', ('Name', 'Age', 'Gender'), '1', ('Fatima', '21', 'Female'), '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4', ('Usman', '20', 'Male')]
*************************************************
---Approach II – Add a New Element at the End of a Tuple---
---Original Nested Tuple---
['Tuple of Students of Introduction to Python', ('Name', 'Age', 'Gender'), '1', ('Fatima', '21', 'Female'), '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4', ('Usman', '20', 'Male')]
------------------------------------------------
---New Tuple after using extend() Function---
['Tuple of Students of Introduction to Python', ('Name', 'Age', 'Gender'), '1', ('Fatima', '21', 'Female'), '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4', ('Usman', '20', 'Male'), 6, ['Laiba', '22', 'Female', 'laiba@gmail.com']]
*************************************************
---Approach III – Add a New Element at the Desired Location of a Tuple---
---Original Nested Tuple---
['Tuple of Students of Introduction to Python', ('Name', 'Age', 'Gender'), '1', ('Fatima', '21', 'Female'), '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4', ('Usman', '20', 'Male'), 6, ['Laiba', '22', 'Female', 'laiba@gmail.com']]
------------------------------------------------
---New Tuple after using insert() Function---
['Tuple of Students of Introduction to Python', ('Name', 'Age', 'Gender'), '1', ('Fatima', '21', 'Female'), "[['Mustafa', '26', 'Male']]", '3', '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4', ('Usman', '20', 'Male'), 6, ['Laiba', '22', 'Female', 'laiba@gmail.com']]
Traverse a Nested Tuple
- Methods to Traverse a Tuple
- In Sha Allah, in next Slides, I will present two methods to traverse a Tuple
- Method 1: Traversing an Entire Nested Tuple
- Method 2: Traversing a Nested Tuple (Element by Element)
- Example 1 - Traversing an Entire Tuple
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Traversing an Entire Tuple
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_date = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to traversing an entire Tuple
'''
# Initializing a Nested Tuple i.e., Student
try:
students = (
('Name', 'Age', 'Gender'),
'1',('Fatima', '21', 'Female'),
'2',('Zainab', '22', 'Female'),
'3',('Ali', '20', 'Male'),
'4',('Usman', '20', 'Male')
)
# Traverse an Entire Tuple
print(students)
except Exception as ex:
print(ex)
(('Name', 'Age', 'Gender'), '1', ('Fatima', '21', 'Female'), '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4', ('Usman', '20', 'Male'))
- Example 2 - Traversing a Tuple (Element by Element)
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Traversing a Tuple (Element by Element)
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_date = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to traversing a Tuple (element by element)
'''
# Initializing a Nested Tuple i.e., Student
try:
students = (
('Name', 'Age', 'Gender'),
'1',('Fatima', '21', 'Female'),
'2',('Zainab', '22', 'Female'),
'3',('Ali', '20', 'Male'),
'4',('Usman', '20', 'Male')
)
# Traverse a Tuple (element by element)
for elements in students:
for elems in elements:
print(elems, end="\n")
except Exception as ex:
print("Error" + ex)
Name
Age
Gender
1
Fatima
21
Female
2
Zainab
22
Female
3
Ali
20
Male
4
Usman
20
Male
Update Elements of a Nested 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.
- Example 1 - Update Elements of a Nested Tuple
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Update Elements of a Nested Tuple
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_date = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to update elements of a nested Tuple
'''
# Initializing a Nested Tuple i.e., Student
try:
students = (
('Name', 'Age', 'Gender'),
'1',('Fatima', '21', 'Female'),
'2',('Zainab', '22', 'Female'),
'3',('Ali', '20', 'Male'),
'4',('Usman', '20', 'Male')
)
# Display Original Tuple
print("---Original Nested Tuple---")
print(students)
print("--------------------------------------------")
# Convert Nested Tuple to Nested List
students = list(students)
# Data-Type of Tuple i.e., 'students'
print("Data-Type of 'students' Tuple is:", type(students))
print("--------------------------------------------")
# Data Values of Tuple i.e., 'students'
print("Display Data Values converted into List:", students)
print("--------------------------------------------")
# Display New Tuple after Updating Elements
print("---New Nested Tuple after updating Elements---")
# Assign Values to Existing Tuple
students[1] = 0
students[2][0] = 'Mahnoor'
students[2][1] = '19'
students[2][2] = 'Female'
students[3] = 1
students[4][0] = 'Ibrahim'
students[4][1] = '26'
students[4][2] = 'Male'
print(students)
except:
print("Error Occurred")
---Original Nested Tuple---
(('Name', 'Age', 'Gender'), '1', ('Fatima', '21', 'Female'), '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4', ('Usman', '20', 'Male'))
--------------------------------------------
Data-Type of 'students' Tuple is:
--------------------------------------------
Display Data Values converted into List: [('Name', 'Age', 'Gender'), '1', ('Fatima', '21', 'Female'), '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4', ('Usman', '20', 'Male')]
--------------------------------------------
---New Nested Tuple after updating Elements---
'tuple' object does not support item assignment
Remove Elements from a Nested Tuple
- Remove Elements from a Nested Tuple
- In Sha Allah, in next Slides, I will present 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
- Example 1 - Remove Elements from a Nested Tuple
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Elements from a Nested Tuple
programming_language = Python
operating_system = Windows 10
ide = Jupyter Notebook
licence = public_domain_1.0
start_date = 21-Jun-2022
end_date = 21-Jun-2022
'''
'''
Purpose of Program
The main purpose of the program is to remove elements from a nested Tuple
'''
# Initializing a Nested Tuple i.e., Student
try:
students = (
('Name', 'Age', 'Gender'),
'1',('Fatima', '21', 'Female'),
'2',('Zainab', '22', 'Female'),
'3',('Ali', '20', 'Male'),
'4',('Usman', '20', 'Male')
)
# Approach I – Remove an Element from the Start of a Tuple
print("---Approach I – Remove an Element from the Start of a Tuple---")
# Display Original Tuple
print("---Original Nested Tuple---")
print(students)
# Convert Tuple to List
students = list(students)
print("------------------------------------------------")
# Display Tuple after Removing an Element from the Start of a Tuple
print("---New Tuple after Removing Element from Start using pop() Function---")
# Removing Element from the Start of a Tuple using pop() Function
students.pop(0)
print(students)
print("\n*************************************************\n")
# Approach II – Removing Element from the End of a Tuple
print("---Approach II – Remove Element from the End of a Tuple---")
# Display Original Tuple
print("---Original Nested Tuple---")
print(students)
print("------------------------------------------------")
# Display Tuple after Removing Element from the End of a Tuple
print("---New Tuple after Removing Element from the End using del Function---")
del students[7]
print(students)
print("\n*************************************************\n")
# Approach III – Removing Element from the Desired Location
print("---Approach III – Removing Element from the Desired Location---")
# Display Original Tuple
print("---Original Nested Tuple---")
print(students)
print("------------------------------------------------")
# Display Tuple after Removing Element from the Desired Location
print("---New Tuple after Removing Element from the Desired Location using remove() Function---")
students.remove(students[1])
print(students)
print("\n*************************************************\n")
# Approach IV – Remove an Entire Tuple
print("---Approach IV – Remove an Entire Tuple---")
# Display Original Tuple
print("---Original Nested Tuple---")
print(students)
print("------------------------------------------------")
# Remove Entire Tuple
print("---New Tuple after Removing an Entire Tuple using del Function---")
del students
print(students)
except Exception as ex:
print(ex)
---Approach I – Remove an Element from the Start of a Tuple---
---Original Nested Tuple---
(('Name', 'Age', 'Gender'), '1', ('Fatima', '21', 'Female'), '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4', ('Usman', '20', 'Male'))
------------------------------------------------
---New Tuple after Removing Element from Start using pop() Function---
['1', ('Fatima', '21', 'Female'), '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4', ('Usman', '20', 'Male')]
*************************************************
---Approach II – Remove Element from the End of a Tuple---
---Original Nested Tuple---
['1', ('Fatima', '21', 'Female'), '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4', ('Usman', '20', 'Male')]
------------------------------------------------
---New Tuple after Removing Element from the End using del Function---
['1', ('Fatima', '21', 'Female'), '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4']
*************************************************
---Approach III – Removing Element from the Desired Location---
---Original Nested Tuple---
['1', ('Fatima', '21', 'Female'), '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4']
------------------------------------------------
---New Tuple after Removing Element from the Desired Location using remove() Function---
['1', '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4']
*************************************************
---Approach IV – Remove an Entire Tuple---
---Original Nested Tuple---
['1', '2', ('Zainab', '22', 'Female'), '3', ('Ali', '20', 'Male'), '4']
------------------------------------------------
---New Tuple after Removing an Entire Tuple using del Function---
name 'students' is not defined
TODO and Your Turn
TODO Task 1
- Task
- Consider the following Tuples and answer the questions given below
student_information = ((‘Registration No.’, ‘Name’, ‘Degree Program’, ‘Course’)
1, (‘FA15-BSE-101’, ‘Ms. Samavi’,’BSE’, ‘Introduction to Python’),
2, (‘FA15-BSE-103’, ‘Ms. Ayesha’,’BSE’, ’23’,’Machine Learning’),
3, (‘FA15-BSE-105’, ‘Mr. Irfan’, ‘BSE’, ’20’, ‘Deep Learning’),
4, (‘FA15-BSE-107’, ‘Ms. Fatima’, ‘BSE’, ’21’, ‘NLP’),
5, (‘FA15-BSE-109’, ‘Mr. Mustafa’, ‘BSE’, ’22’, ‘Artificial Intelligence’)
)
- Questions
- Apply following operations on the given Nested Tuple (as shown in this Chapter)?
- Operation 1: Create a Nested Tuple
- Operation 2: Access Elements of a Nested Tuple
- Operation 3: Add New Element in a Nested Tuple
- Operation 4: Traverse a Nested Tuple
- Operation 5: Update Elements of a Nested Tuple
- Operation 6: Remove Elements of a Nested Tuple
- Apply following operations on the given Nested Tuple (as shown in this Chapter)?
Your Turn Task 1
- Task
- Consider a Nested Tuple (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Apply following operations on selected Nested Tuple (as shown in this Chapter)?
- Operation 1: Create a Nested Tuple
- Operation 2: Access Elements of a Nested Tuple
- Operation 3: Add New Element in a Nested Tuple
- Operation 4: Traverse a Nested Tuple
- Operation 5: Update Elements of a Nested Tuple
- Operation 6: Remove Elements of a Nested Tuple
- Apply following operations on selected Nested Tuple (as shown in this Chapter)?
Passing Nested Tuple to a Function
- Passing Nested Tuple to a Function
- In Sha Allah, in the next Slides, I will present how to pass nested Tuple to a Function
- Passing Nested Tuple to a Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Passing Nested Tuple 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 nested Tuples to a function and display the result on the Output Screen
'''
def fruits_of_jannah(fruits):
print("---Fruits of Jannah in Quran---")
for fruit in fruits:
print(fruit)
if __name__ == "__main__": # main() Function
try:
# Initializing a Nested Tuple
food = ('date', ('Ajwa', 'Anbara', 'Mabroom', 'Safawi', 'Saghai'), 'olive', 'pomegranate', 'grape', 'banana', 'fig')
# Function Call
fruits_of_jannah(food)
except ValueError:
print("Error! Enter Valid Values")
---Fruits of Jannah in Quran---
date
('Ajwa', 'Anbara', 'Mabroom', 'Safawi', 'Saghai')
olive
pomegranate
grape
banana
fig
TODO and Your Turn
TODO Task 1
- Task
- Consider the following Tuples and answer the questions given below
student_information = ((‘Registration No.’, ‘Name’, ‘Degree Program’, ‘Course’)
1, (‘FA15-BSE-101’, ‘Ms. Samavi’,’BSE’, ‘Introduction to Python’),
2, (‘FA15-BSE-103’, ‘Ms. Ayesha’,’BSE’, ’23’,’Machine Learning’),
3, (‘FA15-BSE-105’, ‘Mr. Irfan’, ‘BSE’, ’20’, ‘Deep Learning’),
4, (‘FA15-BSE-107’, ‘Ms. Fatima’, ‘BSE’, ’21’, ‘NLP’),
5, (‘FA15-BSE-109’, ‘Mr. Mustafa’, ‘BSE’, ’22’, ‘Artificial Intelligence’)
)
- Questions
- Pass the above Tuple / Nested Tuple to the Function, Iterate through the Nested Tuple and Display all the elements on the Output Screen (as shown in this Chapter)?
Your Turn Task 1
- Task
- Consider a Nested Tuple (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Pass a Tuple / Nested Tuple to the Function, Iterate through the Nested Tuple and Display all the elements on the Output Screen (as shown in this Chapter)?
Chapter Summary
- Chapter Summary
In this Chapter, I presented the following main concepts:
- 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
In Next Chapter
- In Next Chapter
- In Sha Allah, in the next Chapter, I will present a detailed discussion on
- Sets in Python