Python Functions for Reusability and Scalability

Functions in Python: Write Once, Reuse Everywhere Day 8 of #30DaysOfPython 🐍 Until now, we have been writing logic step by step using conditions and loops. Today, we learned how to group that logic into reusable blocks using functions. This is where Python code becomes clean, reusable, and scalable. Example 1: A simple function 👇 def calculate_discount(price): return price * 0.9 final_price = calculate_discount(2500) print(final_price) 👉 Output: 2250.0 Here: 🔹 def → defines a function 🔹 price → input (parameter) 🔹 return → sends the result back Example 2: Reusing the same function 👇 prices = [1500, 2500, 4000] for p in prices: print(calculate_discount(p)) This shows the real power of functions — one logic, multiple values. Example 3: Function with business logic 👇 def sale_type(amount): if amount > 3000: return "High value sale" else: return "Regular sale" print(sale_type(4000)) 👉 Output: High value sale This is how rules and classifications are handled in real projects. DA Insight 💡 Functions help us: ✔ Avoid repeating code ✔ Keep logic in one place ✔ Make code easier to read and maintain ✔ Apply the same rule across datasets Think of it as: Excel → Reusable formulas SQL → Stored logic / expressions Power BI → Measures Python → Functions Next up: Day 9 – Built-in Functions (Python’s shortcuts) 🚀 #30DaysOfPython #PythonForDataAnalysis #DataAnalytics #LearningInPublic #DataAnalyst #Upskilling

To view or add a comment, sign in

Explore content categories