Python Learning Session-07: Working with Lists
👋 Welcome Back!
If you’ve been following our Python journey, you already
know how to work with strings. Today, we’re stepping into one of the most
powerful tools in Python: Lists.
Think of a list like a shopping basket. You can throw in
apples, bananas, and even a bottle of juice—all in one basket. Similarly,
Python lists can hold multiple items, even of different types.
✅ What is a List?
A list is a collection of items stored in a single
variable. It’s like a container that keeps things organized.
Example:
Python
fruits = ["apple", "banana",
"cherry"]
print(fruits)
Show more lines
Output:
['apple', 'banana', 'cherry']
✅ Why Do We Need Lists?
Imagine you’re building a student management system.
Instead of creating 100 variables for 100 students, you can store all names in one
list. Simple, right?
✅ Creating Lists
Python
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", 3.14, True]
empty_list = []
Show more lines
✔ Pro Tip: Lists can hold
anything—numbers, strings, even other lists!
✅ Accessing Items
Lists use indexing:
Python
fruits = ["apple", "banana",
"cherry"]
print(fruits[0]) # apple
print(fruits[-1]) # cherry
Show more lines
✔ Why important? Indexing helps
you grab exactly what you need.
✅ Updating Lists
Lists are mutable, meaning you can change them:
Python
fruits[1] = "orange"
print(fruits) # ['apple', 'orange', 'cherry']
Show more lines
✔ Real-life example: Updating a
product name in your online store.
✅ Adding Items
- append()
→ Add at the end
Python
fruits.append("grape")
Show more lines
- insert()
→ Add at a specific position
Python
fruits.insert(1, "mango")
Show more lines
✔ Why useful? Dynamic data
handling without creating new lists.
✅ Removing Items
- remove()
→ Remove by value
Python
fruits.remove("apple")
Show more lines
- pop()
→ Remove by index
Python
fruits.pop(0)
Show more lines
✔ Use case: Removing sold-out
items from inventory.
✅ Looping Through Lists
Python
for fruit in fruits:
print(fruit)
Show more lines
✔ Practical example: Displaying
all items in a shopping cart.
✅ List Comprehension
A smart way to create lists:
Python
squares = [x**2 for x in range(5)]
print(squares) # [0, 1, 4, 9, 16]
Show more lines
✔ Why powerful? Less code, more
clarity.
✅ Summary
Lists are your best friend when you need to store and
manage multiple values. They’re flexible, easy to use, and form the backbone of
Python programming.
🔥 Challenge for You
Create a list of your favorite movies and print them one
by one using a loop. Then, add a new movie to the list and remove one you don’t
like anymore.