Chapter 7: Arrays
Arrays (or what most beginners in Python actually use: lists that behave like arrays).
Grab your chai again ☕ — we’re doing this Hyderabad-teacher style: slow, with lots of analogies (cricket team, biryani ingredients, IPL scorecard), many examples, and clear warnings about the Python twist.
1. What is an Array? (The napkin drawing version)
An array is a single named box that can hold many values at once — like a long shelf with numbered slots.
- Instead of 11 separate variables for 11 players’ scores
- You make one box called scores and put all 11 numbers inside it
Key superpowers:
- Store lots of similar things together
- Access any item quickly using its position number (called index)
- Loop through them easily (very important later)
Real Hyderabad analogy – IPL Scoreboard:
Imagine the big screen at Rajiv Gandhi Stadium showing today’s batting scores:
- Virat 85
- Rohit 42
- KL Rahul 19
- … up to 11 players
Without array → you’d need 11 variables: virat_score, rohit_score, rahul_score, … (pain!)
With array → one variable: batting_scores = [85, 42, 19, 67, 3, 12, 0, 28, 15, 4, 9]
Now the computer knows:
- Position 0 = Virat’s score
- Position 1 = Rohit’s score
- etc.
That’s an array — a collection of values under one name, ordered by position.
2. Important Python Truth (beginners get confused here)
Python does NOT have “arrays” the way C, Java, C++ do.
In most programming languages:
- Array = fixed size, same type only, very memory efficient
In Python:
- The built-in thing everyone uses = list → flexible, can grow/shrink, can hold mixed types
- There is a real array module (for same-type, efficient numeric data)
- Most beginners & even intermediate coders just say “array” when they mean list
So in 90% of beginner Python tutorials (including this one) → “array = list” for teaching purposes.
We’ll learn both, but start with lists (what you’ll use daily).
3. Creating & Using Lists (Python’s Everyday “Arrays”)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# Empty list (array with nothing yet) empty = [] # List with numbers marks = [85, 92, 78, 65, 88] # List with strings cricketers = ["Virat", "Rohit", "Bumrah", "Jadeja", "Pant"] # Mixed types (Python allows — other languages usually don't) mixed = [42, "Hyderabad", 5.7, True, "chai"] print(marks) print(cricketers[0]) # Virat ← index starts at 0 !!! print(marks[2]) # 78 print(len(marks)) # 5 ← how many items |
Index rule (super important):
- First item = index 0
- Second = 1
- Last = len(list)-1 or use negative: -1 = last, -2 = second last
|
0 1 2 3 4 5 6 7 |
print(cricketers[-1]) # Pant (last one) print(cricketers[-2]) # Jadeja |
4. Common Operations on Lists (what makes them powerful)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
scores = [45, 67, 23, 89, 12] # 1. Change value scores[2] = 50 # was 23, now 50 print(scores) # [45, 67, 50, 89, 12] # 2. Add at end scores.append(95) # add new score print(scores) # [45, 67, 50, 89, 12, 95] # 3. Add at specific position scores.insert(0, 100) # insert 100 at beginning print(scores) # 4. Remove scores.pop() # remove last scores.remove(67) # remove first 67 found # 5. Length print("Total players:", len(scores)) # 6. Loop through (very common!) for score in scores: print("Score:", score) # Better loop with index for i in range(len(scores)): print(f"Player {i+1}: {scores[i]}") |
5. Real Useful Hyderabad Example – Monthly Expense Tracker
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
expenses = [1200, 450, 2800, 150, 900, 3200] # biryani, auto, rent, chai, movies, shopping total = 0 for amount in expenses: total += amount print("Total expenses this month: ₹", total) # Find most expensive day max_expense = max(expenses) print("Most expensive single spend: ₹", max_expense) # Add new expense expenses.append(750) # late night biryani print("Updated list:", expenses) |
6. Python’s Real array Module (when you need strict array)
Only for same-type (usually numbers), more memory-efficient, used in advanced / performance code.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
import array # 'i' means signed integer numbers = array.array('i', [1, 2, 3, 4, 5]) print(numbers[0]) # 1 numbers.append(6) print(numbers) |
→ You won’t use this much until data science / large numeric data → stick to lists for now.
7. Lists vs Real Arrays (Quick Comparison Table)
| Feature | Python List (what we use 99%) | Real Array (C/Java/NumPy style) | Python array.array |
|---|---|---|---|
| Can hold mixed types? | Yes | No (same type only) | No |
| Size fixed? | No – grows/shrinks easily | Yes (usually) | Grows, but less flexible |
| Memory usage | More (overhead per item) | Very efficient | Efficient |
| Speed for math ops | Slower | Very fast | Faster than list |
| Beginner use | Almost always | Later (NumPy) | Rare at start |
8. Your 10-Minute Mini Challenge (try now!)
Open replit.com or any Python editor and write:
- A list called favorite_foods with 5 Hyderabad foods (“Biryani”, “Irani Chai”, “Pani Puri”, “Haleem”, “Osmania Biscuit”)
- Print the 3rd food
- Add one more food with .append()
- Print how many foods now
- Loop and print “I love [food]!” for each
Bonus:
- Make a list of daily temperatures this week
- Calculate average temperature (sum / len)
Paste your code here if you want feedback, corrections, or “how to make it better” 😄
Any doubt right now?
- “Why index starts at 0?”
- “What happens if I do list[10] when only 5 items?”
- “Show slicing like first 3 items”
- “Next topic please” (loops + lists together are magic!)
You’re doing fantastic — lists/arrays are where programming becomes really powerful 🚀
Keep going, champ!
