1. Explain pop(), remove(), len(), clear(), insert() functions related to list with example.
(10 Marks)
Python provides several built-in functions for lists that make it easy to add, remove, and manipulate
elements. Below is the detailed explanation with examples:
1) pop()
Removes and returns an element from a list at a specific index.
If no index is given, it removes and returns the last element.
Raises IndexError if the list is empty.
Example:
numbers = [10, 20, 30, 40]
numbers.pop() # Removes 40 (last element)
numbers.pop(1) # Removes 20 (index 1)
print(numbers) # Output: [10, 30]
2) remove()
Removes the first occurrence of a specific value from the list.
Raises ValueError if the element is not found.
Example:
fruits = ["apple", "banana", "apple"]
fruits.remove("apple") # Removes the first "apple"
print(fruits) # Output: ['banana', 'apple']
3) len()
Returns the total number of elements in a list.
Example:
names = ["Ram", "Sita", "Lakshman"]
print(len(names)) # Output: 3
4) clear()
Removes all elements from the list (makes it empty).
Example:
items = [1, 2, 3]
items.clear()
print(items) # Output: []
5) insert()
Inserts an element at a specific position without replacing the existing elements.
The elements from that position shift to the right.
Example:
letters = ["A", "C", "D"]
letters.insert(1, "B")
print(letters) # Output: ['A', 'B', 'C', 'D']
These functions make Python lists flexible, dynamic, and easy to manage.
2. Describe structure of Python program. (10 Marks)
A Python program follows a simple but organized structure for readability and modularity. Below is
the typical structure:
1) Shebang Line (Optional)
Used in Linux/Unix systems to specify the Python interpreter path.
Not mandatory for Windows.
Example:
#!/usr/bin/python3
2) Module Imports
Used to import built-in or external libraries for additional functionality.
Example:
import math
import sys
---
3) Global Variables and Constants
Define variables and constants that will be reused across the program.
4) Function Definitions
Functions divide the program into reusable blocks for modularity.
Example:
def greet(name):
print(f"Hello, {name}")
5) Main Program Block
The part of the program that runs only when the file is executed directly (not imported).
Example:
if _name_ == "_main_":
greet("Student")
6) Comments and Documentation
Single-line comments use #.
Multi-line docstrings use triple quotes """.
Typical Flow of a Python Program:
[Shebang Line] → [Imports] → [Global Variables/Constants] → [Functions] → [Main Execution Block]
→ [Output]
This structure ensures clarity, maintainability, and reusability of the Python code.
---