Python Programming PRACTICAL NO.7 ANSWERS
Python Programming PRACTICAL NO.7 ANSWERS
ir
Ans.
eS
IX Conclusion:
statement. These operations help in efficiently managing and manipulating list data
in Python.
M
2. Using sorted() (Returns a new sorted list without modifying the
original list)
ir
2) Justify the statement “Lists are mutable” not.
eS
Ans.
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.
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.
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
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:
Key Points:
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
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))
Ans.
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
my_list = [1, 2, 3, 4, 5]
reversed_list = my_list[::-1]
ir
print("Reversed List:", reversed_list)
eS
Output: