Working with Lists & Indexing

Lists are fundamental data structures in Python that store ordered collections of items. Mastering list indexing is crucial for effective data manipulation.

What are Lists?

colors = ["red", "green", "blue"]
print(len(colors)) # → 3
3
Lists are:
  • Ordered collections of items
  • Mutable (can be modified)
  • Indexable with [position]

Accessing Elements

temperatures = [22.5, 24.0, 19.8, 21.3]
print(temperatures[0]) # → 22.5
print(temperatures[2]) # → 19.8
22.5
19.8

Key points:

  • Indexing starts at 0
  • Access non-existent indexes → IndexError
  • Works with mixed data types

Reverse Indexing

inventory = ["apples", "oranges", "bananas"]
print(inventory[-1]) # → bananas
print(inventory[-2]) # → oranges
bananas
oranges

Negative indexes count backward:

  • -1 → Last element
  • -2 → Second last

Practical Use: Student Grades

grades = [88, 92, 78, 95, 84]
highest = grades[-1] # Latest exam
lowest = min(grades)
print(f"Latest: {highest}, Lowest: {lowest}")
Latest: 84, Lowest: 78

Your Task: List Element Extraction

Given nums = [10, 20, 30, 40]:

  1. Store first element using positive index
  2. Store last element using negative index
  3. Ensure output shows: First: 10, Last: 40

Remember: Lists maintain insertion order - first added = first position.

Section 1/5What are Lists?

What are Lists?

Explore this concept

Working with Lists & Indexing

Lists are fundamental data structures in Python that store ordered collections of items. Mastering list indexing is crucial for effective data manipulation.

What are Lists?

colors = ["red", "green", "blue"]
print(len(colors)) # → 3
3
Lists are:
  • Ordered collections of items
  • Mutable (can be modified)
  • Indexable with [position]

Output:

Click "Check" to run your code.