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

# Python Conditionals

## 1. **What are Conditionals?**

* **Conditionals** are used to make decisions in a program based on certain conditions.
* They allow the program to execute specific blocks of code only if certain conditions are met.
* Python uses `if`, `elif`, and `else` keywords for conditionals.

## 2. **Types of Conditional Statements**

### **1. `if` Statement**

* The `if` statement checks a condition. If the condition is `True`, the block of code under it is executed.
* Syntax:
  ```python theme={"system"}
  if condition:
      # Code to execute if condition is True
  ```

**Example**:

```python theme={"system"}
age = 20
if age >= 18:
    print("Raj is eligible to vote.")
```

### **2. `if-else` Statement**

* The `else` block is executed if the `if` condition is `False`.
* Syntax:
  ```python theme={"system"}
  if condition:
      # Code to execute if condition is True
  else:
      # Code to execute if condition is False
  ```

**Example**:

```python theme={"system"}
age = 16
if age >= 18:
    print("Ram is eligible to vote.")
else:
    print("Ram is not eligible to vote.")
```

### **3. `if-elif-else` Statement**

* The `elif` (else-if) statement allows you to check multiple conditions.
* The `else` block is executed if none of the `if` or `elif` conditions are `True`.
* Syntax:
  ```python theme={"system"}
  if condition1:
      # Code to execute if condition1 is True
  elif condition2:
      # Code to execute if condition2 is True
  else:
      # Code to execute if all conditions are False
  ```

**Example**:

```python theme={"system"}
marks = 85
if marks >= 90:
    print("Anand scored an A grade.")
elif marks >= 80:
    print("Anand scored a B grade.")
else:
    print("Anand scored a C grade.")
```

### **4. Nested Conditionals**

* You can nest `if` statements inside other `if` statements to check for more complex conditions.
* Syntax:
  ```python theme={"system"}
  if condition1:
      if condition2:
          # Code to execute if both conditions are True
  ```

**Example**:

```python theme={"system"}
age = 20
has_id = True
if age >= 18:
    if has_id:
        print("Bala is allowed to enter.")
    else:
        print("Bala needs an ID to enter.")
else:
    print("Bala is too young to enter.")
```

## 3. **Logical Operators in Conditionals**

* Logical operators (`and`, `or`, `not`) are used to combine multiple conditions.
* **`and`**: Both conditions must be `True`.
* **`or`**: At least one condition must be `True`.
* **`not`**: Inverts the condition.

**Examples**:

```python theme={"system"}
age = 20
has_license = True

if age >= 18 and has_license:
    print("Karthik can drive.")
else:
    print("Karthik cannot drive.")

if age < 18 or not has_license:
    print("Kumar cannot drive.")
else:
    print("Kumar can drive.")
```

## 4. **Ternary Operator (Conditional Expression)**

* A shorthand way to write simple `if-else` statements in a single line.
* Syntax:
  ```python theme={"system"}
  value_if_true if condition else value_if_false
  ```

**Example**:

```python theme={"system"}
age = 20
status = "Eligible" if age >= 18 else "Not Eligible"
print(f"David is {status} to vote.")
```

## 5. **Additional Examples**

* **`if` Statement**:
  ```python theme={"system"}
  name = "Siva"
  age = 22
  if age >= 18:
      print(f"{name} is eligible to vote.")
  ```

* **`if-else` Statement**:
  ```python theme={"system"}
  name = "Ramesh"
  marks = 75
  if marks >= 50:
      print(f"{name} has passed the exam.")
  else:
      print(f"{name} has failed the exam.")
  ```

* **`if-elif-else` Statement**:
  ```python theme={"system"}
  name = "Suresh"
  salary = 75000
  if salary >= 100000:
      print(f"{name} is in the high-income bracket.")
  elif salary >= 50000:
      print(f"{name} is in the middle-income bracket.")
  else:
      print(f"{name} is in the low-income bracket.")
  ```

* **Nested Conditionals**:
  ```python theme={"system"}
  name = "Sathish"
  age = 17
  has_permission = True
  if age >= 18:
      print(f"{name} can attend the event.")
  else:
      if has_permission:
          print(f"{name} can attend with parental permission.")
      else:
          print(f"{name} cannot attend the event.")
  ```

* **Ternary Operator**:
  ```python theme={"system"}
  name = "Sujatha"
  age = 16
  status = "Adult" if age >= 18 else "Minor"
  print(f"{name} is a {status}.")
  ```

## 6. **Best Practices**

* Use meaningful variable names to make conditions clear.
* Avoid deeply nested conditionals to keep the code readable.
* Use parentheses to group complex conditions for clarity.
* Use comments to explain complex logic.

**Example**:

```python theme={"system"}
# Check if a person is eligible for a loan
age = 25
income = 60000
credit_score = 700

if (age >= 18) and (income >= 50000) and (credit_score >= 650):
    print("Eligible for a loan.")
else:
    print("Not eligible for a loan.")
```
