🌟 Diplomatech Academy 🌟
Manual Solutions and outputs
Python Programming Course Code: 314004
-Mohade Sir©
Practical No. 7: Write Python program to perform following operations on
Lists:
Create list, Access list, Update list (Add item, Remove item), and Delete list
ir
Ans.
eS
IX Conclusion:
We have successfully implemented Python programs to perform various operations
ad
on lists, including creating, accessing, updating, and deleting lists. We learned how
to create lists with different data types and access elements using indexing and
slicing. We explored updating lists by adding (append(), insert()) and
removing (remove(), pop()) elements. Finally, we deleted lists using the del
oh
statement. These operations help in efficiently managing and manipulating list data
in Python.
M
X Practical related questions
1) Write syntax for a method to sort a list.
Ans.
Syntax to Sort a List in Python
1. Using sort() (Modifies the original list)
list_name.sort() # Ascending order (default)
list_name.sort(reverse=True) # Descending order
2. Using sorted() (Returns a new sorted list without modifying the
original list)
sorted_list = sorted(list_name) # Ascending order
sorted_list = sorted(list_name, reverse=True) # Descending order
ir
2) Justify the statement “Lists are mutable” not.
eS
Ans.
Justification: "Lists are mutable"
In Python, lists are mutable, meaning their elements can be changed after
creation. You can modify a list by adding, removing, or updating elements.
ad
Example:
# Creating a list
numbers = [1, 2, 3, 4]
oh
# Modifying an element
numbers[1] = 10 # Changing 2 to 10
# Adding an element
M
numbers.append(5)
# Removing an element
numbers.remove(3)
print(numbers)
Output:
[1, 10, 4, 5]
Since we can change the contents of a list without changing its identity (memory
address), lists are mutable.
3) Describe various list functions.
Ans.
ir
Various List Functions in Python
append(item) – Adds an item to the end of the list.
eS
lst = [1, 2, 3]
lst.append(4) # [1, 2, 3, 4]
1.
ad
insert(index, item) – Inserts an item at a specified index.
lst.insert(1, 10) # [1, 10, 2, 3]
oh
2.
remove(item) – Removes the first occurrence of the item.
lst.remove(2) # [1, 10, 3]
M
3.
pop(index) – Removes and returns the item at the given index (default: last
item).
lst.pop() # Removes last element
lst.pop(1) # Removes element at index 1
4.
sort() – Sorts the list in ascending order (modifies the original list).
lst.sort() # [1, 3, 10]
5.
sorted(lst) – Returns a new sorted list without modifying the original list.
ir
new_lst = sorted(lst)
6.
eS
reverse() – Reverses the list in place.
lst.reverse() # [10, 3, 1]
ad
4) Describe the use pop operator in list.
Ans.
oh
Use of pop() Operator in Lists
The pop() function in Python is used to remove and return an element from a
list at a specified index. If no index is provided, it removes the last element by
default.
M
Syntax:
list_name.pop(index) # Removes element at the given index
list_name.pop() # Removes the last element if index is not provided
Example 1: Removing the Last Element
numbers = [10, 20, 30, 40]
removed_item = numbers.pop() # Removes 40
print(numbers) # Output: [10, 20, 30]
print("Removed Item:", removed_item) # Output: 40
Key Points:
✅ Modifies the original list
✅ Returns the removed element
ir
✅ Raises an IndexError if the list is empty or the index is out of range
eS
5) Describe various list functions.
Ans.
ad
Common List Functions in Python
1. append(item) – Adds an item to the end.
2. insert(index, item) – Inserts an item at a specific index.
3. remove(item) – Removes the first occurrence of an item.
oh
4. pop(index) – Removes and returns an item (default: last).
5. sort() / sorted(lst) – Sorts the list (modifies / returns new sorted
list).
M
6. reverse() – Reverses the list order.
7. index(item) – Returns index of the first occurrence.
8. count(item) – Counts occurrences of an item.
9. extend(iterable) – Adds multiple items to the list.
10.clear() – Removes all elements.
6) Write a Python program to find common items from two lists.
Ans.
Python Program to Find Common Items from Two Lists
# Define two lists
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
ir
# Find common elements using set intersection
eS
common_items = list(set(list1) & set(list2))
# Display the result
print("Common items:", common_items)
ad
Output:
oh
Common items: [4, 5]
M
7) Write a Python program to reverse a list.
Ans.
Python Program to Reverse a List
Method 1: Using reverse()
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print("Reversed List:", my_list)
Method 2: Using Slicing ([::-1])
my_list = [1, 2, 3, 4, 5]
reversed_list = my_list[::-1]
ir
print("Reversed List:", reversed_list)
eS
Output:
Reversed List: [5, 4, 3, 2, 1]
ad
oh
M