|
| 1 | +# Operators are used to perform operations on variables and values. |
| 2 | + |
| 3 | +""" |
| 4 | +Python divides the operators in the following groups: |
| 5 | +
|
| 6 | +1. Arithmetic operators |
| 7 | +2. Assignment operators |
| 8 | +3. Comparison operators |
| 9 | +4. Logical operators |
| 10 | +5. Identity operators |
| 11 | +6. Membership operators |
| 12 | +7. Bitwise operators |
| 13 | +
|
| 14 | +Note: For Arithmetic operators and Operator Precedence |
| 15 | +See README.md 2.1.1. Basic Arithmetic Operations for explanation on operators |
| 16 | +""" |
| 17 | +""" |
| 18 | +1. Assignment Operators |
| 19 | +Assignment operators are used to assign values to variables |
| 20 | +""" |
| 21 | +# 1.1. Equal operator (=) |
| 22 | +x = 3 |
| 23 | +print(f"Value of x = 3 -> {x}",end='\n') |
| 24 | +# Prints -> Value of x = 3 -> 3 |
| 25 | + |
| 26 | +# 1.2. Add and equal (+=) |
| 27 | +x += 3 |
| 28 | +print(f"Value of x += 3 -> {x}",end='\n') |
| 29 | +# Prints -> Value of x += 3 -> 6 |
| 30 | + |
| 31 | +# 1.3. Subtract and equal (-=) |
| 32 | +x -= 3 |
| 33 | +print(f"Value of x -= 3 -> {x}",end='\n') |
| 34 | +# Prints -> Value of x -= 3 -> 3 |
| 35 | + |
| 36 | +# 1.4. Multiply and equal (*=) |
| 37 | +x *= 3 |
| 38 | +print(f"Value of x *= 3 -> {x}",end='\n') |
| 39 | +# Prints -> Value of x *= 3 -> 9 |
| 40 | + |
| 41 | +# 1.5. Divide and equal (/=) |
| 42 | +x /= 3 |
| 43 | +print(f"Value of x/=3 -> {x}",end='\n') |
| 44 | +# Prints -> Value of x/=3 -> 3.0 |
| 45 | + |
| 46 | +# 1.6. Mod and equal (%=) |
| 47 | +x %= 3 |
| 48 | +print(f"Value of x%=3 -> {x}",end='\n') |
| 49 | +# Prints -> Value of x%=3 -> 0.0 |
| 50 | + |
| 51 | +# 1.6. Floor and equal (//=) |
| 52 | +x = 9 |
| 53 | +x //= 3 # equals -> x = x//3 |
| 54 | +print(f"Value of x //= 3 -> {x}",end='\n') |
| 55 | +# Prints -> Value of x //= 3 -> 3 |
| 56 | + |
| 57 | +#1.7. Power Operator (**=) |
| 58 | +x **=3 |
| 59 | +print(f"Value of x **=3 -> {x}",end='\n') |
| 60 | +#prints -> Value of x **=3 -> 27 |
| 61 | + |
| 62 | +#1.8. And and Equal &= |
| 63 | +""" |
| 64 | +It compares by bitwise |
| 65 | +like x and 3 are applied using AND operator |
| 66 | +lets say x = 6 |
| 67 | + 6 -> 110 |
| 68 | + 3 -> 011 |
| 69 | +------------ |
| 70 | +AND -> 010 -> 2 |
| 71 | +
|
| 72 | +it only keeps that both has |
| 73 | +""" |
| 74 | +x &=3 |
| 75 | +print(f"Value of x &=3 -> {x}",end='\n') |
| 76 | +#prints -> Value of x &=3 -> 3 |
| 77 | + |
| 78 | +#1.9. OR and Equal |= |
| 79 | +""" |
| 80 | +It compares by bitwise |
| 81 | +like x OR 3 are applied using OR operator |
| 82 | +lets say x = 3 |
| 83 | + 3 -> 011 |
| 84 | + 3 -> 011 |
| 85 | +------------ |
| 86 | +OR -> 011 -> 3 |
| 87 | +
|
| 88 | +it only keeps that is present in any of them like x or 3 |
| 89 | +""" |
| 90 | +x |=3 |
| 91 | +print(f"Value of x |=3 -> {x}",end='\n') |
| 92 | +# prints -> Value of x |=3 -> 3 |
| 93 | + |
| 94 | +#1.10. This is a bitwise XOR assignment operation. ^= |
| 95 | + |
| 96 | +""" |
| 97 | +XOR (^) — "Exclusive OR" |
| 98 | +Compares each bit of two numbers |
| 99 | +
|
| 100 | +If the bits are different, the result is 1 |
| 101 | +If the bits are the same, the result is 0 |
| 102 | +
|
| 103 | + x = 0 → 000 |
| 104 | + 3 → 011 |
| 105 | +---------------- |
| 106 | +Result → 011 = 3 |
| 107 | +
|
| 108 | + 5 → 101 |
| 109 | + 3 → 011 |
| 110 | +---------- |
| 111 | + 110 → 6 |
| 112 | +""" |
| 113 | +x = 5 |
| 114 | +x ^= 3 |
| 115 | + |
| 116 | +print(f"Value of x ^=3 -> {x}",end='\n') |
| 117 | + |
| 118 | +#1.11. Bitwise right shift assignment operator by n bits (>>=) |
| 119 | +x = 8 # binary: 1000 |
| 120 | +x >>= 2 # shift 2 bits to the right 0010 -> 2 |
| 121 | +print(f"Value of x >>=3 -> {x}",end='\n') |
| 122 | +# Value of x >>=3 -> 2 |
| 123 | + |
| 124 | +#1.12. Left shift by n bits (<<=) |
| 125 | +x = 5 # binary: 0000 0101 |
| 126 | +x <<= 2 # shift 2 bits to the right 0001 0100 -> 20 |
| 127 | +print(f"Value of x <<=2 -> {x}",end='\n') |
| 128 | + |
| 129 | +#1.13. := (Walrus Operator) |
| 130 | +# It allows you to assign a value to a variable as part of an expression. |
| 131 | +n = len("hello") |
| 132 | +if n > 3: |
| 133 | + print(n) |
| 134 | +# Output: 5 |
| 135 | +if (n := len("hello")) > 3: |
| 136 | + print(n) # Output: 5 |
| 137 | +# It assigned on same line and compared values also |
| 138 | + |
| 139 | +#2. Logical Operators |
| 140 | +""" |
| 141 | +and Returns True if both statements are true x < 5 and x < 10 |
| 142 | +or Returns True if one of the statements is true x < 5 or x < 4 |
| 143 | +not Reverse the result, returns False if the result is true |
| 144 | +""" |
| 145 | +#2.1. AND, OR and NOT |
| 146 | +x,y = 3,2 |
| 147 | +if x>3 and y>3: |
| 148 | + print(f"Value of x and y greater than 3 ", end='\n') # Didn't Execute |
| 149 | +elif not(x<3 or y<3): |
| 150 | + print(f"Not x less than 3 or y less than 3 ", end='\n') #Didn't Execute |
| 151 | +elif x>=3 or y<3: |
| 152 | + print(f"x greater or equal to 3 or y less than 3 ", end='\n') # x greater or equal to 3 or y less than 3 |
| 153 | +else: |
| 154 | + print(f"x less than 3 or y less than 3 ", end='\n') |
| 155 | + |
| 156 | +# 3. Python Identity Operators (IS or IS NOT) |
| 157 | +print(f"Check x is y {x is y}", end='\n') |
| 158 | +# Check x is y False |
| 159 | +print(f"Check x is y {x is not y}", end='\n') |
| 160 | +# Check x is y True |
| 161 | + |
| 162 | +#4. Python Membership Operators (in or not in) |
| 163 | +x = "This is a member" |
| 164 | +y = "This is a member of Company" |
| 165 | +print(f"Check x in y {x in y}", end='\n') |
| 166 | +# Check x in y True |
| 167 | +print(f"Check x not in y {x not in y}", end='\n') |
| 168 | +# Check x not in y False |
| 169 | + |
0 commit comments