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

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

Data Science

P5.5 Python मधला एका line चा if (Conditional Expression)

हे का शिकायचं?

दोनपैकी एक value निवडायची असेल तर चार lines पेक्षा एक line वाचायला सोपी.

हे काय आहे?

value_if_true if condition else value_if_false — हे एक expression आहे, ते थेट variable ला देता येतं.

कसं करायचं?

Step 1. True ची value लिहा.

Step 2. if condition लिहा.

Step 3. else नंतर False ची value लिहा.

उदाहरणं: copy करा आणि चालवा

उदाहरण A: Pass किंवा fail

हा code चालवा:

score = 68
result = "Pass" if score >= 70 else "Fail"
print(result)

तुम्हाला दिसणारा output:

Fail

याचा अर्थ: एका line मध्ये marks वरून result मिळाला.

उदाहरण B: Currency label

हा code चालवा:

country = "UK"
symbol = "£" if country == "UK" else "$"
print(symbol + "250")

तुम्हाला दिसणारा output:

£250

याचा अर्थ: Country नुसार योग्य currency चिन्ह (₹, $, £) एका line मध्ये निवडलं.