Skip to content

Python! Storing Multiple Values | Lectures 9

Lecture 9 Lists Multiple Values

Lets Start Python Programming For Beginners to advanced. Lists – Storing Multiple Values, Lectures 9

🐍 Lecture 9: Lists – Storing Multiple Values

🎯 Goal

By the end of this lecture, you will:

  • Understand what a list is and why it’s useful.
  • Be able to create, modify, and access items in a list.
  • Use common list methods like append(), remove(), and index().
  • Loop through a list using a for loop.

🧠 What Is a List?

A list is like a container that can hold multiple values at once.
You can think of it as a shopping list or a backpack β€” it holds many things in order.

πŸ›’ Example:

fruits = ["apple", "banana", "cherry"]

This list contains 3 items , and they stay in the order you put them in.

πŸ“¦ How to Create a List

Use square brackets [ ] and separate items with commas.

Syntax:
my_list = [item1, item2, item3]

Examples:

numbers = [1, 2, 3, 4, 5]
names = ["Alice", "Bob", "Charlie"]
mixed = [1, "apple", True, 3.14]

πŸ§’ Kids Corner: A list is like your toy box β€” it can hold many different toys all together!

Lecture 9 Lists Multiple Values
Lecture 9 Lists Multiple Values

πŸ”’ Accessing Items in a List

Each item has an index (a number that tells its position).
Indexing starts at 0 , not 1.

fruits = ["apple", "banana", "cherry"]

print(fruits[0])  # Output: apple
print(fruits[1])  # Output: banana
print(fruits[2])  # Output: cherry

You can also use negative indexes to go from the end:

print(fruits[-1])  # Output: cherry
print(fruits[-2])  # Output: banana

✏️ Changing List Items

You can update any item by referring to its index:

fruits = ["apple", "banana", "cherry"]
fruits[1] = "blueberry"
print(fruits)  # Output: ['apple', 'blueberry', 'cherry']

βž• Adding Items to a List

Use .append() to add an item to the end of the list:

fruits = ["apple", "banana"]
fruits.append("orange")
print(fruits)  # Output: ['apple', 'banana', 'orange']

Or use .insert() to add at a specific position:

fruits.insert(1, "grape")
print(fruits)  # Output: ['apple', 'grape', 'banana', 'orange']

βž– Removing Items from a List

Use .remove() to remove a specific value:

fruits.remove("banana")
print(fruits)  # Now banana is gone

Or use del to remove by index:

del fruits[0]
print(fruits)  # First item is gone

Use .pop() to remove and get the last item:

last_fruit = fruits.pop()
print("Removed:", last_fruit)

πŸ” Looping Through a List

Use a for loop to do something for each item:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

πŸ‘‰ Output:

apple
banana
cherry

This is super useful when working with large lists!

πŸ§ͺ Try It Yourself!

Write code that:

  1. Creates a list of your favorite animals.
  2. Prints out each animal using a for loop.
  3. Adds a new animal to the list.
  4. Removes one animal.
  5. Prints the updated list.

πŸ’‘ Sample Code:

animals = ["dog", "cat", "elephant"]
print("My favorite animals:")

for animal in animals:
    print(animal)

animals.append("lion")
animals.remove("cat")

print("\nUpdated list:")
print(animals)

πŸ§’ Kids Corner

🧠 Imagine you have a robot friend who loves collecting stickers.
It can keep all its stickers in a special sticker book β€” that’s what a list is!

πŸ€– Robot says:

I have 5 stickers: cat, dog, star, car, and rainbow!

πŸš€ Challenge (Optional)

Make a grocery list app where the user can:

  1. Add an item.
  2. Remove an item.
  3. View the whole list.

Example:

grocery = []

while True:
    action = input("What do you want to do? (add, remove, view, quit): ")

    if action == "add":
        item = input("Enter item to add: ")
        grocery.append(item)
    elif action == "remove":
        item = input("Enter item to remove: ")
        if item in grocery:
            grocery.remove(item)
        else:
            print("Item not found!")
    elif action == "view":
        print("Your list:", grocery)
    elif action == "quit":
        print("Goodbye!")
        break
    else:
        print("Invalid choice!")

πŸ“Œ Summary

  • A list stores multiple values in order.
  • Use indexes to access or change items.
  • Use .append(), .remove(), and del to modify lists.
  • Loop through a list using for.
  • Lists help organize data and make programs more powerful.
Stay Updated

If you found this information useful, don’t forget to bookmark this page and Share and leave your feedback in the comment section below.

Python Programming For Beginners To Advanced

Python Programming For Beginners! Lecture 1

Python Programming! Variables and Data Types | Lectures 2

Python Programming! Input And Output | Lectures 3

Python Programming! Operators in Python Lectures 4

Python Programming! Conditional Statements Lectures 5

Python! Loops For and While | Lectures 6

Python! Building A Mini Game | Lectures 7

Python! Functions | Lectures 8

Python! Storing Multiple Values | Lectures 9Β 

Najeeb Alam

Najeeb Alam

Technical writer specializes in developer, Blogging and Online Journalism. I have been working in this field for the last 20 years.

Leave a Reply

Your email address will not be published. Required fields are marked *