Complete the code to get the first element of the list.
my_list = [10, 20, 30, 40] first_element = my_list[[1]] print(first_element)
List indexing starts at 0, so the first element is at index 0.
Complete the code to get the last element of the list using negative indexing.
numbers = [5, 10, 15, 20, 25] last_num = numbers[[1]] print(last_num)
Negative indexing starts from the end. -1 is the last element.
Fix the error in the code to slice the list and get elements from index 1 to 3 (inclusive of 1, exclusive of 4).
letters = ['a', 'b', 'c', 'd', 'e'] sublist = letters[[1]] print(sublist)
List slicing uses colon ':' to specify start and end indexes.
Fill both blanks to slice the list and get every second element from index 0 to 5.
data = [2, 4, 6, 8, 10, 12, 14] slice_result = data[[1]:[2]:2] print(slice_result)
The slice data[0:5:2] gets elements at indexes 0, 2, and 4.
Fill both blanks to create a dictionary with words as keys and their lengths as values, but only for words longer than 3 letters.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {: [1] for [2] in words if len(word) > 3}
The dictionary comprehension syntax is {key: value for item in iterable if condition}.