Practical 2: Python Program to Demonstrate Various Operations on Lists
Problem Statement
Write a Python program to demonstrate various operations on lists. The operations should include:
- Adding elements to a list.
- Removing elements from a list.
- Accessing elements using indices.
- Slicing a list.
- Iterating over a list to display elements.
The program should perform these operations on a list of integers and display the results at each step.
Solution Code
# Program to demonstrate operations on lists
# Initializing a list
numbers = [10, 20, 30, 40, 50]
# 1. Adding an element to the list
numbers.append(60) # Adds 60 to the end of the list
print("After appending 60:", numbers)
# 2. Removing an element from the list
numbers.remove(30) # Removes the first occurrence of 30
print("After removing 30:", numbers)
# 3. Accessing elements using indices
first_element = numbers[0] # Accessing the first element
last_element = numbers[-1] # Accessing the last element
print(f"First element: {first_element}, Last element: {last_element}")
# 4. Slicing the list
sliced_list = numbers[1:4] # Get a sublist from index 1 to 3 (not inclusive of index 4)
print("Sliced List:", sliced_list)
# 5. Iterating over the list to display elements
print("Iterating over the list:")
for num in numbers:
print(num)
Output
- The program will initialize a list of integers and perform the following operations:
- Add an element (60) to the list.
- Remove an element (30) from the list.
- Access and display the first and last elements of the list.
- Slice the list to obtain a sublist.
- Iterate over the list and display each element.
After appending 60: [10, 20, 30, 40, 50, 60]
After removing 30: [10, 20, 40, 50, 60]
First element: 10, Last element: 60
Sliced List: [20, 40, 50]
Iterating over the list:
10
20
40
50
60
Process finished with exit code 0
Explanation
Below is the explanation of F.E. PPS Unit 2 Practical 2 solution where we have written Python program to demonstrate various operations on lists.
- Initializing the List:
- The program starts by initializing a list named numbers containing five integers: [10, 20, 30, 40, 50].
- Adding an Element:
- The append() method is used to add an element (60) to the end of the list. After appending, the list becomes [10, 20, 30, 40, 50, 60].
- Removing an Element:
- The remove() method is used to remove the first occurrence of a specified value. Here, 30 is removed from the list, and the list becomes [10, 20, 40, 50, 60].
- Accessing Elements Using Indices:
- Elements can be accessed directly by their indices. The first element is accessed using index 0, and the last element is accessed using index -1.
- Slicing the List:
- The program uses slicing to create a sublist. The syntax numbers[1:4] slices the list from index 1 to 3 (note that index 4 is not included).
- Iterating Over the List:
- A for loop is used to iterate over each element in the list and print it.
Key Concept Learned
- List Operations:
- The practical demonstrates various operations that can be performed on lists in Python, such as adding, removing, accessing, and slicing elements.
- List Methods:
- The methods append() and remove() are used to modify the list by adding and removing elements, respectively.
- List Indexing and Slicing:
- The program shows how to access elements via indices and how to slice the list to obtain a sublist.
- Iteration Over Lists:
- The program demonstrates how to iterate through a list using a for loop, which is a basic technique in Python to process all items in a list.