OPERATORS IN PYTHON
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction,
multiplication, and division. They work with both integers and floating-point numbers.
Operator Description Example Output
+ Addition 5+3 8
- Subtraction 10 - 4 6
* Multiplication 6*2 12
/ Division 8/2 4.0
% Modulus (Remainder) 10 % 3 1
// Floor Division 10 // 3 3
** Exponentiation 2 ** 3 8
2. Comparison (Relational) Operators
Comparison operators are used to compare two values and return True or False depending on
whether the condition is satisfied.
Operator Description Example Output
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 7>4 True
< Less than 3<8 True
>= Greater than or equal to 5 >= 5 True
<= Less than or equal to 4 <= 6 True
3. Logical Operators
Logical operators are used to combine conditional statements. They return True or False based on
logical relationships between expressions.
Operator Description Example Output
and True if both conditions are True (5 > 2) and (6 > 3) True
or True if at least one condition is True (5 < 2) or (6 > 3) True
not Reverses the result not(5 > 2) False
4. Assignment Operators
Assignment operators are used to assign values to variables. They can also be combined with
arithmetic operators to perform operations and assignment in one step.
Operator Description Example Equivalent To
= Assign value x = 10 x = 10
+= Add and assign x += 5 x=x+5
-= Subtract and assign x -= 3 x=x-3
*= Multiply and assign x *= 2 x=x*2
/= Divide and assign x /= 4 x=x/4
%= Modulus and assign x %= 2 x=x%2
**= Exponent and assign x **= 3 x = x ** 3
5. Bitwise Operators
Bitwise operators perform operations on binary numbers (bits). They are mainly used in low-level
programming and optimization tasks.
Operator Description Example Output
& Bitwise AND 5&3 1
| Bitwise OR 5|3 7
^ Bitwise XOR 5^3 6
~ Bitwise NOT ~5 -6
<< Left Shift 5 << 1 10
>> Right Shift 5 >> 1 2
6. Identity Operators
Identity operators are used to compare the memory location of two objects. They check whether
two variables refer to the same object in memory.
Operator Description Example Output
is True if both variables refer to same object x is y True / False
is not True if both refer to different objects x is not y True / False
7. Membership Operators
Membership operators are used to test whether a value is present in a sequence like a list, tuple, or
string. They return True if the value exists, otherwise False.
Operator Description Example Output
in Returns True if value found in sequence 'a' in 'apple' True
not in Returns True if value not found in sequence 'z' not in 'apple' True