Loops in Python
Row 10,000ක් අතින් process කරන්නෙ නෑ. Loop එකක් කියන්නෙ, පොඩි step එකක් දහස් වතාවක් වෙහෙසෙන්නෙ නැතුව computer එකෙන් කරන විදිහ.
You'll never manually process 10,000 rows one by one. Loops are how a computer does the same small step thousands of times without getting tired.
Lesson 7 of 64 · Part 1 · Python & Data Basics · Python Fundamentals · bilingual notes (English + සිංහල)
Why this matters. You'll never manually process 10,000 rows one by one. Loops are how a computer does the same small step thousands of times without getting tired.
සිංහලෙන්: Row 10,000ක් අතින් process කරන්නෙ නෑ. Loop එකක් කියන්නෙ, පොඩි step එකක් දහස් වතාවක් වෙහෙසෙන්නෙ නැතුව computer එකෙන් කරන විදිහ.
What you'll use this for. Training a model is, underneath, a loop that repeats "guess, check how wrong, adjust" hundreds of times — this is where that pattern starts.
සිංහලෙන්: Model එකක් train කරන එකෙන් යටින් තියෙන්නෙ, "guess කරන්න, කොච්චර වැරදිද බලන්න, adjust කරන්න" කියන loop එකක් සිය ගණනක් වතාවක් repeat වෙන එක — ඒ pattern එක පටන් ගන්නෙ මෙතනින්.
Key principles
- for loops know when to stop; while loops don't, by default. A
forloop over a list stops after the last item automatically. Awhileloop keeps going until you explicitly break the condition — forget that, and it runs forever.
for loop දන්නවා කවදා නවත්තන්නද කියලා; while loop, default විදිහට දන්නෙ නෑ
Walkthrough
For loop
laptop1 = int(input('Enter the price of the laptop : '))
laptop2 = int(input('Enter the price of the laptop : '))
laptop3 = int(input('Enter the price of the laptop : '))
laptop4 = int(input('Enter the price of the laptop : '))
laptop5 = int(input('Enter the price of the laptop : '))Output
Enter the price of the laptop : 20000
Enter the price of the laptop : 30000
Enter the price of the laptop : 40000
Enter the price of the laptop : 50000
Enter the price of the laptop : 60000for i in range(7): # 0,1,2,3,4,5,6
laptop_price = int(input('Enter the price of the laptop : '))Output
Enter the price of the laptop : 6
Enter the price of the laptop : 7
Enter the price of the laptop : 8
Enter the price of the laptop : 9
Enter the price of the laptop : 2
Enter the price of the laptop : 3
Enter the price of the laptop : 4numbers = [50, 100, 150, 200]
print(numbers[0])
print(numbers[1])
print(numbers[2])
print(numbers[3])Output
50
100
150
200numbers = [50, 100, 150, 200]
for i in numbers:
print(i)Output
50
100
150
200While Loop
while condition:
statementi = 0
while i<10:
print(i)
i += 1Output
0
1
2
3
4
5
6
7
8
9i = 5
while i<3:
print(i)
i +=1Keep going: ← Lesson 6 · Lesson 8 → · Track it in the Self Guide
Finished this one?
Part of Self Guide · Stage 13: ML 1 — Python & Data Basics. Progress is saved in this browser.



