> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/language/python/basic/conditional-statements.md).

# Conditional Statements

Conditional statements are used to **control the flow of execution** in a Python program based on certain conditions. They allow the program to make **decisions** and execute **specific blocks of code** accordingly.

***

### **1. Basic Concept**

A **condition** in Python is an expression that evaluates to either **`True`** or **`False`**. These conditions are usually created using **comparison operators** (`==`, `>`, `<`, etc.) or **logical operators** (`and`, `or`, `not`).

***

### **2. Basic `if` Statement**

The simplest form of a conditional statement.

```python
if condition:
    # Code block executed if condition is True
```

#### **Example**

```python
age = 18

if age >= 18:
    print("You are eligible to vote.")
```

**Output:**

```
You are eligible to vote.
```

***

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

Used to execute one block when the condition is **True**, and another when it is **False**.

```python
if condition:
    # Executes if condition is True
else:
    # Executes if condition is False
```

#### **Example**

```python
age = 16

if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote.")
```

**Output:**

```
You are not eligible to vote.
```

***

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

Used when you have **multiple conditions** to check sequentially.

```python
if condition1:
    # Executes if condition1 is True
elif condition2:
    # Executes if condition2 is True
else:
    # Executes if none of the above are True
```

#### **Example**

```python
marks = 85

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

**Output:**

```
Grade: B
```

***

### **5. Nested `if` Statements**

An `if` statement **inside another** `if` block. Useful when a condition itself depends on another condition.

#### **Example**

```python
age = 20
citizen = True

if age >= 18:
    if citizen:
        print("You can vote.")
    else:
        print("You must be a citizen to vote.")
else:
    print("You are not eligible to vote.")
```

**Output:**

```
You can vote.
```

***

### **6. Shorthand (Single-Line) `if` Statements**

For short, simple conditions, you can write them on one line.

#### **Single `if`**

```python
x = 10
if x > 5: print("x is greater than 5")
```

#### **`if-else` on One Line**

```python
x = 5
print("Positive") if x > 0 else print("Non-positive")
```

**Output:**

```
Positive
```

***

### **7. Conditional Expressions (Ternary Operator)**

Python’s shorthand way to return values based on a condition.

```python
result = "Even" if num % 2 == 0 else "Odd"
```

#### **Example**

```python
num = 7
result = "Even" if num % 2 == 0 else "Odd"
print(result)
```

**Output:**

```
Odd
```

***

### **8. Logical and Comparison Operators in Conditions**

#### **Comparison Operators**

| Operator | Meaning                  | Example  | Result |
| -------- | ------------------------ | -------- | ------ |
| `==`     | Equal to                 | `5 == 5` | ✅ True |
| `!=`     | Not equal to             | `5 != 3` | ✅ True |
| `>`      | Greater than             | `7 > 4`  | ✅ True |
| `<`      | Less than                | `3 < 5`  | ✅ True |
| `>=`     | Greater than or equal to | `5 >= 5` | ✅ True |
| `<=`     | Less than or equal to    | `4 <= 6` | ✅ True |

#### **Logical Operators**

| Operator | Meaning                               | Example            | Result                      |
| -------- | ------------------------------------- | ------------------ | --------------------------- |
| `and`    | True if **both** conditions are True  | `x > 0 and x < 10` | ✅ True if x is between 0–10 |
| `or`     | True if **any one** condition is True | `x == 0 or y == 0` | ✅ True if either is 0       |
| `not`    | Reverses the condition                | `not x > 5`        | ✅ True if x ≤ 5             |

***

### **9. Combining Conditions**

You can combine multiple conditions using logical operators.

#### **Example**

```python
age = 25
has_id = True

if age >= 18 and has_id:
    print("You are allowed entry.")
else:
    print("Access denied.")
```

**Output:**

```
You are allowed entry.
```

***

### **10. Using Conditions with Data Structures**

#### **Example 1: Checking Membership**

```python
fruits = ["apple", "banana", "cherry"]

if "banana" in fruits:
    print("Banana is available.")
```

#### **Example 2: Checking Empty Lists**

```python
items = []

if not items:
    print("The list is empty.")
```

**Output:**

```
The list is empty.
```

***

### **11. Example**

```python
temperature = int(input("Enter temperature: "))

if temperature > 30:
    print("It's a hot day.")
elif 20 <= temperature <= 30:
    print("It's a nice day.")
elif 10 <= temperature < 20:
    print("It's a bit cold.")
else:
    print("It's cold outside.")
```

***

### **12. Summary**

| Type           | Description                        | Example                       |
| -------------- | ---------------------------------- | ----------------------------- |
| `if`           | Executes code if condition is True | `if x > 0:`                   |
| `if-else`      | Executes one block or another      | `if x>0: ... else: ...`       |
| `if-elif-else` | Multiple conditions                | `if... elif... else...`       |
| Nested `if`    | `if` inside another `if`           | `if x>0: if y>0:`             |
| Shorthand      | One-line conditional               | `"Even" if x%2==0 else "Odd"` |
