Skip to main content

Python Tutorial 4.1 - Lists in Python

Welcome to the “Lists in Python” tutorial!


Overview

Lists are ordered, mutable collections that can hold items of different types.

Creating Lists

fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4]
mixed = [1, "two", 3.0, True]

Common Operations

  • Indexing & Slicing:
    print(fruits[0])     # "apple"
    print(numbers[1:3])  # [2, 3]
    
  • Adding Items:
    fruits.append("date")
    
  • Removing Items:
    fruits.remove("banana")
    
  • Inserting Items:
    fruits.insert(1, "blueberry")
    
  • Length & Membership:
    len(fruits)          # 4
    "cherry" in fruits   # True
    

Key Takeaways

  • Lists are versatile and mutable.
  • Use indexing and slicing to access elements.
  • Utilize list methods to modify the list.

Happy coding!

Comments