Operators are keywords or symbols used to perform operations on values or variables. Your file (operators.py) covers 7 types — let's go through each one.
Used for mathematical calculations on numbers.
| Symbol | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 4 * 3 | 12 |
/ | Division | 7 / 2 | 3.5 (float) |
// | Floor Division | 7 // 2 | 3 (int, rounds down) |
% | Modulus | 7 % 2 | 1 (remainder) |
** | Exponent | 2 ** 3 | 8 (2³) |
/ always returns a float. // returns an int (floor). So 7/2 = 3.5 but 7//2 = 3.
print(10 + 3) # 13 print(10 - 3) # 7 print(10 * 3) # 30 print(10 / 3) # 3.3333... (float) print(10 // 3) # 3 (floor division) print(10 % 3) # 1 (remainder) print(2 ** 10) # 1024
Used to assign values to variables. Shorthand operators combine assignment with an arithmetic operation.
| Operator | Name | Equivalent to | Example |
|---|---|---|---|
= | Assign | — | x = 10 |
+= | Add & assign | x = x + 5 | x += 5 |
-= | Subtract & assign | x = x - 3 | x -= 3 |
*= | Multiply & assign | x = x * 2 | x *= 2 |
/= | Divide & assign | x = x / 2 | x /= 2 |
//= | Floor divide & assign | x = x // 2 | x //= 2 |
%= | Modulus & assign | x = x % 3 | x %= 3 |
**= | Exponent & assign | x = x ** 2 | x **= 2 |
x = 10 x += 5 # x is now 15 x *= 2 # x is now 30 x -= 10 # x is now 20 print(x) # 20
Compare two values and return a Boolean (True or False).
| Operator | Name | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 7 > 5 | True |
< | Less than | 3 < 5 | True |
>= | Greater than or equal | 5 >= 5 | True |
<= | Less than or equal | 4 <= 5 | True |
= is assignment (stores a value). == is comparison (checks equality). Writing if x = 5: is a syntax error in Python.
Combine multiple Boolean conditions. Returns a Boolean value.
| Operator | Meaning | Returns True when… |
|---|---|---|
and | Logical AND | Both conditions are True |
or | Logical OR | At least one condition is True |
not | Logical NOT | Condition is False (reverses it) |
age = 20 has_id = True print(age >= 18 and has_id) # True — both true print(age < 18 or has_id) # True — one is true print(not has_id) # False — reverses True
Operate on individual bits of integer values. Used in low-level programming.
| Operator | Name | Rule | Example (a=5, b=3) |
|---|---|---|---|
& | Bitwise AND | 1 if both bits are 1 | 5 & 3 = 1 |
| | Bitwise OR | 0 if both bits are 0 | 5 | 3 = 7 |
^ | Bitwise XOR | 1 if bits differ | 5 ^ 3 = 6 |
~ | Bitwise NOT | ~a = -(a+1) | ~5 = -6 |
<< | Left shift | Shifts bits left (×2 per shift) | 5 << 1 = 10 |
>> | Right shift | Shifts bits right (÷2 per shift) | 5 >> 1 = 2 |
# 5 in binary: 101 | 3 in binary: 011 print(5 & 3) # 001 → 1 print(5 | 3) # 111 → 7 print(5 ^ 3) # 110 → 6 print(~5) # -(5+1) → -6 print(5 << 1) # 1010 → 10 print(5 >> 1) # 10 → 2
Check if a value exists in a sequence (string, list, etc.). Returns a Boolean.
| Operator | Returns True when… | Example |
|---|---|---|
in | Value IS in the sequence | "a" in "apple" → True |
not in | Value is NOT in the sequence | "z" not in "apple" → True |
word = "Python" print("P" in word) # True print("z" in word) # False print("x" not in word) # True
Check if two variables refer to the same object in memory. Returns a Boolean.
| Operator | Returns True when… |
|---|---|
is | Both variables point to the same memory object |
is not | They point to different memory objects |
a = None print(a is None) # True — 'is' is the correct way to check None x = [1, 2] y = [1, 2] print(x == y) # True — same values print(x is y) # False — different objects in memory
| Comparison | Difference |
|---|---|
= vs == | = assigns a value; == compares two values |
== vs is | == compares values; is compares memory identity |
== vs in | == checks exact equality; in checks membership in a collection |
in vs is | in checks membership; is checks memory identity |
a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) print("Addition:", a + b) print("Subtraction:", a - b) print("Multiplication:", a * b) print("Division:", a / b) print("Floor Division:", a // b) print("Modulus:", a % b) print("Exponent:", a ** b)
score = 100. Then: add 50, multiply by 2, subtract 30, floor-divide by 5.
Print the final value. Use only shorthand assignment operators.
score = 100 score += 50 # 150 score *= 2 # 300 score -= 30 # 270 score //= 5 # 54 print(score) # 54
True if even, False if odd.
number % 2 == 0.number = int(input("Enter a number: ")) print(number % 2 == 0) # True if even, False if odd
in operator with the string "aeiouAEIOU". Print the result.
letter = input("Enter a letter: ") print(letter in "aeiouAEIOU") # True if vowel
print(10 == 10)
print(10 is 10)
print("py" in "python")
print(5 != 5)
print(not True)
print(10 > 5 and 3 < 1)
print(10 == 10) # True print(10 is 10) # True (small ints are cached) print("py" in "python") # True print(5 != 5) # False print(not True) # False print(10 > 5 and 3 < 1) # False (3<1 is False)
7 // 2 return?10 % 3?x = 5; x += 3, what is x?10 > 5 and 3 < 1 evaluate to?a = [1,2] and b = [1,2]. Which statement is correct?"o" in "Python" return?~5?