Lists: Shopping for Data
Lists in Python
Lists are like shopping lists for your code. They store multiple items in a specific order. Each item can be accessed by its position (index).
Creating Lists
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = ["hello", 42, True, 3.14]
Accessing Items
first_fruit = fruits[0] # "apple" (indices start at 0)
second_fruit = fruits[1] # "banana"
Modifying Lists
fruits.append("orange") # Add to end
fruits[0] = "grape" # Change first item
fruits.remove("banana") # Remove specific item
List Operations
len(list)- Get number of itemslist.append(item)- Add item to endlist.insert(index, item)- Insert at specific positionlist.remove(item)- Remove first occurrencelist.pop(index)- Remove and return item at index
Remember
- Lists are ordered (position matters)
- Lists can contain different data types
- Use square brackets
[]to create lists - Indices start at 0, not 1
Next, practice working with lists!
Lists: Shopping for Data
Lists in Python
Lists are like shopping lists for your code. They store multiple items in a specific order. Each item can be accessed by its position (index).
Creating Lists
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = ["hello", 42, True, 3.14]
Accessing Items
first_fruit = fruits[0] # "apple" (indices start at 0)
second_fruit = fruits[1] # "banana"
Modifying Lists
fruits.append("orange") # Add to end
fruits[0] = "grape" # Change first item
fruits.remove("banana") # Remove specific item
List Operations
len(list)- Get number of itemslist.append(item)- Add item to endlist.insert(index, item)- Insert at specific positionlist.remove(item)- Remove first occurrencelist.pop(index)- Remove and return item at index
Remember
- Lists are ordered (position matters)
- Lists can contain different data types
- Use square brackets
[]to create lists - Indices start at 0, not 1
Next, practice working with lists!
main.py
Loading...
Click "Run Code" when you are ready to test your solution.
