Program2(b)
2. b. Write a python program to create a list and perform the following operations
• Inserting an element
• Removing an element
• Appending an element
• Displaying the length of the list
• Popping an element
• Clearing the list
Objective of the Program
To understand Python list data structure
To practice built-in list methods
To perform dynamic operations on a list
To implement menu-driven programs
Logical Construction (Think Before You Code)
Before writing the program, understand the idea clearly.
Create an empty list.
Display a menu with options.
Ask the user to choose an operation.
Based on the choice:
Insert element at given position
Remove specific element
Append element at the end
Display length of list
Pop last element
Clear entire list
Display updated list after each operation.
👉 Key idea: The program modifies the same list repeatedly based on user choice.
Flowchart (Block Diagram of Logic)
Start
Create empty list
Display menu
Read user choice
Perform selected operation
Display updated list
Stop
Algorithm
Start
Create an empty list
Display menu options
Read user choice
If choice = 1 → Insert element
If choice = 2 → Remove element
If choice = 3 → Append element
If choice = 4 → Display length
If choice = 5 → Pop element
If choice = 6 → Clear list
Display updated list
Stop
Python Program
⚠ Students must complete the missing statements.
# Program to perform various list operations
my_list = []
print("1. Insert an element")
print("2. Remove an element")
print("3. Append an element")
print("4. Display length of list")
print("5. Pop an element")
print("6. Clear the list")
choice = int(input("Enter your choice: "))
if choice == 1:
pos = int(input("Enter position: "))
element = input("Enter element: ")
my_list.____________(pos, element)
elif choice == 2:
element = input("Enter element to remove: ")
my_list.____________(element)
elif choice == 3:
element = input("Enter element to append: ")
my_list.____________(element)
elif choice == 4:
print("Length of list =", ____________)
elif choice == 5:
removed = my_list.____________()
print("Popped element =", removed)
elif choice == 6:
my_list.____________()
print("List cleared")
else:
print("Invalid choice")
print("Updated List:", my_list)
Probable Output
Case 1 – Append
Important Concepts Used
List creation
List methods:
insert()remove()append()pop()clear()
len()functionConditional statements
Menu-driven programming
Viva Voce Questions
What is a list in Python?
Difference between
append()andinsert()?What happens if we remove an element not present in list?
What does
pop()return?What is the difference between
clear()and reassigning list?Can lists store different data types?
What is indexing in Python lists?
Procedure to Execute the Program
Open Python IDLE / VS Code / Google Colab
Create a new Python file
Type the program
Save with
.pyextensionRun the program
Enter your choice and required inputs
Google Colab – Online Execution
👉 Paste your Colab link here.
[https://colab.research.google.com/drive/1M6MWdSUepa8L-2-u_8PB1LCrPg9JD8X5?usp=sharing]
Program Explanation
Modify program to run continuously until user selects Exit
Handle exceptions using
try-exceptAllow multiple elements at once
Convert it into a function-based program
Lab Record Instructions
Right Side of the Record
1. Problem Statement
2. Python Program
Write the following neatly on the right-hand side page:
Left Side of the Record
Write the following neatly on the left-hand side page:
3. Flowchart
4. Algorithm
5. Output
⚠️ Students must write ALL possible outputs.

Comments
Post a Comment