Data Science & Machine Learning
May 21, 2025 at 06:24 PM
Now, let’s move on to the next topic in the Data Science Learning Series:
*Loops & Conditional Statements (Data Science Style)*
In Data Science, we often deal with data in lists, dictionaries, or DataFrames. Loops and conditionals help us filter, transform, and understand that data.
*1. Conditional Statements: if, elif, else*
These help you make decisions based on data.
Example:
age = 25
if age < 18:
print("Minor")
elif age < 60:
print("Adult")
else:
print("Senior")
*Real-world use: Imagine you're categorizing customers based on age groups — you'll use conditionals for that!*
*2. For Loop (Iterating over data)*
Example:
sales = [120, 340, 560, 90, 410]
for amount in sales:
if amount > 300:
print("High Value Sale:", amount)
*Use Case: Filter out big spenders from a transaction list.*
*3. While Loop*
Repeat something until a condition is met.
count = 0
while count < 3:
print("Running analysis...")
count += 1
*4. Looping through a list of dictionaries*
orders = [
{"item": "Pizza", "price": 300},
{"item": "Burger", "price": 150},
{"item": "Pasta", "price": 250}
]
for order in orders:
if order["price"] > 200:
print(order["item"], "is Premium")
*Mini Data Science Use Case:*
Say you have a list of customer purchases and want to tag them as "low", "medium", or "high" value:
purchases = [150, 500, 1200, 300]
for amount in purchases:
if amount < 300:
print(amount, "= Low Value")
elif amount <= 800:
print(amount, "= Medium Value")
else:
print(amount, "= High Value")
*React with ❤️ once you're ready for the quiz*
Data Science Learning Series: https://whatsapp.com/channel/0029Va8v3eo1NCrQfGMseL2D/998
Python Cheatsheet: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L/1660
❤️
👍
❤
🇮🇳
🇵🇸
♥
😂
😢
😮
🙏
140