TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun · 77.8K subscribers
Post #3921 5.47K
✅ Python Basics for Data Science: Part-2

Loops Functions 🔁🧠

These two concepts are key to writing clean, efficient, and reusable code — especially when working with data.

1️⃣ Loops in Python
Loops help you repeat tasks like reading data, checking values, or processing items in a list.

For Loop
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)


While Loop
count = 1
while count <= 3:
print("Loading...", count)
count += 1


Loop with Condition
numbers = [10, 5, 20, 3]
for num in numbers:
if num > 10:
print(num, "is greater than 10")


2️⃣ Functions in Python
Functions let you group code into blocks you can reuse.

Basic Function
def greet(name):
return f"Hello, {name}!"

print(greet("Riya"))


Function with Logic
def is_even(num):
if num % 2 == 0:
return True
return False

print(is_even(4)) # Output: True


Function for Calculation
def square(x):
return x * x

print(square(6)) # Output: 36


✅ Why This Matters in Data Science
• Loops help in iterating over datasets
• Functions make your data cleaning reusable
• Helps organize long analysis code into simple blocks

🎯 Practice Task for You:
• Write a for loop to print numbers from 1 to 10
• Create a function that takes two numbers and returns their average
• Make a function that returns "Even" or "Odd" based on input

💬 Tap ❤️ for more!
  • ❤ 16
More from @datasciencefun
  1. Oct 17, 2026𝗜𝗕𝗠 𝗙𝗥𝗘𝗘 𝗧𝗘𝗖𝗛 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 𝟮𝟬𝟮𝟲! 🎓 Explore IBM Cognitive Class and build…
  2. Oct 16, 2026𝗗𝗮𝘁𝗮 𝘀𝗰𝗶𝗲𝗻𝗰𝗲 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗶𝗼𝗻 𝗪𝗶𝘁𝗵 𝗔𝗜😍 Future Ready Data Scien…
  3. Oct 15, 2026Post #4665
  4. Oct 15, 2026Post #4664
  5. Oct 15, 2026Post #4663
  6. Oct 15, 2026Post #4662
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 →