Chapter 31 - Lists in Python
- Authors
- Ms. Samavi Salman
- Dr. Rao Muhammad Adeel Nawab
- Supporting Material
Quick Recap
- Quick Recap – Strings in Python
In previous Chapter, I presented
- String
- A string is defined as a sequence of characters
- String Methods
- String Methods are special functions used to manipulate strings
- String Literals
- In Python, String Literals are surrounded by either single quotes, or double quotes, or triple quotes
- Single Quote, Double Quote and Triple Quote
- Single Quote
- In Python, Single Quotes are generally used for String Literals and Regular Expressions
- Double Quote
- In Python, Double Quotes are generally used for string representation
- Triple Quote
- In Python, Triple Quotes are generally used to allow strings to span multiple lines (which may include tabs, newlines, or any other special characters)
- Escape Sequences
- An Escape Sequence is defined using a combination of Two Characters, were
- First Character is a Backslash \ and
- Second Character is a Letter
- Creating a String
- In Python, generally a String is created using double quotes
- title()
- capitalize()
- upper()
- lower()
- center()
- Accessing Elements of a String
- Method 1: Accessing Elements of a String using Indexing
- Method 2: Accessing Elements of a String using Slicing
- Method 3: Accessing Elements of a String using split() Function
- Removing Whitespaces from a String
- strip()
- rstrip()
- lstrip()
- Operations on Strings
- Operation 1 – Creation
- Operation 2 – Insertion
- Operation 3 – Traversal
- Operation 4 – Searching
- in
- fine()
- startswith()
- endswith()
- Operation 5 – Sorting
- sorted()
- Operation 6 – Merging
- Using + operator
- Using join() Method
- Using format() Method
- Operation 7 – Updation
- replace()
- Operation 8 – Deletion
- del()
- In Python, generally a String is created using double quotes
- An Escape Sequence is defined using a combination of Two Characters, were
- Single Quote
List
- List
- Definition
- A List is defined as an ordered collection of items, which may or may not have same data types
- Purpose
- The main purpose of a List is to
- store and manipulate multiple values (which may or may not have same Data Type)
- The main purpose of a List is to
- Importance
- List is one of the oldest and most important Data Structures and used in almost every program
- Applications
- In a range of Real-world Applications (for e.g., sorting marks of students in Python course in ascending order), we need to store and manipulate multiple values and a Software Developer can
- efficiently handle multiple values using a List
- In a range of Real-world Applications (for e.g., sorting marks of students in Python course in ascending order), we need to store and manipulate multiple values and a Software Developer can
Declaring a List
- List Declaration
- A List can be declared using two methods
- Method 1: Use []
- Method 2: Use list() Function
- In Sha Allah, in next Slides, I will present syntax of both Methods
- Syntax - Use []
- The syntax to declare a List using [] is as follows
LIST_NAME = [
,
,
,
.
.
.
,
,
]
- Syntax - Use list() Function
- The syntax to declare a List using list() Function is as follows
LIST_NAME = list([
,
,
,
.
.
.
,
,
])
- Example 1 – List Declaration
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a List
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 list and display its content (data values) on the Output Screen
'''
# List Initialization - Method 1: Use []
try:
print("---List Initialization - Use []---")
# List Initialization
caliphs_of_islam_1 = ['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
# Display Data Values of a List
print("Data Values of caliphs_of_islam_1 List:", caliphs_of_islam_1)
# Display Length of a List using len() Function
print("Length of caliphs_of_islam_1 List:", len(caliphs_of_islam_1))
# Display Type of a List using type() Function
print("Data-Type of caliphs_of_islam_1 List:", type(caliphs_of_islam_1))
except:
print("Error Occurred")
print("---------------------------------------------------------")
# List Initialization - Method 2: Use list() Function
try:
print("---List Initialization - Use list() Function---")
# List Initialization
caliphs_of_islam_2 = list(('Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.'))
# Display Data Values of a List
print("Data Values of caliphs_of_islam_2 List:", caliphs_of_islam_2)
# Display Length of a List using len() Function
print("Length of caliphs_of_islam_2 List:", len(caliphs_of_islam_2))
# Display Type of a List using type() Function
print("Data-Type of caliphs_of_islam_2 List:", type(caliphs_of_islam_2))
except:
print("Error Occurred")
---List Initialization - Use []---
Data Values of caliphs_of_islam_1 List: ['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
Length of caliphs_of_islam_1 List: 4
Data-Type of caliphs_of_islam_1 List:
---------------------------------------------------------
---List Initialization - Use list() Function---
Data Values of caliphs_of_islam_2 List: ['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
Length of caliphs_of_islam_2 List: 4
Data-Type of caliphs_of_islam_2 List:
- Example 2 – List Declaration
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a List
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 list and display its content (data values) on the Output Screen
'''
# List Initialization - Method 1: Use []
try:
print("---List Initialization - Use []---")
# List Initialization
marks_1 = [60,60,80,61]
# Display Data Values of a List
print("Data Values of marks_1 List:\n", marks_1)
# Display Length of a List using len() Function
print("Length of marks_1 List:",len(marks_1))
# Display Type of a List using type() Function
print("Data-Type of marks_1 List:",type(marks_1))
except:
print("Error Occurred")
print("---------------------------------------------------------")
# List Initialization - Method 2: Use list() Function
try:
print("---Initialize a List - Method 2: list()---")
# List Initialization
marks_2 = list((60,60,80,61))
# Display Data Values of a List
print("Data Values of marks_2 List:\n", marks_2)
# Display Length of a List using len() Function
print("Length of marks_2 List:",len(marks_2))
# Display Type of a List using type() Function
print("Data-Type of marks_2 List:",type(marks_2))
except:
print("Error Occurred")
---List Initialization - Use []---
Data Values of marks_1 List:
[60, 60, 80, 61]
Length of marks_1 List: 4
Data-Type of marks_1 List:
---------------------------------------------------------
---Initialize a List - Method 2: list()---
Data Values of marks_2 List:
[60, 60, 80, 61]
Length of marks_2 List: 4
Data-Type of marks_2 List:
Initialize a List
- List Initialization
- In Sha Allah, in the next Slides, I will present two methods to initialize a List
- Method 1: Use []
- Method 2: Use list()Function
- Example 1 – List Initialization
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a List
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 list and display its content (data values) on the Output Screen
'''
# List Initialization - Method 1: Use []
try:
print("---List Initialization - Use []---")
# List Initialization
caliphs_of_islam_1 = ['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
# Display Data Values of a List
print("Data Values of caliphs_of_islam_1 List:", caliphs_of_islam_1)
# Display Length of a List using len() Function
print("Length of caliphs_of_islam_1 List:", len(caliphs_of_islam_1))
# Display Type of a List using type() Function
print("Data-Type of caliphs_of_islam_1 List:", type(caliphs_of_islam_1))
except:
print("Error Occurred")
print("---------------------------------------------------------")
# List Initialization - Method 2: Use list() Function
try:
print("---List Initialization - Use list() Function---")
# List Initialization
caliphs_of_islam_2 = list(('Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.'))
# Display Data Values of a List
print("Data Values of caliphs_of_islam_2 List:", caliphs_of_islam_2)
# Display Length of a List using len() Function
print("Length of caliphs_of_islam_2 List:", len(caliphs_of_islam_2))
# Display Type of a List using type() Function
print("Data-Type of caliphs_of_islam_2 List:", type(caliphs_of_islam_2))
except:
print("Error Occurred")
---List Initialization - Use []---
Data Values of caliphs_of_islam_1 List: ['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
Length of caliphs_of_islam_1 List: 4
Data-Type of caliphs_of_islam_1 List:
---------------------------------------------------------
---List Initialization - Use list() Function---
Data Values of caliphs_of_islam_2 List: ['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
Length of caliphs_of_islam_2 List: 4
Data-Type of caliphs_of_islam_2 List:
- Example 2 – List Initialization
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Initialize a List
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 list and display its content (data values) on the Output Screen
'''
# List Initialization - Method 1: Use []
try:
print("---List Initialization - Use []---")
# List Initialization
marks_1 = [60,60,80,61]
# Display Data Values of a List
print("Data Values of marks_1 List:\n", marks_1)
# Display Length of a List using len() Function
print("Length of marks_1 List:",len(marks_1))
# Display Type of a List using type() Function
print("Data-Type of marks_1 List:",type(marks_1))
except:
print("Error Occurred")
print("---------------------------------------------------------")
# List Initialization - Method 2: Use list() Function
try:
print("---Initialize a List - Method 2: list()---")
# List Initialization
marks_2 = list((60,60,80,61))
# Display Data Values of a List
print("Data Values of marks_2 List:\n", marks_2)
# Display Length of a List using len() Function
print("Length of marks_2 List:",len(marks_2))
# Display Type of a List using type() Function
print("Data-Type of marks_2 List:",type(marks_2))
except:
print("Error Occurred")
---List Initialization - Use []---
Data Values of marks_1 List:
[60, 60, 80, 61]
Length of marks_1 List: 4
Data-Type of marks_1 List:
---------------------------------------------------------
---Initialize a List - Method 2: list()---
Data Values of marks_2 List:
[60, 60, 80, 61]
Length of marks_2 List: 4
Data-Type of marks_2 List:
TODO and Your Turn
TODO Task 1
- Task
- Declare and Initialize a List called ghazwa_in_Islam using the [] and list() Function?
- Note
- The elements of ghazwa_in_Islam are as follows
- Badr
- Uhud
- Khandaq
- Banu Qurayza
- Banu l-Mustaliq
- Khaybar
- Conquest of Mecca
- Hunayn
- Al-Ta’if
- The elements of ghazwa_in_Islam are as follows
Your Turn Task 1
- Task
- Declare and Initialize a List (similar to the one given in TODO Task 1) using the [] and list() Function?
Accessing Elements of a List
- Accessing Elements of a List
- In Sha Allah, in next Slides, I will present three methods to access element(s) of a List
- Method 1 – Access Element(s) of a List using Indexing
- Method 2 – Access Element(s) of a List using Negative Indexing
- Method 3 – Access Element(s) of a List using Slicing
Access Element(s) of a List using Indexing
- Example 1 - Access Element(s) of a List using Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a List 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 list using indexing and display the result on the output screen
'''
try:
# Initialize List
marks = [60,60,80,61]
# Access Element(s) of a List using Indexing
print("---Access Element(s) of a List using Indexing---")
print("marks[0]:",marks[0])
print("marks[1]:",marks[1])
print("marks[2]:",marks[2])
print("marks[3]:",marks[3])
except:
print("Error Occurred")
---Access Element(s) of a List using Indexing---
marks[0]: 60
marks[1]: 60
marks[2]: 80
marks[3]: 61
- Example 2 - Access Element(s) of a List using Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a List 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 list using indexing and display the result on the output screen
'''
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
# Access Element(s) of a List using Indexing
print("---Access Element(s) of a List using Indexing---")
print("caliphs_of_islam[0]:",caliphs_of_islam[0])
print("caliphs_of_islam[1]:",caliphs_of_islam[1])
print("caliphs_of_islam[2]:",caliphs_of_islam[2])
print("caliphs_of_islam[3]:",caliphs_of_islam[3])
except:
print("Error Occurred")
---Access Element(s) of a List using Indexing---
caliphs_of_islam[0]: Hazrat Abu Bakr Siddique (R.A.)
caliphs_of_islam[1]: Hazrat Umar ibn Khattab (R.A.)
caliphs_of_islam[2]: Hazrat Uthman ibn Affan (R.A.)
caliphs_of_islam[3]: Hazrat Ali ibn Abi Talib (R.A.)
Access Element(s) of a List using Negative Indexing
- Example 1 - Access Element(s) of a List using Negative Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a List 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 list using negative indexing and display the result on the output screen
'''
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
# Access Element(s) of a List using Negative Indexing
print("---Access Element(s) of a List using Negative Indexing---")
print("caliphs_of_islam[-1]:",caliphs_of_islam[-1])
print("caliphs_of_islam[-2]:",caliphs_of_islam[-2])
print("caliphs_of_islam[-3]:",caliphs_of_islam[-3])
print("caliphs_of_islam[-4]:",caliphs_of_islam[-4])
except:
print("Error Occurred")
---Access Element(s) of a List using Negative Indexing---
caliphs_of_islam[-1]: Hazrat Ali ibn Abi Talib (R.A.)
caliphs_of_islam[-2]: Hazrat Uthman ibn Affan (R.A.)
caliphs_of_islam[-3]: Hazrat Umar ibn Khattab (R.A.)
caliphs_of_islam[-4]: Hazrat Abu Bakr Siddique (R.A.)
- Example 2 - Access Element(s) of a List using Negative Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a List 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 list using negative indexing and display the result on the output screen
'''
try:
# Initialize List
marks = [60,60,80,61]
# Access Element(s) of a List using Negative Indexing
print("---Access Element(s) of a List using Negative Indexing---")
print("marks[-1]:",marks[-1])
print("marks[-2]:",marks[-2])
print("marks[-3]:",marks[-3])
print("marks[-4]:",marks[-4])
except:
print("Error Occurred")
---Access Element(s) of a List using Negative Indexing---
marks[-1]: 61
marks[-2]: 80
marks[-3]: 60
marks[-4]: 60
Access Element(s) of a List using Slicing
- Example 1 - Access Element(s) of a List using Slicing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a List 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 list using slicing and display the result on the output screen
'''
try:
# Initialize List
marks = [60,60,80,61]
# Access Element(s) of a List using Slicing
print("---Access Element(s) of a List using Slicing---")
print("marks[2:4]:",marks[2:4])
# Access from 2nd element till 4th element
print("marks[1:4]:", marks[1:4])
# Access from start till 3rd element
print("marks[:3]:", marks[:3])
# Access from 2nd element till end
print("marks[1:]:", marks[1:])
# Access from start till 3rd last element
print("marks[:-2]:", marks[:-2])
# Access from 3rd last element till 1st last element
print("marks[-3:-1]:", marks[-3:-1])
# Access from 2nd element till 2nd last element
print("marks[1:-1]:", marks[1:-1])
# Access all elements
print("marks[:]:",marks[:])
except:
print("Error Occurred")
---Access Element(s) of a List using Slicing---
marks[2:4]: [80, 61]
marks[1:4]: [60, 80, 61]
marks[:3]: [60, 60, 80]
marks[1:]: [60, 80, 61]
marks[:-2]: [60, 60]
marks[-3:-1]: [60, 80]
marks[1:-1]: [60, 80]
marks[:]: [60, 60, 80, 61]
- Example 2 - Access Element(s) of a List using Slicing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Element(s) of a List 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 list using slicing and display the result on the output screen
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
# Access Element(s) of a List using Slicing
print("---Access Element(s) of a List using Slicing---")
print("caliphs_of_islam[2:4]:",caliphs_of_islam[2:4])
# Access from 2nd element till 4th element
print("caliphs_of_islam[1:4]:",caliphs_of_islam[1:4])
# Access from start till 3rd element
print("caliphs_of_islam[:3]:",caliphs_of_islam[:3])
# Access from 2nd element till end
print("caliphs_of_islam[1:]:",caliphs_of_islam[1:])
# Access from start till 3rd last element
print("caliphs_of_islam[:-2]:",caliphs_of_islam[:-2])
# Access from 3rd last element till 1st last element
print("caliphs_of_islam[-3:-1]:",caliphs_of_islam[-3:-1])
# Access from 2nd element till 2nd last element
print("caliphs_of_islam[1:-1]:",caliphs_of_islam[1:-1])
# Access all elements
print("caliphs_of_islam[:]:",caliphs_of_islam[:])
except:
print("Error Occurred")
---Access Element(s) of a List using Slicing---
caliphs_of_islam[2:4]: ['Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
caliphs_of_islam[1:4]: ['Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
caliphs_of_islam[:3]: ['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)']
caliphs_of_islam[1:]: ['Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
caliphs_of_islam[:-2]: ['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)']
caliphs_of_islam[-3:-1]: ['Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)']
caliphs_of_islam[1:-1]: ['Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)']
caliphs_of_islam[:]: ['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Lists 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 Lists and answer the questions given below
- Questions
- Access Elements of the prophets and ages lists suing
- Indexing
- Negative Indexing
- Slicing
- Access Elements of the prophets and ages lists suing
Your Turn Task 1
- Task
- Consider two Lists (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Access Elements of the lists (as shown in this Chapter) using
- Indexing
- Negative Indexing
- Slicing
- Access Elements of the lists (as shown in this Chapter) using
Operations on Lists
Operation 1 – Creation
- Methods to Create a List
- I have already presented methods to create a List in the previous Section i.e.,
- List Initialization
Operation 2 – Insertion
- Methods to Add Element(s) in a List
- In Sha Allah, in next Slides, I will present three methods to add element(s) in a List
- Method 1 – Add a New Element at the Start of a List
- Method 2 – Add a New Element at the End of a List
- Method 3 – Add a New Element at a Desired Location of a List
- Functions to Add Element(s) in a List
- In Sha Allah, I will use the following functions to Add Element(s) in a List
- append() Function
- insert() Function
- append() Function
- Function Name
- append()
- Syntax
list.append(obj)
- Purpose
- The main purpose of append() Function is to
- appends data value(s) at the end of a List
- The main purpose of append() Function is to
- insert() Function
- Function Name
- insert()
- Syntax
list.insert(index, obj)
- Purpose
- The main purpose of insert() Function is to
- inserts data value(s) at a specific Index of a List
- The main purpose of insert() Function is to
Add a New Element at the Start of a List
- Add a New Element at the Start of a List
- In Sha Allah, in the next Slides, I will present method to Add New Element(s) at Start of a List using
- insert() Built-in Function
- Example 1 – Add a New Element at the Start of a List – Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element at the Start of a List
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 list
'''
try:
print("---Add a Single Element at Start of a List---")
# List Initialization
caliphs_of_islam = ['Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
print("---Original List before Adding Element at Start of a List---")
print(caliphs_of_islam)
# Add Data Value at Start of a List using insert() Function
caliphs_of_islam.insert(0,"Hazrat Umar.e.Farooq R.A.")
# Display Updated List after Adding Element at Start of a List
print("---Updated List after Adding Element at Start of a List---")
print(caliphs_of_islam)
print("-------------------------------------------------------------")
print("---Again Add a Single Element at Start of a List---")
print("---Original List before Adding Element at Start of a List---")
print(caliphs_of_islam)
# Add Data Value at Start of a List using insert() Function
caliphs_of_islam.insert(0," Hazrat Abu Bakr Siddique R.A.")
# Display Updated List after Adding Element at Start of a List
print("---Updated List after Adding Element at Start of a List---")
print(caliphs_of_islam)
except:
print("Error Occurred")
---Add a Single Element at Start of a List---
---Original List before Adding Element at Start of a List---
['Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
---Updated List after Adding Element at Start of a List---
['Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
-------------------------------------------------------------
---Again Add a Single Element at Start of a List---
---Original List before Adding Element at Start of a List---
['Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
---Updated List after Adding Element at Start of a List---
[' Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
- Example 2 – Add a New Element at the Start of a List – Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element at the Start of a List
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 list
'''
try:
print("---Add a Single Element at Start of a List---")
# List Initialization
marks = [60, 80, 61]
print("---Original List before Adding Element at Start of a List---")
print(marks)
# Add Data Value at Start of a List using insert() Function
marks.insert(0,60)
# Display Updated List after Adding Element at Start of a List
print("---Updated List after Adding Element at Start of a List---")
print(marks)
except:
print("Error Occurred")
---Add a Single Element at Start of a List---
---Original List before Adding Element at Start of a List---
[60, 80, 61]
---Updated List after Adding Element at Start of a List---
[60, 60, 80, 61]
Add Multiple Elements at the Start of a List
- Example 1 – Add Multiple Elements at the Start of a List – Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add Multiple Elements at the Start of a List
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 list
'''
try:
print("---Add Multiple Elements at the Start of a List---")
# List Initialization
caliphs_of_islam = ['Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
print("---Original List before Adding Elements at Start of a List---")
print(caliphs_of_islam)
# Add Data Values at Start of a List using insert() Function
caliphs_of_islam.insert(0," Hazrat Abu Bakr Siddique R.A.")
caliphs_of_islam.insert(1,"Hazrat Umar.e.Farooq R.A.")
# Display Updated List after Adding Elements at Start of a List
print("---Updated List after Adding Elements at Start of a List---")
print(caliphs_of_islam)
except:
print("Error Occurred")
---Add Multiple Elements at the Start of a List---
---Original List before Adding Elements at Start of a List---
['Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
---Updated List after Adding Elements at Start of a List---
[' Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
- Example 2 – Add Multiple Elements at the Start of a List – Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add Multiple Elements at the Start of a List
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 list
'''
try:
print("---Add Multiple Elements at the Start of a List---")
# List Initialization
marks = [80, 61]
print("---Original List before Adding Elements at Start of a List---")
print(marks)
# Add Data Values at Start of a List using insert() Function
marks.insert(0, 60)
marks.insert(1, 60)
# Display Updated List after Adding Elements at Start of a List
print("---Updated List after Adding Elements at Start of a List---")
print(marks)
except:
print("Error Occurred")
---Add Multiple Elements at the Start of a List---
---Original List before Adding Elements at Start of a List---
[80, 61]
---Updated List after Adding Elements at Start of a List---
[60, 60, 80, 61]
Add a New Element at the End of a List
- Add a New Element at the End of a List
- In Sha Allah, in the next Slides, I will present methods to Add New Element(s) at End of a List using
- append() Built-in Function
- Example 1 – Add a New Element at the End of a List – Using append() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element at the End of a List
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 list
'''
try:
print("---Add a Single Element at End of a List---")
# List Initialization
caliphs_of_islam = ['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.']
print("---Original List before Adding Element at End of a List---")
print(caliphs_of_islam)
# Add Data Value at End of a List using append() Function
caliphs_of_islam.append("Hazrat Ali ul Murtaza R.A.")
# Display Updated List after Adding Element at End of a List
print("---Updated List after Adding Element at End of a List---")
print(caliphs_of_islam)
except:
print("Error Occurred")
---Add a Single Element at End of a List---
---Original List before Adding Element at End of a List---
['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.']
---Updated List after Adding Element at End of a List---
['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
- Example 2 – Add a New Element at the End of a List – Using append() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element at the End of a List
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 list
'''
try:
print("---Add a Single Element at End of a List---")
# List Initialization
marks = [60, 60, 80]
print("---Original List before Adding Element at End of a List---")
print(marks)
# Add Data Value at End of a List using append() Function
marks.append(61)
# Display Updated List after Adding Element at End of a List
print("---Updated List after Adding Element at End of a List---")
print(marks)
except:
print("Error Occurred")
---Add a Single Element at End of a List---
---Original List before Adding Element at End of a List---
[60, 60, 80]
---Updated List after Adding Element at End of a List---
[60, 60, 80, 61]
Add Multiple Elements at the End of a List
- Example 1 – Add Multiple Elements at the End of a List – Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add Multiple Elements at the End of a List
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 list
'''
try:
print("---Add Multiple Elements at End of a List---")
# List Initialization
caliphs_of_islam = ['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.']
print("---Original List before Adding Elements at End of a List---")
print(caliphs_of_islam)
# Add Data Values at End of a List using insert() Function
caliphs_of_islam.insert(2," Hazrat Usman.e.Ghani R.A.")
caliphs_of_islam.insert(3,"Hazrat Ali ul Murtaza R.A.")
# Display Updated List after Adding Elements at End of a List
print("---Updated List after Adding Elements at End of a List---")
print(caliphs_of_islam)
except:
print("Error Occurred")
---Add Multiple Elements at End of a List---
---Original List before Adding Elements at End of a List---
['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.']
---Updated List after Adding Elements at End of a List---
['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', ' Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
- Example 2 – Add Multiple Elements at the End of a List – Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add Multiple Elements at the End of a List
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 list
'''
try:
print("---Add Multiple Elements at End of a List---")
# List Initialization
marks = [60, 61]
print("---Original List before Adding Elements at End of a List---")
print(marks)
# Add Data Values at End of a List using insert() Function
marks.insert(2, 60)
marks.insert(3, 80)
# Display Updated List after Adding Elements at End of a List
print("---Updated List after Adding Elements at End of a List---")
print(marks)
except:
print("Error Occurred")
---Add Multiple Elements at End of a List---
---Original List before Adding Elements at End of a List---
[60, 61]
---Updated List after Adding Elements at End of a List---
[60, 61, 60, 80]
Add a New Element at a Desired Location of a List
- Add a New Element at a Desired Location of a List
- In Sha Allah, in the next Slides, I will present method to add new element(s) to the Desired Location of a List using
- insert() Built-in Function
- Example 1 – Add a New Element(s) at a Desired Location of a List - Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add a New Element(s) at a Desired Location of a List
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 new element(s) at a desired location of a list
'''
try:
print("---Add New Element(s) at Desired Location of a List---")
# List Initialization
caliphs_of_islam = ['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Ali ul Murtaza R.A.']
print("---Original List before Adding Element---")
print(caliphs_of_islam)
# Add Data Values at 1st Index of a List using insert() Function
caliphs_of_islam.insert(1,"Hazrat Umar.e.Farooq R.A.")
# Add Data Values at 2nd Index of a List using insert() Function
caliphs_of_islam.insert(2,"Hazrat Usman.e.Ghani R.A.")
# Display Updated List after Adding Element at Start of a List
print("---Updated List after Adding Element---")
print(caliphs_of_islam)
except:
print("Error Occurred")
---Add New Element(s) at Desired Location of a List---
---Original List before Adding Element---
['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Ali ul Murtaza R.A.']
---Updated List after Adding Element---
['Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
- Example 2 – Add New Element(s) at a Desired Location of a List – Using insert() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add New Element(s) at a Desired Location of a List
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 new element(s) at a desired location of a list
'''
try:
print("---Add New Element(s) at Desired Location of a List---")
# List Initialization
marks = [60, 80]
print("---Original List before Adding Element---")
print(marks)
# Add Data Values at 1st Index of a List using insert() Function
marks.insert(1, 61)
# Add Data Values at 2nd Index of a List using insert() Function
marks.insert(2, 62)
# Display Updated List after Adding Element at Start of a List
print("---Updated List after Adding Element---")
print(marks)
except:
print("Error Occurred")
---Add New Element(s) at Desired Location of a List---
---Original List before Adding Element---
[60, 80]
---Updated List after Adding Element---
[60, 61, 62, 80]
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Lists and answer the questions given below
- prophets = [‘Hazrat Muhammad (PBUH)’, ‘Hazrat Ibrahim (A.S.)’, ‘Hazrat Mosa (A.S.)’, ‘Hazrat Isa (A.S.)’, ‘Hazrat Adam (A.S.)’]
- ages = [25, 30, 15, 20, 22, 33]
- Questions
- Insert Elements to prophets and ages lists using following methods (as shown in this Chapter)
- Method 1 – Add a New Element at the Start of a List
- Method 2 – Add a New Element at the End of a List
- Method 3 – Add a New Element at a Desired Location of a List
- Insert Elements to prophets and ages lists using following methods (as shown in this Chapter)
- Consider the following two Lists and answer the questions given below
Your Turn Task 1
- Task
- Consider two Lists (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Insert Elements to selected lists using following methods (as shown in this Chapter)
- Method 1 – Add a New Element at the Start of a List
- Method 2 – Add a New Element at the End of a List
- Method 3 – Add a New Element at a Desired Location of a List
- Insert Elements to selected lists using following methods (as shown in this Chapter)
Operation 3 – Traverse
- Methods to Traverse a List
- In Sha Allah, in next Slides, I will present two methods to traverse a List
- Method 1: Traversing an Entire List
- Method 2: Traversing a List (Element by Element)
Traversing an Entire List
- Example 1 - Traversing an Entire List
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Traversing an Entire List
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 list and display the result on the Output Screen
'''
try:
# List Initialization
caliphs_of_islam = [' Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
# Traverse an entire list
print(caliphs_of_islam)
except:
print('Error Occurred')
[' Hazrat Abu Bakr Siddique R.A.', 'Hazrat Umar.e.Farooq R.A.', 'Hazrat Usman.e.Ghani R.A.', 'Hazrat Ali ul Murtaza R.A.']
- Example 2 - Traversing an Entire List
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Traversing an Entire List
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 list and display the result on the Output Screen
'''
try:
# List Initialization
marks = [60, 61, 62, 80]
# Traverse an entire list
print(marks)
except:
print('Error Occurred')
[60, 61, 62, 80]
Traversing a List (Element by Element)
- Example 1 - Traversing a List (Element by Element)
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Traversing a List (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 list (element by element) and display the result on the Output Screen
'''
try:
# ---Approach I - Iterate over a List (element by element) using for loop---
# List Initialization
caliphs_of_islam = ['Abu Bakr Siddique', 'Umar ibn Khattab', 'Uthman ibn Affan', 'Ali ibn Abi Talib']
# Iterate over a List (element by element) using for loop
print(" ---Approach I - Iterate over a List (element by element) using for loop---")
print("---Traverse a List (Element by Element) using for Loop---")
for element in caliphs_of_islam:
print(element)
except:
print('Error Occurred')
print("-------------------------------------------------------------------")
try:
# ---Approach II - Iterate over a List (element by element) using Indexing---
# Initialize List
caliphs_of_islam = ['Abu Bakr Siddique', 'Umar ibn Khattab', 'Uthman ibn Affan', 'Ali ibn Abi Talib']
# Iterate over a List (element by element) using for loop with Indexing
print(" ---Approach II - Iterate over a List (element by element) using Indexing---")
print("---Traverse a List (Element by Element) using for Loop with Indexing---")
for element in range(0, len(caliphs_of_islam)):
print("caliphs_of_islam[",element,"]: " + caliphs_of_islam[element])
except:
print('Error Occurred')
print("---------------------------------------------------------------------------")
try:
# ---Approach III - Iterate over a List (element by element) using Slicing---
# Initialize List
caliphs_of_islam = ['Abu Bakr Siddique', 'Umar ibn Khattab', 'Uthman ibn Affan', 'Ali ibn Abi Talib']
counter = 0
print("---Approach III - Iterate over a List (element by element) using Slicing---")
# Iterate over a List (element by element) using for loop with Slicing
print("---Traverse a List (Element by Element) using for Loop with Slicing---")
# Slicing Operator => string[starting index : ending index : step value]
for element in caliphs_of_islam[0 : len(caliphs_of_islam): 1]:
counter = counter + 1
print("caliphs_of_islam[",counter,"]: " + element)
except:
print('Error Occurred')
print("---------------------------------------------------------------------------")
try:
# ---Approach IV - Iterate over a List (element by element) for loop using enumerate() Function---
# Initialize List
caliphs_of_islam = ['Abu Bakr Siddique', 'Umar ibn Khattab', 'Uthman ibn Affan', 'Ali ibn Abi Talib']
counter = 0
print("---Approach IV - Iterate over a List (element by element) using enumerate() Function---")
# Iterate over a List (element by element) using for loop using enumerate() Function
print("---Traverse a List (Element by Element) using for Loop with enumerate() Function---")
# Iterate over a List (element by element) using for loop using enumerate() Function
for count, element in enumerate(caliphs_of_islam):
print (count," ",element)
except:
print('Error Occurred')
---Approach I - Iterate over a List (element by element) using for loop---
---Traverse a List (Element by Element) using for Loop---
Abu Bakr Siddique
Umar ibn Khattab
Uthman ibn Affan
Ali ibn Abi Talib
-------------------------------------------------------------------
---Approach II - Iterate over a List (element by element) using Indexing---
---Traverse a List (Element by Element) using for Loop with Indexing---
caliphs_of_islam[ 0 ]: Abu Bakr Siddique
caliphs_of_islam[ 1 ]: Umar ibn Khattab
caliphs_of_islam[ 2 ]: Uthman ibn Affan
caliphs_of_islam[ 3 ]: Ali ibn Abi Talib
---------------------------------------------------------------------------
---Approach III - Iterate over a List (element by element) using Slicing---
---Traverse a List (Element by Element) using for Loop with Slicing---
caliphs_of_islam[ 1 ]: Abu Bakr Siddique
caliphs_of_islam[ 2 ]: Umar ibn Khattab
caliphs_of_islam[ 3 ]: Uthman ibn Affan
caliphs_of_islam[ 4 ]: Ali ibn Abi Talib
---------------------------------------------------------------------------
---Approach IV - Iterate over a List (element by element) using enumerate() Function---
---Traverse a List (Element by Element) using for Loop with enumerate() Function---
0 Abu Bakr Siddique
1 Umar ibn Khattab
2 Uthman ibn Affan
3 Ali ibn Abi Talib
- Example 2 - Traversing a List (Element by Element)
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Traversing a List (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 list (element by element) and display the result on the Output Screen
'''
try:
# ---Approach I - Iterate over a List (element by element) using for loop---
# List Initialization
marks = [60,60,80,61]
# Iterate over a List (element by element) using for loop
print(" ---Approach I - Iterate over a List (element by element) using for loop---")
print("---Traverse a List (Element by Element) using for Loop---")
for element in marks:
print(element)
except:
print('Error Occurred')
print("----------------------------------------------------------------------------")
try:
# ---Approach II - Iterate over a List (element by element) using Indexing---
# Initialize List
marks = [60,60,80,61]
# Iterate over a List (element by element) using for loop with Indexing
print(" ---Approach II - Iterate over a List (element by element) using Indexing---")
print("---Traverse a List (Element by Element) using for Loop with Indexing---")
for element in range(0, len(marks)):
print("marks[",element,"]: ",marks[element])
except:
print('Error Occurred')
print("----------------------------------------------------------------------------")
try:
# ---Approach III - Iterate over a List (element by element) using Slicing---
# Initialize List
marks = [60,60,80,61]
counter = 0
print("---Approach III - Iterate over a List (element by element) using Slicing---")
# Iterate over a List (element by element) using for loop with Slicing
print("---Traverse a List (Element by Element) using for Loop with Slicing---")
# Slicing Operator => string[starting index : ending index : step value]
for element in marks[0 : len(marks): 1]:
counter = counter + 1
print("marks[",counter,"]: ",element)
except:
print('Error Occurred')
print("----------------------------------------------------------------------------")
try:
# ---Approach IV - Iterate over a List (element by element) for loop using enumerate() Function---
# Initialize List
marks = [60,60,80,61]
counter = 0
print("---Approach IV - Iterate over a List (element by element) using enumerate() Function---")
# Iterate over a List (element by element) using for loop using enumerate() Function
print("---Traverse a List (Element by Element) using for Loop with enumerate() Function---")
# Iterate over a List (element by element) using for loop using enumerate() Function
for count, element in enumerate(marks):
print (count," ",element)
except:
print('Error Occurred')
---Approach I - Iterate over a List (element by element) using for loop---
---Traverse a List (Element by Element) using for Loop---
60
60
80
61
----------------------------------------------------------------------------
---Approach II - Iterate over a List (element by element) using Indexing---
---Traverse a List (Element by Element) using for Loop with Indexing---
marks[ 0 ]: 60
marks[ 1 ]: 60
marks[ 2 ]: 80
marks[ 3 ]: 61
----------------------------------------------------------------------------
---Approach III - Iterate over a List (element by element) using Slicing---
---Traverse a List (Element by Element) using for Loop with Slicing---
marks[ 1 ]: 60
marks[ 2 ]: 60
marks[ 3 ]: 80
marks[ 4 ]: 61
----------------------------------------------------------------------------
---Approach IV - Iterate over a List (element by element) using enumerate() Function---
---Traverse a List (Element by Element) using for Loop with enumerate() Function---
0 60
1 60
2 80
3 61
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Lists and answer the questions given below
- prophets = [‘Hazrat Muhammad (PBUH)’, ‘Hazrat Ibrahim (A.S.)’, ‘Hazrat Mosa (A.S.)’, ‘Hazrat Isa (A.S.)’, ‘Hazrat Adam (A.S.)’]
- ages = [25, 30, 15, 20, 22, 33]
- Questions
- Traverse selected lists using following methods (as shown in this Chapter)
- Method 1: Traversing an Entire List
- Method 2: Traversing a List (Element by Element)
- Traverse selected lists using following methods (as shown in this Chapter)
- Consider the following two Lists and answer the questions given below
Your Turn Task 1
- Task
- Consider two Lists (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Traverse selected lists using following methods (as shown in this Chapter)
- Method 1: Traversing an Entire List
- Method 2: Traversing a List (Element by Element)
- Traverse selected lists using following methods (as shown in this Chapter)
Operation 4 – Searching
- Methods to Search Element(s) from a List
- In Sha Allah, in next Slides, I will present a method to search element(s) from a List
- Method 1: Search a Specific Element from a List
- Example 1 – Search a Specific Element from a List – Using in / not in Operator
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search a Specific Element from a List
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 list
'''
# Approach I – Using in Operator to Search Element in a List
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print("---Approach I – Using in Operator to Search Element in a List---")
element = 'Hazrat Uthman ibn Affan (R.A.)'
# Using in operator to Search Element in a List
if element in caliphs_of_islam:
print(element + " is in List")
else:
print(element + " is not in List")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Element in a List
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print("---Approach II – Using not in Operator to Search Element in a List---")
element = 'Hazrat Uthman ibn Affan (R.A.)'
# Using not in operator to Search Element in a List
if element not in caliphs_of_islam:
print(element + " is not in List")
else:
print(element + " is in List")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Element in a List---
Hazrat Uthman ibn Affan (R.A.) is in List
-------------------------------------------
---Approach II – Using not in Operator to Search Element in a List---
Hazrat Uthman ibn Affan (R.A.) is in List
- Example 2 – Search a Specific Element from a List – Using in / not in Operator
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search a Specific Element from a List
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 list
'''
# Approach I – Using in Operator to Search Element in a List
try:
# Initialize List
marks = [60,60,80,61]
print("---Approach I – Using in Operator to Search Element in a List---")
element = 60
# Using in operator to Search Element in a List
if element in marks:
print(element, "is in List")
else:
print(element, "is not in List")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Element in a List
try:
# Initialize List
marks = [60,60,80,61]
print("---Approach II – Using not in Operator to Search Element in a List---")
element = 90
# Using not in operator to Search Element in a List
if element not in marks:
print(element, "is not in List")
else:
print(element, "is in List")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Element in a List---
60 is in List
-------------------------------------------
---Approach II – Using not in Operator to Search Element in a List---
90 is not in List
Search Specific Elements from a List
- Example 1 – Search Specific Elements from a List – Using in / not in Operator
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search Specific Elements from a List
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 list and display the result on the Output Screen
'''
# Approach I – Using in Operator to Search Element in a List
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print("---Approach I – Using in Operator to Search Element in a List---")
element1 = 'Hazrat Umar ibn Khattab (R.A.)'
element2 = 'Hazrat Uthman ibn Affan (R.A.)'
# Using in operator to Search Element in a List
if (element1 in caliphs_of_islam) and (element2 in caliphs_of_islam):
print("Elements are in List")
else:
print("Elements are not in List")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Element in a List
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print("---Approach II – Using not in Operator to Search Element in a List---")
element1 = 'Hazrat Umar ibn Khattab (R.A.)'
element2 = 'Hazrat Uthman ibn Affan (R.A.)'
# Using not in operator to Search Element in a List
if (element1 not in caliphs_of_islam) and (element2 not in caliphs_of_islam):
print("Elements are not in List")
else:
print("Elements are in List")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Element in a List---
Elements are in List
-------------------------------------------
---Approach II – Using not in Operator to Search Element in a List---
Elements are in List
- Example 2 – Search Specific Elements from a List – Using in / not in Operator
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Search Specific Elements from a List
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 list and display the result on the Output Screen
'''
# Approach I – Using in Operator to Search Element in a List
try:
# Initialize List
marks = [60,60,80,61]
print("---Approach I – Using in Operator to Search Element in a List---")
element1 = 60
element2 = 80
# Using in operator to Search Element in a List
if (element1 in marks) and (element2 in marks):
print("Elements are in List")
else:
print("Elements are not in List")
except:
print('Error Occurred')
print("-------------------------------------------")
# Approach II – Using not in Operator to Search Element in a List
try:
# Initialize List
marks = [60,60,80,61]
print("---Approach II – Using not in Operator to Search Element in a List---")
element1 = 90
element2 = 100
# Using not in operator to Search Element in a List
if (element1 not in marks) and (element2 not in marks):
print("Elements are not in List")
else:
print("Elements are in List")
except:
print('Error Occurred')
---Approach I – Using in Operator to Search Element in a List---
Elements are in List
-------------------------------------------
---Approach II – Using not in Operator to Search Element in a List---
Elements are not in List
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Lists 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 Lists and answer the questions given below
- Questions
- Search from selected lists using following methods (as shown in this Chapter)
- Method 1: Search Specific Character from a List
- Search from selected lists using following methods (as shown in this Chapter)
Your Turn Task 1
- Task
- Consider the following two Lists 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 Lists and answer the questions given below
- Questions
- Search from selected lists using following methods (as shown in this Chapter)
- Method 1: Search Specific Character from a List
- Search from selected lists using following methods (as shown in this Chapter)
Operation 5 – Sorting
- Methods to Sorting a List
- In Sha Allah, in next Slides, I will present three methods of sorting a List
- Method 1: Sort a List Alphabetically
- Method 2: Sort a List in Ascending Order
- Method 3: Sort a List in Descending Order
- sorted() Function
- Function Name
- sort()
- Syntax
list.sort([func])
OR
list.sort(key, reverse)
- Purpose
- The main purpose of sorted() function is to
- Alphabetically sort a List
- The main purpose of sorted() function is to
Sort a List Alphabetically
- Example 1 – Sort a List Alphabetically – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a List 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 list alphabetically
'''
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print('---Original List before using sort() Function---')
print(caliphs_of_islam)
# Using sort() Function to sort a List Alphabetically
caliphs_of_islam.sort()
print('---New List after using sort() Function---')
print(caliphs_of_islam)
except:
print('Error Occurred')
---Original List before using sort() Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
---New List after using sort() Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)']
- Example 2 – Sort a List Alphabetically – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a List 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 List alphabetically
'''
try:
# Initialize List
marks = [60,60,80,61]
print('---Original List before using sort() Function---')
print(marks)
# Using sort() Function to sort a List Alphabetically
marks.sort()
print('---New List after using sort() Function---')
print(marks)
except:
print('Error Occurred')
---Original List before using sort() Function---
[60, 60, 80, 61]
---New List after using sort() Function---
[60, 60, 61, 80]
Sort a List in Ascending Order
- Example 1 – Sort a List in Ascending Order – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a List 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 List in ascending order
'''
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print('---Original List before using sort() Function---')
print(caliphs_of_islam)
# Using sort() Function to Sort a List in Ascending Order
caliphs_of_islam.sort(reverse = False)
print('---New List after using sort() Function---')
print(caliphs_of_islam)
except:
print('Error Occurred')
---Original List before using sort() Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
---New List after using sort() Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)']
- Example 2 – Sort a List in Ascending Order – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a List 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 List in ascending order
'''
try:
# Initialize List
marks = [60,60,80,61]
print('---Original List before using sort() Function---')
print(marks)
# Using sort() Function to Sort a List in Ascending Order
marks.sort(reverse = False)
print('---New List after using sort() Function---')
print(marks)
except:
print('Error Occurred')
---Original List before using sort() Function---
[60, 60, 80, 61]
---New List after using sort() Function---
[60, 60, 61, 80]
Sort a List in Descending Order
- Example 1 – Sort a List in Descending Order – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a List 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 List in descending order
'''
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print('---Original List before using sort() Function---')
print(caliphs_of_islam)
# Using sort() Function to Sort a List in Descending Order
caliphs_of_islam.sort(reverse = True)
print('---New List after using sort() Function---')
print(caliphs_of_islam)
except:
print('Error Occurred')
---Original List before using sort() Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
---New List after using sort() Function---
['Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)', 'Hazrat Abu Bakr Siddique (R.A.)']
- Example 2 – Sort a List in Descending Order – Using sort() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Sort a List 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 List in descending order
'''
# Approach I – Using [] to initialize a List
try:
# Initialize List
marks = [60,60,80,61]
print('---Original List before using sort() Function---')
print(marks)
# Using sort() Function to Sort a List in Descending Order
marks.sort(reverse = True)
print('---New List after using sort() Function---')
print(marks)
except:
print('Error Occurred')
---Original List before using sort() Function---
[60, 60, 80, 61]
---New List after using sort() Function---
[80, 61, 60, 60]
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Lists 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 Lists and answer the questions given below
- Questions
- Sort Elements of the selected lists using following methods (as shown in this Chapter)
- Method 1: Sort a List Alphabetically
- Method 2: Sort a List in Ascending Order
- Method 3: Sort a List in Descending Order
- Sort Elements of the selected lists using following methods (as shown in this Chapter)
Your Turn Task 1
- Task
- Consider two Lists (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Sort Elements of the selected lists using following methods (as shown in this Chapter)
- Method 1: Sort a List Alphabetically
- Method 2: Sort a List in Ascending Order
- Method 3: Sort a List in Descending Order
- Sort Elements of the selected lists using following methods (as shown in this Chapter)
Operation 6 – Merging
- Methods of Merging Lists
- In Sha Allah, in next Slides, I will present four methods to merge / concatenate Lists
- Method 1: Concatenate Lists using + Operator
- Method 2: Concatenate Lists using * Operator
- Method 3: Merge Lists using append() Function
- Method 4: Merge Lists using extend() Function
- extend() Function
- Function Name
- extend()
- Syntax
list.extend(seq)
- Purpose
- The main purpose of extend() function is to
- appends the contents of seq to list
- The main purpose of extend() function is to
Concatenate Lists using + Operator
- Example 1 – Concatenate Lists - Using + Operator
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Concatenate Lists 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 lists using + operator
'''
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
ruling_era = [2, 10, 12, 6]
print('---Original Lists before Merging---')
print(caliphs_of_islam)
print(ruling_era)
# Using + Operator to Concatenate Lists
print('---New List after Merging---')
for count in range(len(caliphs_of_islam)):
Merge_caliphs_era = caliphs_of_islam[count] + " - " + str(ruling_era[count])
print(Merge_caliphs_era)
except:
print("Error Occurred")
print("\n\n")
---Original Lists before Merging---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
[2, 10, 12, 6]
---New List after Merging---
Hazrat Abu Bakr Siddique (R.A.) - 2
Hazrat Umar ibn Khattab (R.A.) - 10
Hazrat Uthman ibn Affan (R.A.) - 12
Hazrat Ali ibn Abi Talib (R.A.) – 6
Concatenate Lists using * Operator
- Example 1 – Concatenate Lists - Using * Operator
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Concatenate Lists 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 lists using * operator
'''
try:
# Initialize List
caliphs_of_islam = list(('Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)'))
ruling_era = [2, 10, 12, 6]
print('---Original List before Merging---')
print(caliphs_of_islam)
print(ruling_era)
# Using * Operator to Concatenate Lists
print('---New List after Merging---')
for count in range(len(caliphs_of_islam)):
merge_caliphs_era = [*(caliphs_of_islam[count], ruling_era[count])]
print(merge_caliphs_era)
except:
print("Error Occurred")
---Original List before Merging---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
[2, 10, 12, 6]
---New List after Merging---
['Hazrat Abu Bakr Siddique (R.A.)', 2]
['Hazrat Umar ibn Khattab (R.A.)', 10]
['Hazrat Uthman ibn Affan (R.A.)', 12]
['Hazrat Ali ibn Abi Talib (R.A.)', 6]
Merge Lists using append() Function
- Example 1 – Merge Lists - Using append() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Merge Lists 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 lists using append() function
'''
try:
# Initialize List
caliphs_of_islam = list(('Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)'))
ruling_era = list((2, 10, 12, 6))
print('---Original List before Merging---')
print(caliphs_of_islam)
print(ruling_era)
# Using append() Function to Concatenate Lists
print('---New List after Merging---')
for element in ruling_era:
caliphs_of_islam.append(element)
print(caliphs_of_islam)
except:
print("Error Occurred")
---Original List before Merging---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
[2, 10, 12, 6]
---New List after Merging---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)', 2, 10, 12, 6]
Merge Lists using extend() Function
- Example 1 – Merge Lists - Using extend() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Merge Lists 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 lists using extend()
'''
try:
# Initialize List
caliphs_of_islam = list(('Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)'))
ruling_era = [2, 10, 12, 6]
print('---Original List before Merging---')
print(caliphs_of_islam)
print(ruling_era)
# Using extend() Function to Concatenate Lists
print('---New List after Merging---')
caliphs_of_islam.extend(ruling_era)
print(caliphs_of_islam)
except:
print("Error Occurred")
---Original List before Merging---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
[2, 10, 12, 6]
---New List after Merging---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)', 2, 10, 12, 6]
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Lists 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 Lists and answer the questions given below
- Questions
- Merge Elements of the selected lists using following methods (as shown in this Chapter)
- Method 1: Concatenate Lists using + Operator
- Method 2: Concatenate Lists using * Operator
- Method 3: Merge Lists using append() Function
- Method 4: Merge Lists using extend() Function
- Merge Elements of the selected lists using following methods (as shown in this Chapter)
Your Turn Task 1
- Task
- Consider two Lists (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Merge Elements of the selected lists using following methods (as shown in this Chapter)
- Method 1: Concatenate Lists using + Operator
- Method 2: Concatenate Lists using * Operator
- Method 3: Merge Lists using append() Function
- Method 4: Merge Lists using extend() Function
- Merge Elements of the selected lists using following methods (as shown in this Chapter)
Operation 7 – Updation
- Methods of Updating a List
- In Sha Allah, in next Slides, I will present two methods to update a List
- Method 1: Update Entire List using Slicing
- Method 2: Update List Element by Element using Indexing
Update Entire List using Slicing
- Example 1 – Update Entire List - Using Slicing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Update Entire List 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 list using Slicing
'''
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print('---Original List before Updating---')
print(caliphs_of_islam)
# Update Entire List using Slicing
print('---New List after Updating---')
caliphs_of_islam[0:4] = ['Hazrat Abu Bakr Siddique (R.A.) – 2','Hazrat Umar ibn Khattab (R.A.) - 10','Hazrat Uthman ibn Affan (R.A.) - 12','Hazrat Ali ibn Abi Talib (R.A.) - 6']
print(caliphs_of_islam)
except:
print("Error Occurred")
---Original List before Updating---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
---New List after Updating---
['Hazrat Abu Bakr Siddique (R.A.) – 2', 'Hazrat Umar ibn Khattab (R.A.) - 10', 'Hazrat Uthman ibn Affan (R.A.) - 12', 'Hazrat Ali ibn Abi Talib (R.A.) - 6']
- Example 2 – Update Entire List - Using Slicing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Update Entire List 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 list using Slicing
'''
try:
# Initialize List
marks = [60,60,80,61]
print('---Original List before Updating---')
print(marks)
# Update Entire List using Slicing
print('---New List after Updating---')
marks[0:4] = ['Fatima – 60','Zainab - 60','Abdullah - 80', 'Ibrahim - 61']
print(marks)
except:
print("Error Occurred")
---Original List before Updating---
[60, 60, 80, 61]
---New List after Updating---
['Fatima – 60', 'Zainab - 60', 'Abdullah - 80', 'Ibrahim - 61']
Update List Element by Element using Indexing
- Example 1 – Update List Element by Element - Using Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Update List 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 list element by element using indexing
'''
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print('---Original List before Updating---')
print(caliphs_of_islam)
# Update List Element by Element using Indexing
print('---New List after Updating---')
caliphs_of_islam[2] = 'Hazrat Usman ibn Affan (R.A.)
print(caliphs_of_islam)
except:
print("Error Occurred")
---Original List before Updating---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
---New List after Updating---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Usman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
- Example 2 – Update List Element by Element - Using Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Update List 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 list element by element using indexing
'''
try:
# Initialize List
marks = [60,60,80,61]
print('---Original List before Updating---')
print(marks)
# Update List Element by Element using Indexing
print('---New List after Updating---')
marks[1] = 70
print(marks)
except:
print("Error Occurred")
---Original List before Updating---
[60, 60, 80, 61]
---New List after Updating---
[60, 70, 80, 61]
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Lists 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 Lists and answer the questions given below
- Questions
- Update Elements of the selected lists using following methods (as shown in this Chapter)
- Method 1: Update Entire List using Slicing
- Method 2: Update List Element by Element using Indexing
- Update Elements of the selected lists using following methods (as shown in this Chapter)
Your Turn Task 1
- Task
- Consider two Lists (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Update Elements of the selected lists using following methods (as shown in this Chapter)
- Method 1: Update Entire List using Slicing
- Method 2: Update List Element by Element using Indexing
- Update Elements of the selected lists using following methods (as shown in this Chapter)
Operation 8 – Deletion
- Methods to Search Character from a List
- In Sha Allah, in next Slides, I will present four methods to delete a List
- Method 1: Delete Entire List using del Function
- Method 2: Remove All Elements of a List using clear() Function
- Method 3: Remove Specific Element from a List using remove() Function
- Method 4: Remove an Element from a List using pop() Function
- del Function
- Function Name
- del
- Syntax
del list
- Purpose
- The main purpose of del Function is to
- delete a single element, multiple elements, or the entire list
- The main purpose of del Function is to
- clear() Function
- Function Name
- clear()
- Syntax
list.clear()
- Purpose
- The main purpose of clear() Function is to
- removes all items from the list
- The main purpose of clear() Function is to
- remove() Function
- Function Name
- remove()
- Syntax
list.remove(obj)
- Purpose
- The main purpose of del Function is to
- search for the given element in a List and removes the first matching element
- The main purpose of del Function is to
- pop() Function
- Function Name
- pop()
- Syntax
list.pop(obj = list[-1])
- Purpose
- The main purpose of del Function is to
- removes an item at the specified index from the list
- The main purpose of del Function is to
Delete Entire List using del Function
- Example 1 – Delete Entire List - Using del
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Delete Entire List - 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 list using del
'''
# Approach I – Delete an Entire List using del Function
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print('---Original List before using del Function---')
print(caliphs_of_islam)
# Delete Entire List using del Function
print('---New List after using del Function---')
del caliphs_of_islam
print(caliphs_of_islam)
except Exception as ex:
print(ex)
print("-------------------------------------------")
# Approach II – Delete a Single Element from a List using del Function
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print('---Original List before using del Function---')
print(caliphs_of_islam)
# Delete a Single Element using del
print('---New List after using del Function---')
del caliphs_of_islam[0]
print(caliphs_of_islam)
except:
print("Error Occurred")
print("-------------------------------------------")
# Approach III – Delete Multiple Elements from a List using del Function
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print('---Original List before using del Function---')
print(caliphs_of_islam)
# Delete a Multiple Elements using del
print('---New List after using del Function---')
del caliphs_of_islam[1:3]
print(caliphs_of_islam)
except:
print("Error Occurred")
---Original List before using del Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
---New List after using del Function---
name 'caliphs_of_islam' is not defined
-------------------------------------------
---Original List before using del Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
---New List after using del Function---
['Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
-------------------------------------------
---Original List before using del Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
---New List after using del Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
- Example 2 – Delete Entire List - Using del
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Delete Entire List - 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 list using del
'''
# Approach I – Delete an Entire List using del Function
try:
# Initialize List
marks = [60,60,80,61]
print('---Original List before using del Function---')
print(marks)
# Delete Entire List using del Function
print('---New List after using del Function---')
del marks
print(marks)
except Exception as ex:
print(ex)
print("-------------------------------------------")
# Approach II – Delete a Single Element from a List using del Function
try:
# Initialize List
marks = [60,60,80,61]
print('---Original List before using del Function---')
print(marks)
# Delete a Single Element using del
print('---New List after using del Function---')
del marks[2]
print(marks)
except:
print("Error Occurred")
print("-------------------------------------------")
# Approach III – Delete Multiple Elements from a List using del Function
try:
# Initialize List
marks = [60,60,80,61]
print('---Original List before using del Function---')
print(marks)
# Delete a Multiple Elements using del
print('---New List after using del Function---')
del marks[1:3]
print(marks)
except:
print("Error Occurred")
---Original List before using del Function---
[60, 60, 80, 61]
---New List after using del Function ---
name 'marks' is not defined
-------------------------------------------
---Original List before using del Function---
[60, 60, 80, 61]
---New List after using del Function ---
[60, 60, 61]
-------------------------------------------
---Original List before using del Function---
[60, 60, 80, 61]
---New List after using del Function---
[60, 61]
Remove Entire List using clear() Function
- Example 1 – Remove Entire List - Using clear() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Entire List
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 list
'''
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print('---Original List before using clear() Function---')
print(caliphs_of_islam)
# Remove Entire List using clear() Function
print('---New List after using clear() Function---')
caliphs_of_islam.clear()
print(caliphs_of_islam)
except:
print("Error Occurred")
---Original List before using clear() Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
---New List after using clear() Function---
[]
- Example 2 – Remove Entire List - Using clear() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Entire List 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 list
'''
try:
# Initialize List
marks = [60,60,80,61]
print('---Original List before using clear() Function---')
print(marks)
# Remove Entire List using clear() Function
print('---New List after using clear() Function---')
marks.clear()
print(marks)
except:
print("Error Occurred")
---Original List before using clear() Function---
[60, 60, 80, 61]
---New List after using clear() Function---
[]
Remove Specific Element from a List using remove() Function
- Example 1 – Remove Specific Element from a List - Using remove() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific Element from the List
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 List
'''
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print('---Original List before using remove() Function---')
print(caliphs_of_islam)
# Remove Specific Element from a List using remove() Function
print('---New List after using remove() Function---')
caliphs_of_islam.remove('Hazrat Umar ibn Khattab (R.A.)')
print(caliphs_of_islam)
except:
print("Error Occurred")
---Original List before using remove() Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
---New List after using remove() Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
- Example 2 – Remove Specific Element from a List - Using remove() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific Element from the List
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 List
'''
try:
# Initialize List
marks = [60,60,80,61]
print('---Original List before using remove() Function---')
print(marks)
# Remove Specific Element from a List using remove() Function
print('---New List after using remove() Function---')
marks.remove(60)
print(marks)
except:
print("Error Occurred")
---Original List before using remove() Function---
[60, 60, 80, 61]
---New List after using remove() Function---
[60, 80, 61]
Remove Specific / Last Element from a List using pop() Function
- Example 1 – Remove Specific / Last Element from a List - Using pop() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific / Last Element from the List
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 List
'''
# Approach I - Remove Specific Element from a List using pop() Function
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print('---Original List before using pop() Function---')
print(caliphs_of_islam)
# Remove Specific Element from a List using pop() Function
print('---New List after using pop() Function---')
caliphs_of_islam.pop(3)
print(caliphs_of_islam)
except:
print("Error Occurred")
print("-------------------------------------------")
# Approach II - Remove Last Element from a List using pop() Function
try:
# Initialize List
caliphs_of_islam = ['Hazrat Abu Bakr Siddique (R.A.)','Hazrat Umar ibn Khattab (R.A.)','Hazrat Uthman ibn Affan (R.A.)','Hazrat Ali ibn Abi Talib (R.A.)']
print('---Original List using pop() Function---')
print(caliphs_of_islam)
# Remove Last Element from a List using pop() Function
print('---New List after using pop() Function---')
caliphs_of_islam.pop()
print(caliphs_of_islam)
except:
print("Error Occurred")
---Original List before using pop() Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
---New List after using pop() Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)']
-------------------------------------------
---Original List using pop() Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)', 'Hazrat Ali ibn Abi Talib (R.A.)']
---New List after using pop() Function---
['Hazrat Abu Bakr Siddique (R.A.)', 'Hazrat Umar ibn Khattab (R.A.)', 'Hazrat Uthman ibn Affan (R.A.)']
- Example 2 – Remove Specific / Last Element from a List - Using pop() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Specific / Last Element from the List
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 List
'''
# Approach I - Remove Specific Element from a List using pop() Function
try:
# Initialize List
marks = [60,60,80,61]
print('---Original List before using pop() Function---')
print(marks)
# Remove Specific Element from a List using pop() Function
print('---New List after using pop() Function---')
marks.pop(2)
print(marks)
except:
print("Error Occurred")
print("-------------------------------------------")
# Approach II - Remove Last Element from a List using pop() Function
try:
# Initialize List
marks = [60,60,80,61]
print('---Original List before using pop() Function---')
print(marks)
# Remove Last Element from a List using pop() Function
print('---New List after using pop() Function---')
marks.pop()
print(marks)
except:
print("Error Occurred")
---Original List before using pop() Function---
[60, 60, 80, 61]
---New List after using pop() Function---
[60, 60, 61]
-------------------------------------------
---Original List before using pop() Function---
[60, 60, 80, 61]
---New List after using pop() Function---
[60, 60, 80]
TODO and Your Turn
TODO Task 1
- Task
- Consider the following two Lists and answer the questions given below
- Questions
- Remove Elements from the selected lists using following methods (as shown in this Chapter)
- Method 1: Remove Entire / Element(s) from List using del
- Method 2: Remove Entire List using clear() Function
- Method 3: Remove Specific Element from a List using remove() Function
- Method 4: Remove Last Element from a List using pop() Function
- Remove Elements from the selected lists using following methods (as shown in this Chapter)
Your Turn Task 1
- Task
- Consider two Lists (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Remove Elements from the selected lists using following methods (as shown in this Chapter)
- Method 1: Remove Entire / Element(s) from List using del
- Method 2: Remove Entire List using clear() Function
- Method 3: Remove Specific Element from a List using remove() Function
- Method 4: Remove Last Element from a List using pop() Function
- Remove Elements from the selected lists using following methods (as shown in this Chapter)
List Comprehension
- List Comprehension
- Definition
- List Comprehension provides a short and simple way to create a new list based on the values of an existing interrable (tuple, set, list etc.)
- Strengths
- List Comprehension
- is more time-efficient and space-efficient compared to loops
- requires fewer lines of code
- transforms iterative statement into a formula
- List Comprehension
- List Comprehension
newlist = [expression for item in iterable if condition == True]
- Example 1 – List Comprehension
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = List Comprehension
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 new list based on values of an existing list
'''
try:
# List Initialization
qul_shareef = ['Al-Kafiroon', 'Al-Ikhlas', 'Al-Falaq', 'Al-Naas']
# Create a new list with element of previous List i.e., qul_shareef
list_comprehension = [element for element in qul_shareef if "la" in element]
print("New List containing alphabets 'al':",list_comprehension)
except:
print("Error Occurred")
New List containing alphabets 'al': ['Al-Ikhlas', 'Al-Falaq']
- Example 2 – List Comprehension
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = List Comprehension
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 new list base on values of an existing list
'''
try:
# List Initialization
words = ['Allah', 'is', 'one', 'Allah', 'created', 'us', 'Allah', 'has', 'control', 'over', 'everything']
stopwords = ['is', 'us', 'has']
# List Comprehenion
content_words = [word for word in words if word not in stopwords]
print("New List containing 'content words':\n", content_words)
except:
print("Error Occurred")
New List containing 'content words':
['Allah', 'one', 'Allah', 'created', 'Allah', 'control', 'over', 'everything']
TODO and Your Turn
TODO Task 1
- Task
- Consider the following Lists and answer the questions given below
- list_of_cars = [‘Mehran’, ‘Mercedes’, ‘Cultus’, ‘WagonR’]
- prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19]
- Consider the following Lists and answer the questions given below
- Questions
- Use list_of_cars to create a new list which contains names of cars starting with character M?
- Use prime_numbers to create a new list which contains number less than 10?
Your Turn Task 1
- Task
- Consider the following Lists and answer the questions given below
- list_of_cars = [‘Mehran’, ‘Mercedes’, ‘Cultus’, ‘WagonR’]
- prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19]
- Consider the following Lists and answer the questions given below
- Questions
- Use list_of_cars to create a new list which contains names of cars starting with character M?
- Use prime_numbers to create a new list which contains number less than 10?
Nested List
- Nested List
- Definition
- A Nest List is a list of list(s)
Creating a Nested List
- Syntax - Creating a Nested List
- The syntax to create a Nested List using [] is as follows
LIST_NAME = [
[, , ],
[, , ],
.
.
.
[, , ]
]
- Example 1 - Creating a Nested List
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Create a Nested List
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 list
'''
# Initializing a Nested List 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("List of Students for Introduction to Python:")
print(students)
# Data-Type of List i.e., students
print("Data-Type of students:", type(students))
# Length of List i.e., students
print("Length of students:", len(students))
except:
print("Error Occurred")
List 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: 9Length of studnets: 9
Access Elements of a Nested List
- Access Elements of a Nested List
- You can access individual items in a Nested List using multiple indexes
- Example 1 - Access Elements of a Nested List – Using Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Elements of Nested List
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 list using indexing
'''
# Initializing a Nested List i.e., Student
try:
students = [
['Name', 'Age', 'Gender'],
'1',['Fatima', '21', 'Female'],
'2',['Zainab', '22', 'Female'],
'3',['Ali', '20', 'Male'],
'4',['Usman', '20', 'Male']
]
# Access Elements of Nested List 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 List – Using Negative Indexing
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Access Elements of Nested List
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 list using indexing
'''
# Initializing a Nested List i.e., Student
try:
students = [
['Name', 'Age', 'Gender'],
'1',['Fatima', '21', 'Female'],
'2',['Zainab', '22', 'Female'],
'3',['Ali', '20', 'Male'],
'4',['Usman', '20', 'Male']
]
# Access Elements of Nested List 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 List
- In Sha Allah, in next Slides, I will present three methods to add element(s) in a Nested List
- Method 1 – Add a New Element at the Start of a Nested List
- Method 2 – Add a New Element at the End of a Nested List
- Method 3 – Add a New Element at a Desired Location of a Nested List
- Example 1 - Add a New Element in a Nested List
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Add Elements in a Nested List
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 list
'''
# Initializing a Nested List 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 List
print("---Approach I – Add a New Element at the Start of a List---")
# Display Original List
print("---Original Nested List---")
print(students)
print("------------------------------------------------")
# Display List after Adding a New Element at the Start of a List
print("---New List after using insert() Function---")
# Adding Element at the Start of a List using insert() Function
students.insert(0,'List of Students of Introduction to Python')
print(students)
print("\n*************************************************\n")
# Approach II – Add a New Element at the End of a List
print("---Approach II – Add a New Element at the End of a List---")
# Display Original List
print("---Original Nested List---")
print(students)
print("------------------------------------------------")
# Display List after Adding a New Element at the End of a List
print("---New List after using append() Function---")
students[1].append('Email')
students[3].append('fatima@gmail.com')
students[5].append('zainab@yahoo.com')
students[7].append('ali@hotmail.com')
students[9].append('usman@gmail.com')
students.append([5,['Umer','26','Male','umer@hotmail.com']])
print(students)
print("\n*************************************************\n")
# Approach III – Add a New Element at the End of a List
print("---Approach III – Add a New Element at the End of a List---")
# Display Original List
print("---Original Nested List---")
print(students)
print("------------------------------------------------")
# Display List after Adding a New Element at the End of a List
print("---New List after using extend() Function---")
students.extend([6,['Laiba','22','Female','laiba@gmail.com']])
print(students)
print("\n*************************************************\n")
# Approach IV – Add a New Element at the Desired Location of a List
print("---Approach IV – Add a New Element at the Desired Location of a List---")
# Display Original List
print("---Original Nested List---")
print(students)
print("------------------------------------------------")
# Display List after Adding a New Element at the Desired Location of a List
print("---New List after using insert() Function---")
# Adding Element at the Desired Location of a List
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 List---
---Original Nested List---
[['Name', 'Age', 'Gender'], 1, ['Fatima', '21', 'Female'], 2, ['Zainab', '22', 'Female'], 3, ['Ali', '20', 'Male'], 4, ['Usman', '20', 'Male']]
------------------------------------------------
---New List after using insert() Function---
['List 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 List---
---Original Nested List---
['List 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 List after using append() Function---
['List of Students of Introduction to Python', ['Name', 'Age', 'Gender', 'Email'], 1, ['Fatima', '21', 'Female', 'fatima@gmail.com'], 2, ['Zainab', '22', 'Female', 'zainab@yahoo.com'], 3, ['Ali', '20', 'Male', 'ali@hotmail.com'], 4, ['Usman', '20', 'Male', 'usman@gmail.com'], [5, ['Umer', '26', 'Male', 'umer@hotmail.com']]]
*************************************************
---Approach III – Add a New Element at the End of a List---
---Original Nested List---
['List of Students of Introduction to Python', ['Name', 'Age', 'Gender', 'Email'], 1, ['Fatima', '21', 'Female', 'fatima@gmail.com'], 2, ['Zainab', '22', 'Female', 'zainab@yahoo.com'], 3, ['Ali', '20', 'Male', 'ali@hotmail.com'], 4, ['Usman', '20', 'Male', 'usman@gmail.com'], [5, ['Umer', '26', 'Male', 'umer@hotmail.com']]]
------------------------------------------------
---New List after using extend() Function---
['List of Students of Introduction to Python', ['Name', 'Age', 'Gender', 'Email'], 1, ['Fatima', '21', 'Female', 'fatima@gmail.com'], 2, ['Zainab', '22', 'Female', 'zainab@yahoo.com'], 3, ['Ali', '20', 'Male', 'ali@hotmail.com'], 4, ['Usman', '20', 'Male', 'usman@gmail.com'], [5, ['Umer', '26', 'Male', 'umer@hotmail.com']], 6, ['Laiba', '22', 'Female', 'laiba@gmail.com']]
*************************************************
---Approach IV – Add a New Element at the Desired Location of a List---
---Original Nested List---
['List of Students of Introduction to Python', ['Name', 'Age', 'Gender', 'Email'], 1, ['Fatima', '21', 'Female', 'fatima@gmail.com'], 2, ['Zainab', '22', 'Female', 'zainab@yahoo.com'], 3, ['Ali', '20', 'Male', 'ali@hotmail.com'], 4, ['Usman', '20', 'Male', 'usman@gmail.com'], [5, ['Umer', '26', 'Male', 'umer@hotmail.com']], 6, ['Laiba', '22', 'Female', 'laiba@gmail.com']]
------------------------------------------------
---New List after using insert() Function---
['List of Students of Introduction to Python', ['Name', 'Age', 'Gender', 'Email'], 1, ['Fatima', '21', 'Female', 'fatima@gmail.com'], "[['Mustafa', '26', 'Male']]", '3', 2, ['Zainab', '22', 'Female', 'zainab@yahoo.com'], 3, ['Ali', '20', 'Male', 'ali@hotmail.com'], 4, ['Usman', '20', 'Male', 'usman@gmail.com'], [5, ['Umer', '26', 'Male', 'umer@hotmail.com']], 6, ['Laiba', '22', 'Female', 'laiba@gmail.com']]
Traverse a Nested List
- Methods to Traverse a List
- In Sha Allah, in next Slides, I will present two methods to traverse a List
- Method 1: Traversing an Entire Nested List
- Method 2: Traversing a Nested List (Element by Element)
- Example 1 - Traversing an Entire List
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Traversing an Entire List
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 list
'''
# Initializing a Nested List 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 List
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 List (Element by Element)
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Traversing a List (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 list (element by element)
'''
# Initializing a Nested List 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 List (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 List
- Update Elements of a Nested List
- The value of a specific item in a Nested List can be changed by referring to its index number
- Example 1 - Update Elements of a Nested List
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Update Elements of a Nested List
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 list
'''
# Initializing a Nested List 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 List
print("---Original Nested List---")
print(students)
print("--------------------------------------------")
# Display New List after Updating Elements
print("---New Nested List after updating Elements---")
# Assign Values to Existing List
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 List---
[['Name', 'Age', 'Gender'], 1, ['Fatima', '21', 'Female'], 2, ['Zainab', '22', 'Female'], 3, ['Ali', '20', 'Male'], 4, ['Usman', '20', 'Male']]
--------------------------------------------
---New Nested List after updating Elements---
[['Name', 'Age', 'Gender'], 0, ['Mahnoor', '19', 'Female'], 1, ['Ibrahim', '26', 'Male'], 3, ['Ali', '20', 'Male'], 4, ['Usman', '20', 'Male']]
Remove Elements from a Nested List
- Remove Elements from a Nested Tuple
- In Sha Allah, in next Slides, I will present three methods to remove element(s) in a Nested List
- Method 1 – Remove a New Element at the Start of a Nested List
- Method 2 – Remove a New Element at the End of a Nested List
- Method 3 – Remove a New Element at a Desired Location of a Nested List
- Example 1 - Remove Elements from a Nested List
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Remove Elements from a Nested List
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 list
'''
# Initializing a Nested List 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 List
print("---Approach I – Remove an Element from the Start of a List---")
# Display Original List
print("---Original Nested List---")
print(students)
print("------------------------------------------------")
# Display List after Removing an Element from the Start of a List
print("---New List after Removing Element from Start using pop() Function---")
# Removing Element from the Start of a List using pop() Function
students.pop(0)
print(students)
print("\n*************************************************\n")
# Approach II – Removing Element from the End of a List
print("---Approach II – Remove Element from the End of a List---")
# Display Original List
print("---Original Nested List---")
print(students)
print("------------------------------------------------")
# Display List after Removing Element from the End of a List
print("---New List 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 List
print("---Original Nested List---")
print(students)
print("------------------------------------------------")
# Display List after Removing Element from the Desired Location
print("---New List after Removing Element from the Desired Location using remove() Function---")
students.remove(students[1])
print(students)
print("\n*************************************************\n")
# Approach IV – Remove an Entire List
print("---Approach IV – Remove an Entire List---")
# Display Original List
print("---Original Nested List---")
print(students)
print("------------------------------------------------")
# Remove Entire List
print("---New List after Removing an Entire List using del Function---")
del students
print(students)
except Exception as ex:
print(ex)
---Approach I – Remove an Element from the Start of a List---
---Original Nested List---
[['Name', 'Age', 'Gender'], 1, ['Fatima', '21', 'Female'], 2, ['Zainab', '22', 'Female'], 3, ['Ali', '20', 'Male'], 4, ['Usman', '20', 'Male']]
------------------------------------------------
---New List 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 List---
---Original Nested List---
[1, ['Fatima', '21', 'Female'], 2, ['Zainab', '22', 'Female'], 3, ['Ali', '20', 'Male'], 4, ['Usman', '20', 'Male']]
------------------------------------------------
---New List after Removing Element from the End of using del Function---
[1, ['Fatima', '21', 'Female'], 2, ['Zainab', '22', 'Female'], 3, ['Ali', '20', 'Male'], 4]
*************************************************
---Approach III – Remove an Element from the End of a List---
---Original Nested List---
[1, ['Fatima', '21', 'Female'], 2, ['Zainab', '22', 'Female'], 3, ['Ali', '20', 'Male'], 4]
------------------------------------------------
---New List after using remove() Function---
[1, 2, ['Zainab', '22', 'Female'], 3, ['Ali', '20', 'Male'], 4]
*************************************************
---Approach IV – Remove an Entire List---
---Original Nested List---
[1, 2, ['Zainab', '22', 'Female'], 3, ['Ali', '20', 'Male'], 4]
------------------------------------------------
---New List after using del Function---
name 'students' is not defined
TODO and Your Turn
TODO Task 1
- Task
- Consider the following Lists 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 List (as shown in this Chapter)?
- Operation 1: Create a Nested List
- Operation 2: Access Elements of a Nested List
- Operation 3: Add New Element in a Nested List
- Operation 4: Traverse a Nested List
- Operation 5: Update Elements of a Nested List
- Operation 6: Remove Elements of a Nested List
- Apply following operations on the given Nested List (as shown in this Chapter)?
Your Turn Task 1
- Task
- Consider a Nested List (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Apply following operations on selected Nested List (as shown in this Chapter)?
- Operation 1: Create a Nested List
- Operation 2: Access Elements of a Nested List
- Operation 3: Add New Element in a Nested List
- Operation 4: Traverse a Nested List
- Operation 5: Update Elements of a Nested List
- Operation 6: Remove Elements of a Nested List
- Apply following operations on selected Nested List (as shown in this Chapter)?
Lambda Function
- Lambda Function
- Definition
- In Python, a Lambda Function is an anonymous function that is defined without a name
- Note
- Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned.
- Syntax – Lambda Function
lambda arguments: expression
- Example 1 - Lambda Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Lambda Function
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 understand the concept of lambda function
'''
'''
Recall - Simple Functions in Python
def display(message):
return message + " " + 'Allah created us.'
display('Allah is one.')
'''
try:
# Define a Lambda Function
msg = lambda message: message + " " + 'Allah created us.'
# Display Message by calling Lambda Function
print(msg('Allah is one.'))
except:
print("Error Occurred")
Allah is one. Allah created us.
- Example 2 - Lambda Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Lambda Function
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 understand the concept of lambda function
'''
'''
Recall - Simple Functions in Python
def product(number):
return number * 5
product(250)
'''
try:
# Define a Lambda Function
product = lambda number: number * 5
# Display Message by calling Lambda Function
print("Product of numbers are:",product(250))
except:
print("Error Occurred")
Product of numbers are: 1250
- Functions of Lambda Functions
- Following are the Functions used with Lambda Function
- filter()
- map()
- Filter() Function
- Function Name
- filter()
- Definition
- The filter() function is called with all the items in the list and a new list is returned which contains items for which the function evaluates to True
- Syntax
filter(function, iterable)
- Example 1 – Lambda Function – Using filter() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Lambda Function
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 program is to filter only the even items from the List
'''
try:
# List Initiazation
numbers = [2, 4, 6, 8, 11, 13, 15, 18]
print("---Origninal List before using filter() Function---")
print(numbers)
# List after using filter() Function
updated_numbers = list(filter(lambda number: (number % 2 == 0) , numbers))
print("---New List after using filter() Function---")
print(updated_numbers)
except:
print("Error Occurred")
---Origninal List before using filter() Function---
[2, 4, 6, 8, 11, 13, 15, 18]
---New List after using filter() Function---
[2, 4, 6, 8, 18]
- map() Function
- Function Name
- map()
- Definition
- The map() function is called with all the items in the list and a new list is returned which contains items returned by that function for each item
- Syntax
map(function, iterables)
- Example 2 – Lambda Function – Using map() Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Lambda Function
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 program is to add a specific number each item in a list using map()
'''
try:
# List Initiazation
numbers = [2, 4, 6, 8, 11, 13, 15, 18]
print("---Original List before using map() Function---")
print(numbers)
# List after using filter() Function
updated_numbers = list(map(lambda number: number + 10 , numbers))
print("---New List after using filter() Function---")
print(updated_numbers)
except:
print("Error Occurred")
---Original List before using filter() Function---
[2, 4, 6, 8, 11, 13, 15, 18]
---New List after using filter() Function---
[12, 14, 16, 18, 21, 23, 25, 28]
TODO and Your Turn
TODO Task 1
- Task
- Consider the following Lists and answer the questions given below
- list_of_cars = [‘Mehran’, ‘Mercedes’, ‘Cultus’, ‘WagonR’]
- prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19]
- Consider the following Lists and answer the questions given below
- Questions
- Implement the concept of Lambda Function using the following methos dicussed in this Chapter
- Method 1: Use filter() Function
- Method 2: Use map() Function
- Implement the concept of Lambda Function using the following methos dicussed in this Chapter
Your Turn Task 1
- Task
- Consider two Lists (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Implement the concept of Lambda Function using the following methos dicussed in this Chapter
- Method 1: Use filter() Function
- Method 2: Use map() Function
- Implement the concept of Lambda Function using the following methos dicussed in this Chapter
Passing List to a Function
- Passing List to a Function
- In Sha Allah, in the next Slides, I will present how to pass List to a Function
- Example 1 - Passing Lists to a Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Passing Lists 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 lists 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 List
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
Passing Nested List to a Function
- Passing Nested List to a Function
- In Sha Allah, in the next Slides, I will present how to pass nested List to a Function
- Example 1 – Passing Nested Lists to a Function
'''
Author and System Information
author_name = Ms. Samavi Salman
program_name = Passing Nested List 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 lists 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 List
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 Lists and answer the questions given below
- list_of_cars = [‘Mehran’, ‘Mercedes’, ‘Cultus’, ‘WagonR’]
- Consider the following Lists 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 List / Nested List to the Function, Iterate through the Nested List and Display all the elements on the Output Screen (as shown in this Chapter)?
Your Turn Task 1
- Task
- Consider a Nested List (similar to the ones given in TODO Task 1) and answer the questions given below
- Questions
- Pass a List / Nested List to the Function, Iterate through the Nested List 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:
- List
- A List is defined as an ordered collection of items, which may or may not have same data types
- List Declaration
- A List can be declared using two methods
- Method 1: Use []
- Method 2: Use list() Function
- A List can be declared using two methods
- List Initialization
- There are two methods to initialize a List
- Method 1: Use []
- Method 2: Use list() Function
- There are two methods to initialize a List
- Accessing Elements of a List
- There are three methods to access element(s) of a List
- Method 1 – Access Element(s) of a List using Indexing
- Method 2 – Access Element(s) of a List using Negative Indexing
- Method 3 – Access Element(s) of a List using Slicing
- There are three methods to access element(s) of a List
- Operations on List
- Operation 1 – Creation
- Operation 2 – Insertion
- Operation 3 – Traversal
- Operation 4 – Searching
- Operation 5 – Sorting
- Operation 6 – Merging
- Operation 7 – Updation
- Operation 8 – Deletion
- List Comprehension
- List Comprehension provides a short and simple way to create a new List based on the values of an existing interrable (tuple, set, list etc.)
- Nested List
- A Nest List is a list of list(s)
- Lambda Function
- In Python, a Lambda Function is an anonymous function that is defined without a name
In Next Chapter
- In Next Chapter
- In Sha Allah, in the next Chapter, I will present a detailed discussion on
- Dictionaries in Python