Operators in Python
An operator is a symbol or keyword used to perform an operation on values or variables.
Example:
a = 10 b = 5 print(a + b)
Here, + is an operator.
Python operators are mainly divided into 7 types:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 5 | 2.0 |
% | Modulus | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
// | Floor Division | 10 // 3 | 3 |
Example
a = 10 b = 3 print(a + b) print(a - b) print(a * b) print(a / b) print(a % b) print(a ** b) print(a // b)
Output:
13 7 30 3.3333333333333335 1 1000 3
Modulus %
Returns the remainder:
print(10 % 3)
Output:
1
Floor Division //
Returns the quotient without the decimal part:
print(10 // 3)
Output:
3
2. Assignment Operators
Assignment operators are used to assign or update values in variables.
| Operator | Example | Meaning |
|---|---|---|
= | x = 10 | Assign |
+= | 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 |
&= | x &= 5 | Bitwise AND |
= | `x | = 5` |
^= | x ^= 5 | Bitwise XOR |
>>= | x >>= 5 | Right shift |
<<= | x <<= 5 | Left shift |
Example
x = 10 x += 5 print(x)
Output:
15
This:
x += 5
is equivalent to:
x = x + 5
3. Comparison Operators
Comparison operators are used to compare two values. The result is always:
True
or:
False
| Operator | Name | Example |
|---|---|---|
== | Equal to | 10 == 10 |
!= | Not equal to | 10 != 5 |
> | Greater than | 10 > 5 |
< | Less than | 10 < 5 |
>= | Greater than or equal to | 10 >= 10 |
<= | Less than or equal to | 5 <= 10 |
Example
a = 10 b = 5 print(a == b) print(a != b) print(a > b) print(a < b) print(a >= b) print(a <= b)
Output:
False True True False True False
Important: = vs ==
= means assignment:
x = 10
== means comparison:
x == 10
4. Logical Operators
Logical operators are used to combine or modify conditions.
Python has three logical operators:
| Operator | Meaning |
|---|---|
and | True if both conditions are true |
or | True if at least one condition is true |
not | Reverses the result |
and
Both conditions must be true.
age = 20 print(age >= 18 and age <= 60)
Output:
True
or
At least one condition must be true.
age = 15 print(age < 18 or age > 60)
Output:
True
not
Reverses the result.
is_student = True print(not is_student)
Output:
False
5. Identity Operators
Identity operators check whether two variables refer to the same object in memory.
There are two identity operators:
| Operator | Meaning |
|---|---|
is | Same object |
is not | Not the same object |
Example:
a = [1, 2, 3] b = a print(a is b)
Output:
True
Because b refers to the same list object as a.
is not
a = [1, 2, 3] b = [1, 2, 3] print(a is not b)
Output:
True
The lists contain the same values, but they are separate objects.
== vs is
This is important:
a == b
checks whether the values are equal.
a is b
checks whether they are the same object.
6. Membership Operators
Membership operators check whether a value exists inside a sequence or collection.
There are two:
| Operator | Meaning |
|---|---|
in | Value exists |
not in | Value does not exist |
in
fruits = ["apple", "banana", "mango"] print("apple" in fruits)
Output:
True
not in
fruits = ["apple", "banana", "mango"] print("orange" not in fruits)
Output:
True
Membership operators can be used with strings too:
text = "Python" print("Py" in text)
Output:
True
7. Bitwise Operators
Bitwise operators work with numbers at the binary/bit level.
| Operator | Name | |
|---|---|---|
& | AND | |
| ` | ` | OR |
^ | XOR | |
~ | NOT | |
<< | Left Shift | |
>> | Right Shift |
Bitwise AND &
a = 5 b = 3 print(a & b)
Binary:
5 = 101 3 = 011 --------- 001
Result:
1
Bitwise OR |
print(5 | 3)
Result:
7
Bitwise XOR ^
print(5 ^ 3)
Result:
6
Bitwise NOT ~
print(~5)
Result:
-6
Left Shift <<
print(5 << 1)
Result:
10
Right Shift >>
print(5 >> 1)
Result:
2
Bitwise operators are especially useful in areas such as low-level programming, networking, embedded systems, and cybersecurity.
Operator Precedence
When an expression contains multiple operators, Python follows a specific order.
For example:
result = 10 + 5 * 2
Python performs multiplication first:
5 * 2 = 10 10 + 10 = 20
So:
print(result)
Output:
20
Using parentheses changes the order:
result = (10 + 5) * 2 print(result)
Output:
30
Basic precedence order
From higher to lower:
() ** +x, -x, ~x *, /, //, % +, - <<, >> & ^ | <, <=, >, >=, ==, != not and or
Complete Summary
| Type | Operators | |
|---|---|---|
| Arithmetic | + - * / % ** // | |
| Assignment | `= += -= *= /= %= //= **= &= | = ^= >>= <<=` |
| Comparison | == != > < >= <= | |
| Logical | and or not | |
| Identity | is, is not | |
| Membership | in, not in | |
| Bitwise | & | ^ ~ << >> |
Simple way to remember
Arithmetic → Calculate Assignment → Assign/Update Comparison → Compare Logical → Combine conditions Identity → Check same object Membership → Check presence Bitwise → Work with binary bits