1. What is a Python list?

   – A list is an ordered and mutable collection of items in Python.

2. How is a list created in Python?

   – Lists are created using square brackets `[]` and elements separated by commas.

3. Explain the term “mutable” in the context of lists.

   – Mutable means that the contents of a list can be changed after its creation.

4. How can you access an element at a specific index in a list?

   – By using square bracket notation, e.g., `my_list[index]`.

5. What will `len(my_list)` return?

   – The number of elements in `my_list`.

6. How do you add an item to the end of a list?

   – Using the `append()` method, e.g., `my_list.append(item)`.

7. Which method is used to insert an element at a specific index in a list?

   – `insert(index, item)`.

[amazon box=”B0C8P8WBSJ”]

8. How can you remove an item by its value from a list?

   – Using the `remove(item)` method.

9. Explain the difference between `pop()` and `remove()` methods.

   – `pop()` removes an item by index and returns it, while `remove()` removes an item by value.

10. How do you check if an item exists in a list?

    – By using the `in` keyword, e.g., `item in my_list`.

11. What method is used to count the occurrences of a specific item in a list?

    – Answer: The `count()` method, e.g., `count = my_list.count(item)`

12. Explain the difference between the `extend()` and `append()` methods.

    – Answer: The `extend()` method adds the elements of another iterable to the end of the list, while `append()` adds a single item to the end.

[amazon box=”B0B4N6JVMW”]

13. How can you reverse the order of elements in a list?

    – Answer: Using the `reverse()` method, e.g., `my_list.reverse()`

14. Create a list containing the characters of the string “hello”.

    – Answer: `char_list = list(“hello”)`

15. How do you check if two lists are equal?

    – Answer: By using the equality operator `==`, e.g., `are_equal = list1 == list2`

16. Create a list of odd numbers from 1 to 10 using list comprehension.

    – Answer: `odd_numbers = [x for x in range(1, 11) if x % 2 != 0]`

17. Explain the purpose of the `index()` method with optional start and end arguments.

    – Answer: The `index()` method searches for an element within a specified range and returns its index if found, otherwise raises an error.

18. What method is used to remove an element at a specific index and return its value?

    – Answer: The `pop()` method, e.g., `removed_item = my_list.pop(index)`

19. How can you check if a list is empty?

    – Answer: By using the `not` keyword with the list, e.g., `is_empty = not my_list`

20. Describe a scenario where a nested list could be useful.

    – Answer: A nested list could be used to represent a matrix or a grid of values, where each inner list represents a row or column.

error: Content is protected !!