← Back to Index

PYTHON OPERATORS

Operators are special symbols that perform operations on values and variables.

1. Arithmetic Operators

Used to perform mathematical operations.

Operator Name Example Result
+ Addition 10 + 3 13
- Subtraction 10 - 3 7
* Multiplication 10 * 3 30
/ Division 10 / 3 3.333...
// Floor Division 10 // 3 3
% Modulus 10 % 3 1
** Exponentiation 10 ** 2 100

Example Usage

a = 10
b = 3
print(a + b)   # 13
print(a / b)   # 3.3333333333333335
print(a // b)  # 3
print(a % b)   # 1
print(a ** b)  # 1000

2. Assignment Operators

Used to assign values to variables.

Operator Example Same As
= x = 10 x = 10
+= x += 5 x = x + 5
-= x -= 5 x = x - 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
//= x //= 5 x = x // 5
%= x %= 5 x = x % 5
**= x **= 5 x = x ** 5

Example Usage

x = 10
x += 5   # x = 15
x -= 3   # x = 12
x *= 2   # x = 24
x //= 3  # x = 8
x **= 2  # x = 64

3. Comparison (Relational) Operators

Used to compare two values.

Operator Name Example Result
== Equal 5 == 5 True
!= Not Equal 5 != 3 True
> Greater Than 5 > 3 True
< Less Than 5 < 3 False
>= Greater Than or Equal 5 >= 5 True
<= Less Than or Equal 5 <= 3 False

Example Usage

a = 10
b = 20
print(a == b)  # False
print(a != b)  # True
print(a < b)   # True
print(a >= 10) # True

4. Logical Operators

Used to combine conditional statements.

Operator Name Example Result
and Returns True if both are True (5 > 3) and (2 < 4) True
or Returns True if at least one is True (5 > 3) or (2 > 4) True
not Reverses the result not(5 > 3) False

Example Usage

a = True
b = False
print(a and b) # False
print(a or b)  # True
print(not a)   # False

5. Bitwise Operators

Used to perform bit-level operations.

Operator Name Example (x = 5, y = 3) Result
& AND 5 & 3 (0101 & 0011) 1
| OR 5 | 3 (0101 | 0011) 7
^ XOR 5 ^ 3 (0101 ^ 0011) 6
~ NOT ~5 (~0101) -6
<< Left Shift 5 << 1 (0101 << 1) 10
>> Right Shift 5 >> 1 (0101 >> 1) 2

Example Usage

a = 5  # 0101
b = 3  # 0011
print(a & b)  # 1
print(a | b)  # 7
print(~a)     # -6
print(a << 1) # 10

6. Membership Operators

Used to test membership in a sequence.

Operator Name Example Result
in Returns True if value is found 'a' in 'apple' True
not in Returns True if value is not found 'z' not in 'apple' True

Example Usage

fruits = ['apple', 'banana', 'cherry']
print('banana' in fruits)   # True
print('orange' in fruits)   # False
print('grape' not in fruits)# True

7. Identity Operators

Used to compare the memory location of objects.

Operator Name Example Result
is Returns True if both variables point to the same object a is b True
is not Returns True if both variables point to different objects a is not b False

Example Usage

a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b)     # True
print(a is c)     # False
print(a is not c) # True

8. Ternary (Conditional) Operator

Used to evaluate a condition in a single line.

Syntax:
value_if_true if condition else value_if_false

Example Usage

age = 18
status = "Adult" if age >= 18 else "Minor"
print(status)  # Adult

Quick Reference Table

Arithmetic Assignment Comparison Logical Bitwise Membership Identity
+ - * / //
% **
= += -= *= /=
//= %= **=
== != > <
>= <=
and or not & | ^ ~
<< >>
in not in is is not

Key Takeaways

Operators make code powerful and concise.
Arithmetic for math operations.
Comparison & Logical for decision making.
Bitwise for low-level operations.
Membership & Identity for object/sequence checks.
Use the right operator to write efficient Python code.