Exponentiation and Arithmetic Logic
Python handles integer exponentiation using the double asterisk operator. When calculating powers, the base value is raised to the specified exponent.
base_val = 5
power_val = 2
result_val = base_val ** power_val
print(result_val) # Output: 25
In this scenario, the base value is 5 and the exponant is 2. The operation computes 5 squared, resulting in 25 stored within result_val.
Comparison Operations
Integers are frequently evaluated against one another using comparison operators. These expressions return boolean values (True or False) based on the relationship between operands.
| Operator | Description |
|---|---|
| == | Checks if left and right operands are equal |
| != | Checks if left and right operands are not equal |
| > | Checks if left operand is greater than right |
| < | Checks if left operand is less than right |
| >= | Checks if left operand is greater than or equal to right |
| <= | Checks if left operand is less than or equal to right |
Equality Check (==)
This operator validates whether two values are identical. If the condition holds, it returns True; otherwise, it returns False.
operand_left = 10
operand_right = 10
print(operand_left == operand_right) # Output: True
Since both variables hold the value 10, the equality condition is satisfied.
operand_left = 10
operand_right = 15
print(operand_left == operand_right) # Output: False
Here, the values differ, so the expression evaluates to False.
Inequality Check (!=)
This operator confirms if two values differ. It returns True when values are not the same.
operand_left = 10
operand_right = 10
print(operand_left != operand_right) # Output: False
Because the values match, the inequality condition fails.
operand_left = 10
operand_right = 15
print(operand_left != operand_right) # Output: True
The values are distinct, satisfying the inequality condition.
Greater Than (>)
Evaluates if the left operand exceeds the right operand.
operand_left = 10
operand_right = 15
print(operand_left > operand_right) # Output: False
10 is not greater than 15.
operand_left = 10
operand_right = 10
print(operand_left > operand_right) # Output: False
10 is equal to 10, not greater.
operand_left = 10
operand_right = 5
print(operand_left > operand_right) # Output: True
10 exceeds 5, returning True.
Less Than (<)
Evaluates if the left operand is smaller than the right operand.
operand_left = 15
operand_right = 10
print(operand_left < operand_right) # Output: False
15 is not smaller than 10.
operand_left = 10
operand_right = 10
print(operand_left < operand_right) # Output: False
Values are equal.
operand_left = 5
operand_right = 10
print(operand_left < operand_right) # Output: True
5 is smaller than 10.
Greater or Equal (>=)
Returns True if the left operand is larger than or equal to the right.
operand_left = 15
operand_right = 10
print(operand_left >= operand_right) # Output: True
operand_left = 10
operand_right = 10
print(operand_left >= operand_right) # Output: True
operand_left = 5
operand_right = 10
print(operand_left >= operand_right) # Output: False
Less or Equal (<=)
Returns True if the left operand is smaller than or equal to the right.
operand_left = 15
operand_right = 10
print(operand_left <= operand_right) # Output: False
operand_left = 10
operand_right = 10
print(operand_left <= operand_right) # Output: True
operand_left = 5
operand_right = 10
print(operand_left <= operand_right) # Output: True
Assignment Operators
Assignment operators bind values to variables or update existing variables using arithmetic logic.
| Operator | Description |
|---|---|
| = | Basic assignment |
| += | Add and assign |
| -= | Subtract and assign |
| *= | Multiply and assign |
| /= | Divide and assign |
| %= | Modulo and assign |
| //= | Floor divide and assign |
| **= | Exponentiate and assign |
Basic Assignment (=)
Variables act as labels for data objects. This operator assigns a value to a name.
val_x = 5
val_y = 10
val_sum = val_x + val_y
val_x holds 5, val_y holds 10, and val_sum receives the result 15.
Python allows simultaneous assignment and swapping without temporary variables.
val_x = 5
val_y = 10
print(val_x) # 5
print(val_y) # 10
val_x, val_y = val_y, val_x
print(val_x) # 10
print(val_y) # 5
Multiple variables can also be initialized in a single line.
val_x, val_y, val_z = 5, 10, 15
Augmented Assignment (+=)
This operator performs addition and updates the variable in place. The variable must exist prior to usage.
counter = 5
counter += 10 # Equivalent to counter = counter + 10
print(counter) # Output: 15
Attempting to use this on an undefined variable raises a NameError.
Other Augmented Operators
The logic applies similar to subtraction, multiplication, and division.
counter = 5
counter -= 2 # Result: 3
print(counter)
counter = 5
counter *= 3 # Result: 15
print(counter)
counter = 10
counter /= 2 # Result: 5.0 (float)
print(counter)
counter = 7
counter %= 3 # Result: 1
print(counter)
counter = 7
counter //= 3 # Result: 2
print(counter)
counter = 2
counter **= 3 # Result: 8
print(counter)
Bitwise Operators
Bitwise operators manipulate individual bits of integer binary representations. These are less common in high-level logic but essential for low-level optimization.
| Operator | Description |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise NOT |
| << | Left Shift |
| >> | Right Shift |
Bitwise AND (&)
Compares bits; returns 1 only if both bits are 1.
mask_a = 10 # Binary: 1010
mask_b = 6 # Binary: 0110
print(mask_a & mask_b) # Output: 2 (Binary: 0010)
Bitwise OR (|)
Returns 1 if at least one of the corresponding bits is 1.
mask_a = 10 # Binary: 1010
mask_b = 6 # Binary: 0110
print(mask_a | mask_b) # Output: 14 (Binary: 1110)
Bitwise XOR (^)
Returns 1 if the corresponding bits are different.
mask_a = 10 # Binary: 1010
mask_b = 6 # Binary: 0110
print(mask_a ^ mask_b) # Output: 12 (Binary: 1100)
Bitwise NOT (~)
Inverts all bits. In Python, this follows the formula ~n = -(n + 1).
val = 8
print(~val) # Output: -9
Shift Operators (<<, >>)
Shifts bits left or right. Left shift multiplies by powers of 2; right shift divides.
val = 3
print(val << 2) # Output: 12 (3 * 4)
val = 3
print(val >> 2) # Output: 0
Identity Operators
Identity operators check if two variables point to the exact same object in memory, rather than just having equal values.
| Operator | Description |
|---|---|
| is | True if operands refer to the same object |
| is not | True if operands refer to different objects |
Using 'is'
Python caches small integers, so identical values often share memory addresses.
ref_p = 100
ref_q = 100
print(id(ref_p)) # Memory address
print(id(ref_q)) # Same memory address
print(ref_p is ref_q) # Output: True
Different values reside at different addresses.
ref_p = 100
ref_q = 200
print(ref_p is ref_q) # Output: False
Using 'is not'
Validates that two references do not point to the same object.
ref_p = 100
ref_q = 200
print(ref_p is not ref_q) # Output: True
For immutable types like integers, == checks value equality, while is checks memory identity. While small integers may pass both checks due to caching, relying on is for value comparison is not recommended practice.