Skip to content

Day 3 : Array + String

🟢 Day 3 — Working With Many Values (Lists & Strings)

1️⃣ Why Do We Need This?

Until now, we worked like this:
a = 10
b = 20
c = 30

Real-life problem

What if:
You have marks of 50 students
You have prices of 100 products
You have names of 1000 users
Creating separate variables is:
Impossible to manage
Error-prone
Not scalable
💡 When you have many similar values, you use a collection.

2️⃣ Introducing Lists — One Variable, Many Values

Real-world analogy

Think of a train:
One train (list)
Many compartments (elements)
Each compartment has a number (index)

Creating a List

numbers = [10, 20, 30, 40]
Here:
numbers → list
10, 20, 30, 40 → elements

3️⃣ Indexing — How to Access Elements

print(numbers[0]) # 10
print(numbers[1]) # 20

Important Rule (VERY IMPORTANT)

Python starts counting from 0, not 1.
Index: 0 1 2 3
Value: 10 20 30 40

Why Index Starts From 0?

You don’t need to memorize why, just accept:
Index = how far from the start
First element is 0 steps away.

4️⃣ What Happens If Index Does Not Exist?

print(numbers[10])
❌ Program crashes.
Why?
Python does not guess
It only accesses what exists
🧠 The computer never assumes. It either finds something or fails.

5️⃣ Looping Over a List (MOST USED PATTERN)

Instead of accessing elements one by one:
print(numbers[0])
print(numbers[1])
Use a loop:
for num in numbers:
print(num)

How Computer Thinks

Take first element → num
Print it
Take next element
Stop when list ends

6️⃣ Lists + Logic = Real Programs

Example: Find Largest Number

numbers = [10, 45, 23, 89, 5]

largest = numbers[0]

for num in numbers:
if num > largest:
largest = num

print(largest)

How to Think (THIS IS IMPORTANT)

Assume first number is largest
Compare every number
Update when you find bigger
🧠 This thinking pattern is used everywhere in programming.

7️⃣ Strings Are Also Collections 🤯

You already know strings:
name = "python"
But a string is actually:
'p' 'y' 't' 'h' 'o' 'n'

Looping Over String

for ch in name:
print(ch)
💡 Strings behave like lists of characters.

8️⃣ Length of List or String

print(len(numbers))
print(len(name))
len() tells how many items
Works for list and string

9️⃣ Counting Pattern (Extremely Important)

Example: Count Even Numbers

count = 0

for num in numbers:
if num % 2 == 0:
count += 1

print(count)

Thinking Pattern

Start counter at 0
Increase when condition matches
Print at end
🧠 Counting is one of the most used patterns in programming.

🔁 Key Thinking Patterns Learned Today

Whenever you see a collection, ask:
Do I need to check every element?
Do I need to remember something while looping?
Do I need to compare values?
Do I need to count or accumulate?

⚠️ Common Beginner Mistakes

Using wrong index
Modifying list incorrectly
Forgetting loop
Confusing index and value
Assuming list has unlimited size

🧪 Practice Questions (Very Important)

Do not rush. Think → write steps → then code.

Q1

Create a list of 5 numbers and print all elements.

Q2

Find the smallest number in a list.

Q3

Count how many even numbers are in a list.

Q4

Check if a given number exists in a list.
Example:
List: [1, 2, 3, 4]
Input: 3
Output: Found

Q5

Reverse a list without using built-in reverse.
 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.