If you want to store multiple values in the same variable, you can use a List.
For example, to store the names of multiple items, you can use a List.
The List is represented by square brackets [ ].
food_list = ["apples", "bananas", "milk", "bread"]
Here, we have created a list named food_list. It contains 4 items.
🎉 Features of Lists
- Ordered → The items stay in the order you list them. If you write
"milk"before"bread", that order is remembered. - Changeable → You can update your list. If you decide you want
"orange juice"instead of"milk", you can change it. - Mixed Items → You can mix different things in a list, like numbers and words.
🎉 Using Lists in Everyday Life
Just like making a to-do list, you can create and use lists in Python.
Example: To-do List
to_do_list = ["finish homework", "buy groceries", "go for a walk", "read a book"]
# Access List Items
first_task = to_do_list[0] # "finish homework"
# Change Item
to_do_list[3] = "call a friend"
# Add Item
to_do_list.append("water the plants")
# Delete Item
to_do_list.remove("water the plants")
# Count Items
tasks_left = len(to_do_list)
📌 Basic List Methods
| Method | Description |
|---|---|
append(x) | Add item at the end |
insert(i, x) | Insert item at index i |
remove(x) | Remove first occurrence of x |
pop(i) | Remove item at index i (default last) |
sort() | Sort list in ascending order |
reverse() | Reverse the list |
len(list) | Count elements in list |
startswith() / endswith() | Check whether a string begins or ends with certain characters |
count(x) | Count how many times x appears |
🎉 Examples
Example 1: Planning a Party
# Step 1: Create a list of tasks
party_tasks = ["buy decorations", "send invitations", "order food"]
# Step 2: Add a new task
party_tasks.append("set up music")
print("Updated Party Tasks:", party_tasks)
# Step 3: Check first task
first_task = party_tasks[0]
print("First task to do:", first_task)
# Step 4: Remove a completed task
party_tasks.remove("send invitations")
print("Remaining Tasks:", party_tasks)
# Step 5: Add another task
party_tasks.append("clean the house")
print("Final Task List:", party_tasks)
# Step 6: Count tasks left
tasks_left = len(party_tasks)
print("Number of tasks left to do:", tasks_left)
Example 2: Reverse a List
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print(my_list) # Output: [5, 4, 3, 2, 1]
Example 3: Display Last Item of a List
mylist = [1, 2, 3, 4, 5]
last_item = mylist[-1] # -1 gives the last item
print(last_item) # Output: 5
Example 4: Monthly Expense Tracker
# Step 1: Create a list of expenses
monthly_expenses = [150, 200, 50, 75, 100]
# Step 2: Add new expense
monthly_expenses.append(60)
print("Updated Expenses:", monthly_expenses)
# Step 3: Calculate total
total_expense = sum(monthly_expenses)
print("Total Expense:", total_expense)
# Step 4: Remove unnecessary expense
monthly_expenses.remove(75)
print("Expenses after removing utilities:", monthly_expenses)
# Step 5: New total
total_expense = sum(monthly_expenses)
print("New Total Expense:", total_expense)
# Step 6: Find highest expense
highest_expense = max(monthly_expenses)
print("Highest Expense:", highest_expense)
# Step 7: Find lowest expense
lowest_expense = min(monthly_expenses)
print("Lowest Expense:", lowest_expense)
# Step 8: Calculate average
average_expense = sum(monthly_expenses) / len(monthly_expenses)
print("Average Expense:", average_expense)
💡 Challenge 15
- Create an empty list of type strings called
days. Use theappend()method to add the names of 7 days and print all days. - Add your 7 friends’ names to a list. Use
startswith()to find the names that start with the alphabet A.
📌 This Notes is part of Python learning series on my YouTube channel Coding With Subin.
🎥 Watch the full video here 👉 Coding with subin
For more detailed notes, visit: subinthapa.com.np