Understanding Lists in Python
What is a List?
A list in Python is a collection of items (elements) that can be of any type, such as numbers, strings, or even other lists. Lists are ordered, changeable, and allow duplicate elements.
Why Use Lists?
Lists are useful when you want to store and organize multiple pieces of data in one place. For example, you might use a list to store your favorite books, numbers, or even tasks.
Creating a List
A list is defined using square brackets [ ], and the items are separated by commas.
Example: Creating a List
# A list of fruits
fruits = ["apple", "banana", "cherry"]
# A list of numbers
numbers = [1, 2, 3, 4, 5]
# A mixed list
mixed = ["hello", 42, True]
Accessing Items in a List
You can access items in a list using their index. The index starts at 0 for the first item, 1 for the second, and so on.
Example: Accessing List Items
fruits = ["apple", "banana", "cherry"]
# Access the first item
print(fruits[0])
# Output: apple
# Access the second item
print(fruits[1])
# Output: banana
# Access the last item
print(fruits[-1])
# Output: cherry
Changing Items in a List
Lists are mutable, meaning you can change their elements.
Example: Changing Items
fruits = ["apple", "banana", "cherry"]
# Change the second item
fruits[1] = "orange"
print(fruits)
# Output: ['apple', 'orange', 'cherry']
Adding Items to a List
You can add new items to a list using:
append(): Adds an item to the end.
insert(): Adds an item at a specific position.
Example: Adding Items
fruits = ["apple", "banana"]
# Add to the end
fruits.append("cherry")
# Add at a specific position
fruits.insert(1, "orange")
print(fruits)
# Output: ['apple', 'orange', 'banana', 'cherry']
Removing Items from a List
You can remove items using:
remove(): Removes a specific item.
pop(): Removes an item at a specific index (or the last item if no index is given).
Example: Removing Items
fruits = ["apple", "banana", "cherry"]
# Remove by value
fruits.remove("banana")
# Remove by index
fruits.pop(0)
print(fruits)
# Output: ['cherry']
Practice Exercises
Exercise 1: Create and Print a List
Create a list of your favorite three animals and print each one using its index.
Exercise 2: Update a List
Create a list of three colors. Change the second color to a new one and print the updated list.
Exercise 3: Add and Remove
Create a list of three numbers. Add another number to the end and remove the first number. Print the final list.
Exercise 4: Access the Last Item
Create a list of five hobbies. Print the last hobby using negative indexing.
Exercise 5: Counting Items
Write a program that creates a list of fruits. Use the len() function to print the number of items in the list.
Challenge Exercise
Create a program that:
Starts with an empty list.
Uses a loop to ask the user to add 5 items to the list.
Prints the final list.
Asks the user for an item to remove and removes it.
Prints the updated list.
Key Points
Lists are defined with square brackets: [ ].
Indexing starts at 0.
Lists are mutable, meaning you can add, remove, or change items.
Use append() and insert() to add items.
Use remove() and pop() to remove items.
Congratulations!
You’ve learned the basics of creating and managing lists in Python. Lists are powerful and can make your programs more flexible and efficient. Practice these exercises to become a list expert! 🎉