|
| 1 | +--- |
| 2 | +id: python-conditional-statements |
| 3 | +title: Conditional Statements in Python |
| 4 | +sidebar_label: Conditional Statements in Python |
| 5 | +sidebar_position: 9 |
| 6 | +tags: |
| 7 | + [ |
| 8 | + Python, |
| 9 | + Conditional Statements, |
| 10 | + if else, |
| 11 | + elif, |
| 12 | + Control Flow, |
| 13 | + Python Syntax |
| 14 | + ] |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +# Conditional Statements in Python |
| 19 | + |
| 20 | +Conditional statements in Python allow you to make decisions in your code. They control the flow of execution depending on whether a given condition is **True** or **False**. |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## The `if` Statement |
| 25 | + |
| 26 | +The simplest conditional is the `if` statement. |
| 27 | + |
| 28 | +**Syntax:** |
| 29 | + |
| 30 | +```python |
| 31 | +if condition: |
| 32 | + # block of code |
| 33 | +``` |
| 34 | + |
| 35 | +**Example:** |
| 36 | + |
| 37 | +```python |
| 38 | +x = 10 |
| 39 | +if x > 5: |
| 40 | + print("x is greater than 5") |
| 41 | +``` |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +## The `if...else` Statement |
| 46 | + |
| 47 | +The `else` block runs when the `if` condition is **False**. |
| 48 | + |
| 49 | +```python |
| 50 | +x = 3 |
| 51 | +if x > 5: |
| 52 | + print("x is greater than 5") |
| 53 | +else: |
| 54 | + print("x is less than or equal to 5") |
| 55 | +``` |
| 56 | + |
| 57 | +**Output:** |
| 58 | +``` |
| 59 | +x is less than or equal to 5 |
| 60 | +``` |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +## The `if...elif...else` Statement |
| 65 | + |
| 66 | +`elif` stands for "else if". It lets you check multiple conditions. |
| 67 | + |
| 68 | +```python |
| 69 | +score = 85 |
| 70 | + |
| 71 | +if score >= 90: |
| 72 | + print("Grade A") |
| 73 | +elif score >= 75: |
| 74 | + print("Grade B") |
| 75 | +elif score >= 60: |
| 76 | + print("Grade C") |
| 77 | +else: |
| 78 | + print("Grade D") |
| 79 | +``` |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## Nested `if` Statements |
| 84 | + |
| 85 | +You can put an `if` statement inside another `if` statement. |
| 86 | + |
| 87 | +```python |
| 88 | +x = 15 |
| 89 | +if x > 10: |
| 90 | + if x < 20: |
| 91 | + print("x is between 10 and 20") |
| 92 | +``` |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## Conditional Expressions (Ternary Operator) |
| 97 | + |
| 98 | +Python has a shorter way to write `if...else` using **ternary expressions**. |
| 99 | + |
| 100 | +```python |
| 101 | +age = 18 |
| 102 | +status = "Adult" if age >= 18 else "Minor" |
| 103 | +print(status) |
| 104 | +``` |
| 105 | + |
| 106 | +**Output:** |
| 107 | +``` |
| 108 | +Adult |
| 109 | +``` |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +## Logical Operators in Conditions |
| 114 | + |
| 115 | +You can combine multiple conditions using `and`, `or`, and `not`. |
| 116 | + |
| 117 | +```python |
| 118 | +x = 7 |
| 119 | +if x > 5 and x < 10: |
| 120 | + print("x is between 5 and 10") |
| 121 | + |
| 122 | +if x < 5 or x > 10: |
| 123 | + print("x is outside 5 to 10") |
| 124 | + |
| 125 | +if not x == 8: |
| 126 | + print("x is not 8") |
| 127 | +``` |
| 128 | + |
| 129 | +--- |
| 130 | + |
| 131 | +## Comparing Multiple Values |
| 132 | + |
| 133 | +You can check if a value exists in a sequence using `in`. |
| 134 | + |
| 135 | +```python |
| 136 | +fruits = ["apple", "banana", "cherry"] |
| 137 | +if "apple" in fruits: |
| 138 | + print("Apple is in the list") |
| 139 | +``` |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +## Indentation Rules |
| 144 | + |
| 145 | +In Python, indentation is important for defining code blocks. |
| 146 | + |
| 147 | +```python |
| 148 | +if True: |
| 149 | + print("This is indented correctly") |
| 150 | + print("Still inside if block") |
| 151 | +print("Outside if block") |
| 152 | +``` |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +## Summary Table |
| 157 | + |
| 158 | +| Statement Type | Description | |
| 159 | +|------------------------|------------------------------------------| |
| 160 | +| `if` | Executes a block if condition is `True` | |
| 161 | +| `if...else` | Executes `else` block if condition is `False` | |
| 162 | +| `if...elif...else` | Checks multiple conditions | |
| 163 | +| Nested `if` | `if` inside another `if` | |
| 164 | +| Ternary Expression | Short form of `if...else` | |
| 165 | + |
| 166 | +--- |
| 167 | + |
| 168 | +## Conclusion |
| 169 | + |
| 170 | +Conditional statements are essential for decision-making in programs. Mastering `if`, `elif`, and `else` allows you to control your program's logic effectively. |
0 commit comments