> ## Documentation Index
> Fetch the complete documentation index at: https://ai.tharung.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Conditional Statements

***

# 1. Conditional Statements (`if`, `elif`, `else`)

Used to execute different code based on conditions.

## Syntax

```python theme={null}
if condition:
    # code
elif condition:
    # code
else:
    # code
```

***

## `if`

Runs only if the condition is `True`.

```python theme={null}
age = 20

if age >= 18:
    print("Adult")
```

Output

```text theme={null}
Adult
```

***

## `if...else`

```python theme={null}
age = 15

if age >= 18:
    print("Adult")
else:
    print("Minor")
```

Output

```text theme={null}
Minor
```

***

## `if...elif...else`

Checks multiple conditions.

```python theme={null}
marks = 85

if marks >= 90:
    print("Grade A")
elif marks >= 75:
    print("Grade B")
elif marks >= 50:
    print("Grade C")
else:
    print("Fail")
```

Output

```text theme={null}
Grade B
```

***

## Nested if

```python theme={null}
age = 20
citizen = True

if age >= 18:
    if citizen:
        print("Eligible to vote")
```

Output

```text theme={null}
Eligible to vote
```

***

## Short Hand if

```python theme={null}
a = 10

if a > 5: print("Greater")
```

***

## Short Hand if...else (Ternary Operator)

```python theme={null}
a = 10

print("Even") if a % 2 == 0 else print("Odd")
```

***

## Logical Operators

### `and`

Both conditions must be True.

```python theme={null}
age = 25
salary = 40000

if age > 18 and salary > 30000:
    print("Eligible")
```

***

### `or`

At least one condition must be True.

```python theme={null}
age = 15
student = True

if age > 18 or student:
    print("Allowed")
```

***

### `not`

Reverses the result.

```python theme={null}
logged_in = False

if not logged_in:
    print("Please login")
```

***

## Comparison Operators

| Operator | Meaning          |
| :------- | :--------------- |
| `==`     | Equal            |
| `!=`     | Not equal        |
| `>`      | Greater than     |
| `<`      | Less than        |
| `>=`     | Greater or equal |
| `<=`     | Less or equal    |

***

## Membership Operators

```python theme={null}
fruits = ["Apple", "Banana"]

if "Apple" in fruits:
    print("Found")
```

***

## Identity Operators

```python theme={null}
a = [1,2]
b = a

print(a is b)
```

Output

```text theme={null}
True
```

***

# Quick Revision

### Conditional Statements

* `if` → Check one condition.
* `elif` → Check another condition.
* `else` → Runs if all conditions are False.
* Nested `if` → `if` inside another `if`.
* Ternary operator → Short `if-else`.
