Python Tutorial: Understanding Lists in Python
📚 Python Tutorial: Understanding Lists in Python
Introduction
Lists are one of Python’s most powerful data structures. They allow you to store multiple values in a single variable, making them essential for organizing and manipulating data.
1. Creating a List
fruits = ["apple", "banana", "cherry"]
print(fruits)
Try it yourself: Create a list of your favorite colors.
2. Accessing Elements
print(fruits[0]) # apple
print(fruits[2]) # cherry
Exercise: Print the second item in your list.
3. Modifying Lists
fruits[1] = "blueberry"
print(fruits)
Challenge: Replace one item in your list with a new value.
4. Adding and Removing Items
fruits.append("orange")
fruits.remove("apple")
print(fruits)
Try it: Add two new items and remove one.
5. Looping Through Lists
for fruit in fruits:
print(fruit)
Exercise: Loop through your list and print each item with a custom message.
6. Real‑World Example: Shopping Cart
cart = ["milk", "bread", "eggs"]
cart.append("butter")
for item in cart:
print("You bought:", item)
Challenge: Add a feature to remove an item from the cart.
Want to dive deeper? Check out these related tutorials:
➡
Python Interactive Tutorial: From Basics to Mastery
➡
Python Tutorial: Mastering Fuzzy Buzzy (FizzBuzz)
💻 Programming & Science Books — Only $1 Each
Learn coding, algorithms, and data science with my easy-to-follow tutorials.
Buy on Gumroad Buy on Amazon KDP

Comments
Post a Comment