You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Lists and tuples are Python's primary sequence types. A list is a mutable, ordered collection of items. A tuple is similar but immutable — once created, it cannot be changed. Understanding when to use each is a key part of writing effective Python.
# A list of integers
numbers = [1, 2, 3, 4, 5]
# A list of strings
fruits = ["apple", "banana", "cherry"]
# A mixed list
mixed = [42, "hello", 3.14, True, None]
# An empty list
empty = []
# List from range
digits = list(range(10))
print(digits) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
fruits = ["apple", "banana", "cherry", "date", "elderberry"]
print(fruits[0]) # apple — first element
print(fruits[-1]) # elderberry — last element
print(fruits[1:3]) # ['banana', 'cherry'] — slice
print(fruits[:2]) # ['apple', 'banana'] — first two
print(fruits[3:]) # ['date', 'elderberry'] — from index 3 onwards
print(fruits[::2]) # ['apple', 'cherry', 'elderberry'] — every other
Lists are mutable — you can change them after creation:
fruits = ["apple", "banana", "cherry"]
# Change an element
fruits[1] = "blueberry"
print(fruits) # ['apple', 'blueberry', 'cherry']
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.