Python - Lists

5.
What is the purpose of negative indexing in Python lists?

Negative indexing in Python lists provides a convenient way to access elements from the end of the list without the need to know its length. The last element is indexed as -1, the second-to-last as -2, and so on. This can be particularly useful when working with lists of unknown or variable length.

Here's an example program illustrating the purpose of negative indexing:

# Creating a list
my_list = [10, 20, 30, 40, 50]

# Accessing elements using positive indexing
third_element = my_list[2]
print(third_element)  # Output: 30

# Accessing the same element using negative indexing
third_element_negative = my_list[-3]
print(third_element_negative)  # Output: 30

The program initializes a list and demonstrates how to access the third element using both positive and negative indexing. Negative indexing simplifies the process of accessing elements from the end of the list, making code more readable and avoiding the need to calculate the length of the list.

Outputs:

30
30

6.
Discuss the use of slicing in Python lists.

Slicing in Python lists allows you to extract a portion (sublist) of a list by specifying a range of indices. The syntax for slicing is list[start:stop:step], where start is the starting index, stop is the stopping index (exclusive), and step is the step size between elements. If any of these values are omitted, they default to certain values (0 for start, len(list) for stop, and 1 for step).

Here's an example program illustrating the use of slicing in Python lists:

# Creating a list
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Slicing to get a sublist from index 2 to 5 (exclusive)
sublist = my_list[2:5]
print(sublist)  # Output: [2, 3, 4]

# Slicing to get every second element
every_second = my_list[::2]
print(every_second)  # Output: [0, 2, 4, 6, 8]

# Slicing to reverse the list
reversed_list = my_list[::-1]
print(reversed_list)  # Output: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

The program demonstrates slicing to obtain a sublist, extract every second element, and reverse the original list.

Outputs:

[2, 3, 4]
[0, 2, 4, 6, 8]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

7.
How do you add an element to the end of a list in Python?

In Python, you can add an element to the end of a list using the append() method. The append() method adds the specified element to the end of the list.

# Creating a list
my_list = [1, 2, 3]

# Adding an element to the end of the list
my_list.append(4)

# Displaying the updated list
print(my_list)  # Output: [1, 2, 3, 4]

The program initializes a list and uses the append() method to add the element 4 to the end of the list.

Output:

[1, 2, 3, 4]

8.
Explain the difference between the append() and extend() methods in Python lists.

In Python, both append() and extend() are methods used to add elements to a list, but they differ in their functionality.

append() method:

# Creating a list
my_list = [1, 2, 3]

# Adding a single element to the end of the list
my_list.append(4)
print(my_list)  # Output: [1, 2, 3, 4]

# If you append a list, it becomes a nested list
my_list.append([5, 6])
print(my_list)  # Output: [1, 2, 3, 4, [5, 6]]

extend() method:

# Creating a list
my_list = [1, 2, 3]

# Extending the list with elements from another iterable
my_list.extend([4, 5, 6])
print(my_list)  # Output: [1, 2, 3, 4, 5, 6]

# Extending with elements from any iterable
my_list.extend((7, 8, 9))
print(my_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

The key difference is that append() adds a single element, possibly a list as a whole, to the end of the list, while extend() adds individual elements from an iterable to the end of the list, effectively extending the list.

Outputs:

[1, 2, 3, 4]
[1, 2, 3, 4, [5, 6]]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9]