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.

  1. Create an empty list.

  2. Display a menu with options.

  3. Ask the user to choose an operation.

  4. 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

  5. Display updated list after each operation.

👉 Key idea: The program modifies the same list repeatedly based on user choice.


Flowchart (Block Diagram of Logic)


Flowchart Explanation
  • Start

  • Create empty list

  • Display menu

  • Read user choice

  • Perform selected operation

  • Display updated list

  • Stop


Algorithm

  1. Start

  2. Create an empty list

  3. Display menu options

  4. Read user choice

  5. If choice = 1 → Insert element

  6. If choice = 2 → Remove element

  7. If choice = 3 → Append element

  8. If choice = 4 → Display length

  9. If choice = 5 → Pop element

  10. If choice = 6 → Clear list

  11. Display updated list

  12. 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

Enter your choice: 3
Enter element to append: 10
Updated List: ['10']

Case 2 – Insert
Enter your choice: 1
Enter position: 0
Enter element: 5
Updated List: ['5']

Case 3 – Length
Enter your choice: 4
Length of list = 0
Updated List: []

Case 4 – Clear
Enter your choice: 6
List cleared
Updated List: []

Important Concepts Used

  • List creation

  • List methods:

    • insert()

    • remove()

    • append()

    • pop()

    • clear()

  • len() function

  • Conditional statements

  • Menu-driven programming


Viva Voce Questions

  1. What is a list in Python?

  2. Difference between append() and insert()?

  3. What happens if we remove an element not present in list?

  4. What does pop() return?

  5. What is the difference between clear() and reassigning list?

  6. Can lists store different data types?

  7. What is indexing in Python lists?


Procedure to Execute the Program

  1. Open Python IDLE / VS Code / Google Colab

  2. Create a new Python file

  3. Type the program

  4. Save with .py extension

  5. Run the program

  6. 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



Assignment for Students
  • Modify program to run continuously until user selects Exit

  • Handle exceptions using try-except

  • Allow 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.



-:END:-

Comments

Popular posts from this blog