Skip to content

Advanced Python · Week 1

List Review and Algorithms

Step 1 of 5

Exercise 1: List Terminology Review

Welcome to Python 2! Let's start by reviewing some important list terminology.

Key List Terms

  • List: An ordered, mutable collection of elements.
  • Index: The position of an element in a list (starting from 0).
  • Slice: A portion of a list, specified by a range of indices.
  • Nested List: A list that contains other lists as elements.

Let's practice working with these concepts. Your task is to create a list, access elements, and perform slicing.

# Create a list of fruits fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'] # Print the third fruit (remember, indexing starts at 0) print(fruits[2]) # Print a slice of the list (from index 1 to 3, not including 3) print(fruits[1:3]) # Print the last fruit using negative indexing print(fruits[-1])

Now it's your turn! Modify the code above to:

  1. Add 'fig' to the end of the list
  2. Print the first and last two fruits using slicing
  3. Print the list in reverse order

Type your code in the editor below, then hit the Run button to see if it works!

Starter code
# Modify the list and print as instructed
fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']

# Your code here

Write your code here