|
| 1 | +--- |
| 2 | +id: python-operators |
| 3 | +title: Python Operators |
| 4 | +sidebar_label: Python Operators #displays in sidebar |
| 5 | +sidebar_position: 5 |
| 6 | +tags: |
| 7 | + [ |
| 8 | + Python, |
| 9 | + Introduction of python, |
| 10 | + Python Syntax, |
| 11 | + Python Variables, |
| 12 | + Python Operators, |
| 13 | + |
| 14 | + ] |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | + |
| 19 | +# Python Operators |
| 20 | + |
| 21 | +In Python, **operators** are special symbols used to perform operations on variables and values. Python supports a wide variety of operators categorized based on their functionality. |
| 22 | + |
| 23 | + |
| 24 | +## Operator Categories |
| 25 | + |
| 26 | +Python provides the following types of operators: |
| 27 | + |
| 28 | +1. [Arithmetic Operators](#1-arithmetic-operators) |
| 29 | +2. [Comparison Operators](#2-comparison-operators) |
| 30 | +3. [Logical Operators](#3-logical-operators) |
| 31 | +4. [Assignment Operators](#4-assignment-operators) |
| 32 | +5. [Bitwise Operators](#5-bitwise-operators) |
| 33 | +6. [Membership Operators](#6-membership-operators) |
| 34 | +7. [Identity Operators](#7-identity-operators) |
| 35 | +8. [Operator Precedence](#8-operator-precedence) |
| 36 | + |
| 37 | + |
| 38 | +## Arithmetic Operators |
| 39 | + |
| 40 | +Used to perform basic mathematical operations: |
| 41 | + |
| 42 | +| Operator | Description | Example | Result | |
| 43 | +|----------|-------------------|-----------|--------| |
| 44 | +| `+` | Addition | `10 + 5` | `15` | |
| 45 | +| `-` | Subtraction | `10 - 5` | `5` | |
| 46 | +| `*` | Multiplication | `10 * 5` | `50` | |
| 47 | +| `/` | Division | `10 / 5` | `2.0` | |
| 48 | +| `//` | Floor Division | `10 // 3` | `3` | |
| 49 | +| `%` | Modulus (remainder)| `10 % 3` | `1` | |
| 50 | +| `**` | Exponentiation | `2 ** 3` | `8` | |
| 51 | + |
| 52 | + |
| 53 | +## Comparison Operators |
| 54 | + |
| 55 | +Used to compare two values and return a Boolean result (`True` or `False`). |
| 56 | + |
| 57 | +| Operator | Description | Example | Result | |
| 58 | +|----------|----------------------|-------------|--------| |
| 59 | +| `==` | Equal to | `5 == 5` | `True` | |
| 60 | +| `!=` | Not equal to | `5 != 3` | `True` | |
| 61 | +| `>` | Greater than | `5 > 3` | `True` | |
| 62 | +| `<` | Less than | `5 < 3` | `False`| |
| 63 | +| `>=` | Greater than or equal| `5 >= 5` | `True` | |
| 64 | +| `<=` | Less than or equal | `5 <= 3` | `False`| |
| 65 | + |
| 66 | + |
| 67 | +## Logical Operators |
| 68 | + |
| 69 | +Used to combine conditional statements. |
| 70 | + |
| 71 | +| Operator | Description | Example | Result | |
| 72 | +|----------|-----------------------------------|----------------------|--------| |
| 73 | +| `and` | True if both operands are true | `True and False` | `False`| |
| 74 | +| `or` | True if at least one is true | `True or False` | `True` | |
| 75 | +| `not` | Reverses the result | `not True` | `False`| |
| 76 | + |
| 77 | + |
| 78 | +## Assignment Operators |
| 79 | + |
| 80 | +Used to assign values to variables. |
| 81 | + |
| 82 | +| Operator | Example | Same as | |
| 83 | +|----------|----------|----------------| |
| 84 | +| `=` | `x = 5` | Assign 5 to x | |
| 85 | +| `+=` | `x += 3` | `x = x + 3` | |
| 86 | +| `-=` | `x -= 2` | `x = x - 2` | |
| 87 | +| `*=` | `x *= 4` | `x = x * 4` | |
| 88 | +| `/=` | `x /= 2` | `x = x / 2` | |
| 89 | +| `//=` | `x //= 2`| `x = x // 2` | |
| 90 | +| `%=` | `x %= 2` | `x = x % 2` | |
| 91 | +| `**=` | `x **= 2`| `x = x ** 2` | |
| 92 | + |
| 93 | + |
| 94 | +## Bitwise Operators |
| 95 | + |
| 96 | +Used to perform bit-level operations. |
| 97 | + |
| 98 | +| Operator | Description | Example | Result | |
| 99 | +|----------|-------------|-----------|--------| |
| 100 | +| `&` | AND | `5 & 3` | `1` | |
| 101 | +| `|` | OR | `5 | 3` | `7` | |
| 102 | +| `^` | XOR | `5 ^ 3` | `6` | |
| 103 | +| `~` | NOT | `~5` | `-6` | |
| 104 | +| `<<` | Left Shift | `5 << 1` | `10` | |
| 105 | +| `>>` | Right Shift | `5 >> 1` | `2` | |
| 106 | + |
| 107 | + |
| 108 | +## Membership Operators |
| 109 | + |
| 110 | +Used to test if a sequence contains a value. |
| 111 | + |
| 112 | +| Operator | Description | Example | Result | |
| 113 | +|------------|------------------------------|----------------------|--------| |
| 114 | +| `in` | Value exists in the sequence | `"a" in "apple"` | `True` | |
| 115 | +| `not in` | Value not in sequence | `"z" not in "apple"` | `True` | |
| 116 | + |
| 117 | + |
| 118 | +## Identity Operators |
| 119 | + |
| 120 | +Used to compare the memory location of two objects. |
| 121 | + |
| 122 | +| Operator | Description | Example | Result | |
| 123 | +|------------|-------------------------------------|-------------|--------| |
| 124 | +| `is` | Returns `True` if same object | `x is y` | `True` | |
| 125 | +| `is not` | Returns `True` if not same object | `x is not y`| `True` | |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +## Use Cases of Python Operators |
| 130 | + |
| 131 | +--- |
| 132 | + |
| 133 | +### 1. **Arithmetic Operators** |
| 134 | + |
| 135 | +📌 **Use Case**: Shopping Cart Total |
| 136 | + |
| 137 | +```python |
| 138 | +price = 150 |
| 139 | +quantity = 3 |
| 140 | +total = price * quantity # ➜ 450 |
| 141 | +discount = 0.10 |
| 142 | +final_amount = total - (total * discount) # ➜ 405.0 |
| 143 | +``` |
| 144 | + |
| 145 | +**Explanation**: Calculates the total bill with a discount using `*` and `-`. |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +### 2. **Comparison Operators** |
| 150 | + |
| 151 | +📌 **Use Case**: Age Verification for Voting |
| 152 | + |
| 153 | +```python |
| 154 | +age = 17 |
| 155 | +if age >= 18: |
| 156 | + print("Eligible to vote") |
| 157 | +else: |
| 158 | + print("Not eligible") |
| 159 | +``` |
| 160 | + |
| 161 | +**Explanation**: Compares age using `>=` to determine eligibility. |
| 162 | + |
| 163 | +--- |
| 164 | + |
| 165 | +### 3. **Logical Operators** |
| 166 | + |
| 167 | +📌 **Use Case**: Login System Authentication |
| 168 | + |
| 169 | +```python |
| 170 | +username = "admin" |
| 171 | +password = "1234" |
| 172 | + |
| 173 | +if username == "admin" and password == "1234": |
| 174 | + print("Login successful") |
| 175 | +else: |
| 176 | + print("Invalid credentials") |
| 177 | +``` |
| 178 | + |
| 179 | +**Explanation**: Combines two conditions using `and`. |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +### 4. **Assignment Operators** |
| 184 | + |
| 185 | +📌 **Use Case**: Updating Game Score |
| 186 | + |
| 187 | +```python |
| 188 | +score = 0 |
| 189 | +score += 10 # Player scored |
| 190 | +score += 5 # Bonus |
| 191 | +# Final score = 15 |
| 192 | +``` |
| 193 | + |
| 194 | +**Explanation**: Increments the score using `+=`. |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +### 5. **Bitwise Operators** |
| 199 | + |
| 200 | +📌 **Use Case**: File Permission System (Read = 4, Write = 2, Execute = 1) |
| 201 | + |
| 202 | +```python |
| 203 | +read = 4 |
| 204 | +write = 2 |
| 205 | +execute = 1 |
| 206 | + |
| 207 | +permission = read | write # ➜ 6 (read + write) |
| 208 | +has_write = permission & write # ➜ 2 (True) |
| 209 | +``` |
| 210 | + |
| 211 | +**Explanation**: Combines permissions using `|` and checks with `&`. |
| 212 | + |
| 213 | +--- |
| 214 | + |
| 215 | +### 6. **Membership Operators** |
| 216 | + |
| 217 | +📌 **Use Case**: Search Term Filtering |
| 218 | + |
| 219 | +```python |
| 220 | +query = "python" |
| 221 | +if "python" in ["java", "python", "c++"]: |
| 222 | + print("Result found") |
| 223 | +``` |
| 224 | + |
| 225 | +**Explanation**: Checks if a word exists in a list using `in`. |
| 226 | + |
| 227 | +--- |
| 228 | + |
| 229 | +### 7. **Identity Operators** |
| 230 | + |
| 231 | +📌 **Use Case**: Comparing Object Identity |
| 232 | + |
| 233 | +```python |
| 234 | +x = [1, 2, 3] |
| 235 | +y = x |
| 236 | +z = [1, 2, 3] |
| 237 | + |
| 238 | +print(x is y) # True |
| 239 | +print(x is z) # False |
| 240 | +``` |
| 241 | + |
| 242 | +**Explanation**: Uses `is` to check if variables point to the same object in memory. |
| 243 | + |
| 244 | +--- |
| 245 | + |
| 246 | +### 8. **Operator Precedence** |
| 247 | + |
| 248 | +📌 **Use Case**: Evaluating an Expression |
| 249 | + |
| 250 | +```python |
| 251 | +result = 10 + 5 * 2 # ➜ 10 + (5 * 2) = 20 |
| 252 | +``` |
| 253 | + |
| 254 | +**Explanation**: `*` is evaluated before `+` due to higher precedence. |
| 255 | + |
| 256 | +--- |
| 257 | + |
| 258 | +## Summary Table |
| 259 | + |
| 260 | +| Operator Type | Example Use Case | |
| 261 | +| ------------- | ---------------------------------- | |
| 262 | +| Arithmetic | Calculating total cost | |
| 263 | +| Comparison | Validating age for access | |
| 264 | +| Logical | Checking login credentials | |
| 265 | +| Assignment | Updating scores or counters | |
| 266 | +| Bitwise | Managing file permissions (bits) | |
| 267 | +| Membership | Search and filter operations | |
| 268 | +| Identity | Verifying object references | |
| 269 | +| Precedence | Proper expression evaluation order | |
| 270 | + |
| 271 | + |
| 272 | + |
| 273 | +## Conclusion |
| 274 | + |
| 275 | +Operators are the core building blocks of logic and calculation in Python. Understanding how they work is crucial to writing effective Python code. |
| 276 | + |
0 commit comments