|
| 1 | +# Basic Operators |
| 2 | + |
| 3 | +## Table of Contents |
| 4 | + |
| 5 | +- **[Arithmetic Operators](#arithmetic-operators)** |
| 6 | +- **[Comparison Operators](#comparison-operators)** |
| 7 | +- **[Logical Operators](#logical-operators)** |
| 8 | +- **[Operator Precedence](#operator-precedence)** |
| 9 | + - **[Highest to Lowest Precendence:](#highest-to-lowest-precendence))** |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Arithmetic Operators |
| 14 | + |
| 15 | +Arithmetic operators perform mathematical operations on numbers (integers, floats). |
| 16 | + |
| 17 | +| Operator | Symbol | Description | Example | |
| 18 | +|:----------|:-------|:------------|:--------| |
| 19 | +| **Addition** | `+` | Adds two operands | `3 + 2 = 5` | |
| 20 | +| **Subtraction** | `-` | Subtracts second operand from first | `5 - 2 = 3` | |
| 21 | +| **Multiplication** | `*` | Multiplies two operands | `3 * 2 = 6` | |
| 22 | +| **Division** | `/` | Divides first operand by second, returns float | `5 / 2 = 2.5` | |
| 23 | +| **Modulus** | `%` | Returns remainder of division | `5 % 2 = 1` | |
| 24 | +| **Floor Division** | `//` | Divides first operand by second, returns integer (floor) | `5 // 2 = 2` | |
| 25 | +| **Exponentiation** | `**` | Raises first operand to the power of second | `3 ** 2 = 9` | |
| 26 | + |
| 27 | +**Example:** |
| 28 | + |
| 29 | +```python |
| 30 | +a = 10 |
| 31 | +b = 3 |
| 32 | +print(a + b) # 13 |
| 33 | +print(a - b) # 7 |
| 34 | +print(a * b) # 30 |
| 35 | +print(a / b) # 3.3333333333333335 |
| 36 | +print(a % b) # 1 |
| 37 | +print(a // b) # 3 |
| 38 | +print(a ** b) # 1000 |
| 39 | +``` |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## Comparison Operators |
| 44 | + |
| 45 | +Comparison operators are used to compare two values. They return either `True` or `False`. |
| 46 | + |
| 47 | +| Operator | Symbol | Description | Example | |
| 48 | +|:----------|:-------|:------------|:--------| |
| 49 | +| **Equal to** | `==` | Checks if two values are equal | `3 == 3` → True | |
| 50 | +| **Not equal to** | `!=` | Checks if two values are not equal | `3 != 2` → True | |
| 51 | +| **Greater than** | `>` | Checks if left operand is greater | `5 > 3` → True | |
| 52 | +| **Less than** | `<` | Checks if left operand is smaller | `2 < 5` → True | |
| 53 | +| **Greater than or equal to** | `>=` | Checks if left operand is greater or equal | `5 >= 5` → True | |
| 54 | +| **Less than or equal to** | `<=` | Checks if left operand is smaller or equal | `3 <= 5` → True | |
| 55 | + |
| 56 | +**Example:** |
| 57 | + |
| 58 | +```python |
| 59 | +x = 5 |
| 60 | +y = 3 |
| 61 | +print(x == y) # False |
| 62 | +print(x != y) # True |
| 63 | +print(x > y) # True |
| 64 | +print(x < y) # False |
| 65 | +print(x >= y) # True |
| 66 | +print(x <= y) # False |
| 67 | +``` |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## Logical Operators |
| 72 | + |
| 73 | +Logical operators combine multiple conditions to return a single boolean result. They’re often used with comparison operators. |
| 74 | + |
| 75 | +| Operator | Symbol | Description | Example | |
| 76 | +|:----------|:-------|:------------|:--------| |
| 77 | +| **AND** | `and` | True if both conditions are true | `True and False` → False | |
| 78 | +| **OR** | `or` | True if at least one condition is true | `True or False` → True | |
| 79 | +| **NOT** | `not` | Negates the condition (True to False, vice versa) | `not True` → False | |
| 80 | + |
| 81 | +**Example:** |
| 82 | + |
| 83 | +```python |
| 84 | +a = 5 |
| 85 | +b = 3 |
| 86 | +c = 8 |
| 87 | +print(a > b and c > a) # True, both conditions are True |
| 88 | +print(a > b or b > c) # True, one condition is True |
| 89 | +print(not (a > b)) # False, as a > b is True, but `not` negates it |
| 90 | +``` |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +## Operator Precedence |
| 95 | + |
| 96 | +Operator precedence determines the order in which operations are performed. |
| 97 | +Operators with higher precedence are evaluated first. If operators have the same precedence, they are evaluated from left to right. |
| 98 | + |
| 99 | +### Highest to Lowest Precendence: |
| 100 | + |
| 101 | +1. **Exponentiation:** ** |
| 102 | +2. **Unary Operators:** +, - (positive, negative) |
| 103 | +3. **Multiplication, Division, Modulus, Floor Division:** *, /, %, // |
| 104 | +4. **Addition, Subtraction:** +, - |
| 105 | +5. **Comparison Operators:** <, <=, >, >= |
| 106 | +6. **Equality Operators:** ==, != |
| 107 | +7. **Logical NOT:** not |
| 108 | +8. **Logical AND:** and |
| 109 | +9. **Logical OR:** or |
| 110 | + |
| 111 | +**Example of operator precendence:** |
| 112 | + |
| 113 | +```python |
| 114 | +result = 5 + 3 * 2 ** 2 - 4 / 2 |
| 115 | +# Step-by-step evaluation: |
| 116 | +# 1. 2 ** 2 = 4 |
| 117 | +# 2. 3 * 4 = 12 |
| 118 | +# 3. 4 / 2 = 2 |
| 119 | +# 4. 5 + 12 = 17 |
| 120 | +# 5. 17 - 2 = 15 |
| 121 | +print(result) # Output: 15 |
| 122 | +``` |
| 123 | + |
| 124 | + - **Using parentheses to override precedence:** Parentheses `()` can be used to explicitly control the order of evaluation. |
| 125 | + |
| 126 | + ```python |
| 127 | + result = (5 + 3) * (2 ** 2) - (4 / 2) |
| 128 | + print(result) # Output: 30.0 |
| 129 | + ``` |
0 commit comments