List in python
List in python
Introduction to Lists
• A list is a collection of items that are ordered and changeable. Lists are created using square
brackets [].
• Example:
• my_list = [1, 2, 3, 4, 5]
Key Features of Lists
1. Access Elements:
Use index numbers, starting from 0.
2. my_list = [10, 20, 30]
3. print(my_list[0]) # Output: 10
4. Modify Elements:
5. my_list[1] = 25
6. print(my_list) # Output: [10, 25, 30]
7. Add Elements:
o Append an element:
o my_list.append(40)
o print(my_list) # Output: [10, 25, 30, 40]
o Insert an element:
o my_list.insert(2, 35)
o print(my_list) # Output: [10, 25, 35, 30, 40]
8. Remove Elements:
o Remove a specific element:
o my_list.remove(25)
o print(my_list) # Output: [10, 35, 30, 40]
o Remove by index:
o my_list.pop(2)
o print(my_list) # Output: [10, 35, 40]
9. Loop through a List:
10. for item in my_list:
11. print(item)
20 Questions on Lists
Section A: Basic Questions
These questions cover basic, intermediate, and advanced list operations to give students a
comprehensive understanding of Python lists.