2. Introduction to Lists
• • A list is a collection of items in a particular
order.
• • Lists are mutable (can be changed).
• • Lists can contain different data types.
3. Creating Lists
• • Lists are defined using square brackets []
• • Example: my_list = [1, 2, 3, 'Python', True]
4. Accessing Elements
• • Use indexing to access elements (zero-based
indexing)
• • Example: my_list[0] gives first element
• • Negative indexing: my_list[-1] gives last
element
5. Modifying Lists
• • Lists are mutable, meaning elements can be
changed.
• • Example: my_list[1] = 'New Value'
6. Common List Methods
• • append(): Adds an item to the end
• • insert(): Inserts an item at a specified index
• • remove(): Removes a specific item
• • pop(): Removes and returns the last or
specified item
• • sort(): Sorts the list
7. Looping Through Lists
• • Use a for loop to iterate through a list
• • Example:
• for item in my_list:
• print(item)
8. List Comprehensions
• • A concise way to create lists
• • Example: squares = [x**2 for x in range(10)]
9. List Operations
• • Concatenation: list1 + list2
• • Repetition: list * 3
• • Membership: 'item' in list
10. Examples & Exercises
• • Create a list of your favorite fruits.
• • Access the second item in the list.
• • Append a new item and print the list.
• • Remove an item from the list.