TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun ยท 77.8K subscribers
Post #4436 2.88K
๐Ÿš€ Data Science Roadmap 2026

๐Ÿ“˜ Phase 1: Programming Fundamentals

๐Ÿ Topic 5: Python Loops ("for" & "while")

Welcome back! ๐Ÿ‘‹

In the previous lesson, you learned how Python makes decisions using if, elif, and else. Now it's time to learn how to repeat tasks automatically using loops.

Loops are one of the most powerful concepts in Python. Instead of writing the same code multiple times, you can use loops to execute a block of code repeatedly.

In Data Science, loops are commonly used to process datasets, automate repetitive tasks, iterate through lists, and build machine learning workflows.

๐Ÿ”น 1. What is a Loop?

A loop is used to execute a block of code multiple times until a condition is met.

Without loops:

print("Hello")
print("Hello")
print("Hello")
print("Hello")
print("Hello")


Using a loop:

for i in range(5):
print("Hello")


Both produce the same output, but the loop is much shorter and easier to maintain.

๐Ÿ”น 2. Types of Loops in Python

Python provides two main types of loops:

โœ… "for" Loop

โœ… "while" Loop

๐Ÿ”น 3. The "for" Loop โญ

A "for" loop is used when you know how many times you want to repeat a task.

Syntax

for variable in sequence:
# Code to execute


Example

for i in range(5):
print(i)


Output:

0  
1
2
3
4


Notice that range(5) generates numbers from 0 to 4.

๐Ÿ”น 4. The "range()" Function โญ

The range() function generates a sequence of numbers.

Example 1

for i in range(5):
print(i)


Output: 0 1 2 3 4

Example 2

for i in range(1, 6):
print(i)


Output: 1 2 3 4 5

Example 3

for i in range(2, 11, 2):
print(i)


Output: 2 4 6 8 10

The third argument is called the step size.

๐Ÿ”น 5. Looping Through a List

fruits = ["Apple", "Banana", "Mango"]

for fruit in fruits:
print(fruit)


Output:

Apple  
Banana
Mango


๐Ÿ”น 6. The "while" Loop โญ

A "while" loop continues executing as long as the condition remains True.

Syntax

while condition:
# Code


Example

count = 1

while count <= 5:
print(count)
count += 1


Output: 1 2 3 4 5

๐Ÿ”น 7. Infinite Loop

Be careful when using "while" loops.

while True:
print("Hello")


This loop never stops unless interrupted.

Always ensure the condition eventually becomes False.

๐Ÿ”น 8. Loop Control Statements โญ

"break" - Stops the loop immediately

for i in range(10):
if i == 5:
break
print(i)


Output: 0 1 2 3 4

"continue" - Skips the current iteration

for i in range(5):
if i == 2:
continue
print(i)


Output: 0 1 3 4

"pass" - Acts as a placeholder

for i in range(5):
pass


Useful when writing incomplete code.

๐Ÿ”น 9. Nested Loops

A loop inside another loop.

for i in range(3):
for j in range(2):
print(i, j)


Output:

0 0  
0 1
1 0
1 1
2 0
2 1


๐Ÿ”น 10. Real-World Data Science Example

Calculate the total sales.
  • โค 7
  • ๐Ÿ‘ 1
More from @datasciencefun
  1. Oct 8, 2026๐Ÿ”ฌ Kandinsky 6.0 Video: Training Details and Architecture The new Kandinsky 6.0 Video lineโ€ฆ
  2. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  3. Oct 7, 2026ORDER BY salary DESC LIMIT 5; Mistake 3 โ€” Forgetting that LIMIT applies after sorting Forโ€ฆ
  4. Oct 7, 2026๐Ÿš€ Data Science Roadmap 2026 ๐Ÿ“ Phase 3: SQL for Data Science ๐Ÿ“– Topic 4 โ€” LIMIT LIMIT isโ€ฆ
  5. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  6. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’