Q1. What is a list in Python?
A list is a mutable, ordered collection of items, which can be of different types. Lists are created with square brackets []. Example:
my_list = [1, "hello", 3.14] They support indexing, slicing, and various methods like append, extend, insert, remove, pop, etc.Q2. How do you add elements to a list?
Use append() to add at the end, insert(index, value) at a specific position, or extend() to add multiple items from another iterable. Concatenation (+) creates a new list. Example:
lst.append(10)
lst.insert(0, 5)
lst.extend([6,7])Q3. How do you remove elements from a list?
Remove by value: remove(value) removes the first occurrence. Remove by index: pop(index) (default last) returns the removed element. Delete by slice: del lst[start:end]. Clear all: clear(). Example:
lst.remove(10)
last = lst.pop()
del lst[1:3]Q4. What is list slicing?
Slicing extracts a portion of a list using the syntax [start:stop:step]. It returns a new list. Example:
sub = lst[2:5] # elements at index 2,3,4
rev = lst[::-1] # reverse list Negative indices count from the end.Q5. How do you sort a list?
Use list.sort() for in-place sorting, or sorted(list) for a new sorted list. Example:
lst.sort() # ascending
lst.sort(reverse=True) # descending
new_list = sorted(lst) # new list You can provide a key function for custom sorting.