# P5.2 Python मध्ये if आणि else कसं लिहायचं?

Source: https://ravindrabagale.com/mr/datascience/py05-python-if-elif-else-statements-with-real-examples/p5-2-how-to-write-if-and-else-in-python.html
Language: mr (Marathi with English technical terms)

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

Condition खरी असेल तेव्हाच काही code चालवायचा असतो — उदा. मोठ्या order ला free delivery.

हे काय आहे?

if condition: नंतरचा indent केलेला code condition True असेल तेव्हा चालतो. else: खालचा code False असेल तेव्हा. Colon आणि 4 spaces indent विसरू नका.

कसं करायचं?

Step 1. if condition: लिहा.
Step 2. खालचा code 4 spaces आत लिहा.
Step 3. गरज असेल तर else: जोडा.

Python आणि इतर भाषा: तुलना

- Python: if x > 30:  (then indented lines)
- Java: if (x > 30) { ... } else { ... }
- JavaScript: if (x > 30) { ... } else { ... }
- Go: if x > 30 { ... } else { ... }

उदाहरण A: एकच if

```python
balance = 75
if balance < 100:
    print("Low balance warning")
```

Output:

```text
Low balance warning
```

याचा अर्थ: Condition True होती म्हणून आतला code चालला.

उदाहरण B: if सोबत else

```python
age = 15
if age >= 18:
    print("Adult ward")
else:
    print("Children's ward")
```

Output:

```text
Children's ward
```

याचा अर्थ: Condition False झाली की else चा code चालतो. दोनपैकी एकच भाग चालतो.

उदाहरण C: निर्णयावरून value ठरवा

```python
minutes = 42
if minutes > 30:
    status = "Late"
else:
    status = "On time"
print(status)
```

Output:

```text
Late
```

याचा अर्थ: if/else ने variable ला value दिली आणि नंतर ती वापरली.

रवींद्र बागले यांची tip

Python curly brackets ऐवजी indentation वापरतो. नेहमी 4 spaces वापरा. Colon नंतर Enter दाबल्यावर VS Code आणि Jupyter हे आपोआप करतात.
