DEV Community

Sameer Qaiser
Sameer Qaiser

Posted on

Python Lists: The Beginner's Guide

๐Ÿ“Œ Quick Info

  • Topic: Lists in Python
  • Target Audience: Beginners who know functions and loops
  • Goal: Understand how to store and work with multiple items

1. Introduction

"I used to store one value in a variable. But what if I have 10 names? Or 100 items? Thatโ€™s where lists come inโ€”and now I use them everywhere."

2. The Problem (Without Lists)

name1 = "Ali"
name2 = "Sara"
name3 = "Ahmed"
name4 = "John"
name5 = "Emma"

print(name1)
print(name2)
print(name3)
๐Ÿ“บ Info Siaran: sameerqaiser/python lists the beginners guide fl - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sameerqaiser/python lists the beginners guide fl - Jadwal Siaran Langsung

Problem: Imagine doing this with 50 names. Messy and unmanageable

3. The Solution (With Lists)

names = ["Ali", "Sara", "Ahmed", "John", "Emma"]

for name in names:
    print(name)
๐Ÿ“บ Info Siaran: sameerqaiser/python lists the beginners guide fl - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sameerqaiser/python lists the beginners guide fl - Jadwal Siaran Langsung

Output:

Ali
Sara
Ahmed
John
Emma
๐Ÿ“บ Info Siaran: sameerqaiser/python lists the beginners guide fl - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sameerqaiser/python lists the beginners guide fl - Jadwal Siaran Langsung

4. How It Works (Line by Line)

Line 1: names= [โ€ Aliโ€,โ€Saraโ€,โ€ฆ ]- creates a list with 5 items.

Line 3: for name in names: - Loops through each item.

Line 4: print(name) - prints each one.

Thatโ€™s it. One List, one loop- cleaner than 5 variables.

5. Common List Operators

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

fruits.append("mango")

fruits.remove("banana")

print(fruits[0]) 

print(len(fruits))
๐Ÿ“บ Info Siaran: sameerqaiser/python lists the beginners guide fl - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sameerqaiser/python lists the beginners guide fl - Jadwal Siaran Langsung

6. Real Examples (My Practise)

scores = [85, 92, 78, 95, 88]

total = 0
for score in scores:
    total += score

average = total / len(scores)
print(f"Average: {average}")
๐Ÿ“บ Info Siaran: sameerqaiser/python lists the beginners guide fl - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sameerqaiser/python lists the beginners guide fl - Jadwal Siaran Langsung

Output:

Average: 87.6
๐Ÿ“บ Info Siaran: sameerqaiser/python lists the beginners guide fl - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sameerqaiser/python lists the beginners guide fl - Jadwal Siaran Langsung

7. What I Learned

  • Lists store multiple items in one variable.
  • Indexes start at 0, not 1.
  • append() adds an item.
  • remove() deletes an item.
  • len() tells you how many items.
  • Loops+ Lists are a perfect match.

8. Conclusion

Lists seemed simple at first. But once I combined them with loops and functions, my code got way more powerful. Now I canโ€™t imagine.

Top comments (0)