LEARN Python - 4.1 - Lists Python’s Shopping Cart of Data - zerotopyhero.com

Learn Python – 4.1: Lists – Python’s Shopping Cart of Data

So far, Python has been dealing with one thing at a time.

One name.
One number.
One answer.

That’s fine for small programs.
But real programs need to remember lots of things.

That’s where lists come in.

A list is Python saying:

“I’ll keep all of these together for you.”

What Is a List?

A list is a collection of values stored under one name.

Instead of this:

				
					item1 = "apple"
item2 = "banana"
item3 = "orange"

				
			

You write this:

				
					shopping_list = ["apple", "banana", "orange"]

				
			

One variable.
Three items.
Much cleaner.

Creating Your First List

Lists are written using square brackets [].

				
					numbers = [1, 2, 3, 4, 5]
names = ["Alex", "Sam", "Jamie"]
				
			

A list can hold:

  • Numbers

  • Strings

  • Or a mix of both

				
					mixed = ["Python", 3, True]

				
			

Python doesn’t mind.

Lists Are Ordered

Order matters in lists.

				
					colors = ["red", "green", "blue"]

				
			

Python remembers:

  • "red" is first

  • "green" is second

  • "blue" is third

That order will matter when we start accessing items.

Lists Can Change (This Is Important)

Lists are mutable, which is a fancy way of saying:

“You can change them.”

You can:

  • Add items

  • Remove items

  • Replace items

That flexibility is what makes lists so useful.

Adding Items to a List

To add something to the end of a list, use .append().

				
					
animals = ["cat", "dog"]
animals.append("hamster")

print(animals)

				
			

Output:

				
					['cat', 'dog', 'hamster']

				
			

Python just tacked it onto the end.

Removing Items from a List

To remove an item by value, use .remove().

				
					animals.remove("dog")
print(animals)

				
			

Now "dog" is gone.

Important note:
Python removes the first matching item it finds.

Lists and Loops: Best Friends

Lists really shine when combined with loops.

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

for fruit in fruits:
    print(fruit)

				
			

Python goes through the list one item at a time.

This is one of the most common patterns in Python.

A Friendly Example

				
					tasks = ["wake up", "eat", "code", "sleep"]

for task in tasks:
    print(f"Don't forget to {task}.")

				
			

Simple.
Readable.
Powerful.

Checking How Many Items Are in a List

Use len() to count items.

				
					numbers = [10, 20, 30, 40]
print(len(numbers))

				
			

Output:

				
					4

				
			

Python counts for you.

Common Beginner Mistakes (Totally Normal)

Mistake 1: Forgetting brackets

				
					numbers = 1, 2, 3   # Not a list

				
			

Always use [].

Mistake 2: Expecting lists to stay fixed

Lists are meant to change.
If you want something fixed, that’s coming next.

What You’ve Learned

You now know:

  • What a list is

  • How to create one

  • How to add items

  • How to remove items

  • How to loop through a list

  • Why lists are so useful

This is a big step toward real-world programs.

Mini Quiz

Try these:

  1. What symbol do lists use?

  2. Can a list hold different types of values?

  3. What does .append() do?

  4. What does len() return?

  5. What will this print?

				
					items = ["a", "b"]
items.append("c")
print(items)

				
			

Coming Up Next

Now that Python can store many things,
we need to learn how to find specific ones.

ZeroToPyHero