Ravindra Bagale · Data Scienceसर्व coursesया course चे lessonsशोधाEnglish

Data Science · मराठी आवृत्ती

3.3 if, for आणि functions

रवींद्र बागले यांच्या course वर आधारित · सहज मराठीत explanation

अट तपासून वेगळं काम करायचं असेल तर if वापरतो. पुढच्या उदाहरणात Delivered आणि किमान ₹1,000 amount असेल तर high-value band मिळतो.

status = "Delivered"
amount = 1299

if status == "Delivered" and amount >= 1000:
    band = "High value delivered"
elif status == "Cancelled":
    band = "Cancelled"
else:
    band = "Other"
print(band)

def delivery_fee(order_amount: float) -> int:
    """Fictional fee slabs for classroom only."""
    if order_amount >= 499:
        return 0
    if order_amount >= 199:
        return 15
    return 30

print(delivery_fee(250))  # 15

वरील delivery_fee function मधले fee slabs फक्त class साठी काल्पनिक आहेत. आता छोट्या list वर loop वापरून spike तपासूया:

daily_orders = [180, 210, 950, 880, 200]  # fictional; day 3–4 = Diwali rush
for i, n in enumerate(daily_orders, start=1):
    tag = "SPIKE" if n > 500 else "normal"
    print(f"Day {i}: {n} orders ({tag})")

Practice

is_maharashtra_city(name) नावाचं function लिहा. Course मधल्या सहा cities साठीच ते True देईल. "Pune" आणि "Mumbai" देऊन test करा.

इथला नियम “course मधल्या सहा cities” असा आहे. Mumbai महाराष्ट्रात असूनही या ठरवलेल्या list मध्ये नसल्यामुळे या function ने False द्यायचं आहे.