Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: List Indexing and In Keyword

check

Searching and retrieving values from Python lists.


In Python, lists store multiple items in a single variable. List items are ordered, changeable, and allow duplicate values. List items are indexed, with the first item having an index of 0.

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(days[0])
print(days[3])

> "Monday"
> "Thursday"

Python also allows negative indexing, where -1 refers to the last item, -2 refers to the second last item, and so on.

print(days[-1])
print(days[-2])

> "Friday"
> "Thursday"

The 'in' keyword can be used to check if an item exists in the list.

if "Wednesday" in days:
  print("Yes, 'Wednesday' is in the list days.")

> "Yes, 'Wednesday' is in the list days."

Practice: Retrieve Item at a Specific Index

  1. Create a list named fruits and assign it the values "Apple", "Banana", "Cherry", "Dragon Fruit", "Elderberry".

  2. Print the item at index 2 from the list.

Run your code and confirm that the output is "Cherry".

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> "Cherry"
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
fruits = ["Apple", "Banana", "Cherry", "Dragon Fruit", "Elderberry"]
print(fruits[2])

Practice: Check if an Item Exists

  1. Create a list named fruits and assign it the values "Apple", "Banana", "Cherry", "Dragon Fruit", "Elderberry" if you haven't already.

  2. Use the 'in' keyword to check if "Banana" is in the fruits list and print the result.

  3. Use the 'in' keyword to check if "Grapes" is in the fruits list and print the result.

Run your code and confirm that the output is True for "Banana" and False for "Grapes".

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> True
> False
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
fruits = ["Apple", "Banana", "Cherry", "Dragon Fruit", "Elderberry"]
print("Banana" in fruits)
print("Grapes" in fruits)

Good job! Your understanding of lists, their indexing, and the 'in' keyword in Python is improving. This knowledge is very useful when dealing with sequential data in Python.