Python: Exercises


2. Accessing list elements. In Python, you can access list elements by referencing their index value. Consider the list a_list = [10, 20, 30, 40, 50]. To access the number 20 in the list, you would use the syntax a_list[1]. Note that indexation starts at 0 in Python.

The list object, a_list has been defined for you. Do the following things to that list:

1. Print the 3rd element.

2. Change the 5th element to the list [0, 1].

3. Change the 2nd element to the empty list[].


a_list = [10, 20, 30, 40, 50] # Print the 3rd element. print(___) # Set 5th element to list [0, 1]. a_list[4] = ___ # Set 2nd element to empty list: []. ___ # Print the 3rd element. print(a_list[2]) # Set 5th element to list [0, 1]. a_list[4] = [0, 1] # Set 2nd element to empty list: []. a_list[1] = [] test_object("a_list") success_msg("Excellent work.")
Remember that indexation starts at 0 in Python.

Previous Exercise Next Exercise