Python Operators
Operators are special symbols or keywords used to perform operations on values and variables. Python provides different types of operators for arithmetic calculations, comparisons, logical operations, assignments and other programming tasks.
Types of Python Operators
| Operator Type | Purpose |
|---|---|
| Arithmetic Operators | Perform mathematical calculations |
| Assignment Operators | Assign and update values |
| Comparison Operators | Compare two values |
| Logical Operators | Combine or reverse conditions |
| Bitwise Operators | Perform operations on individual bits |
| Membership Operators | Check whether a value exists in a sequence |
| Identity Operators | Check whether two references refer to the same object |
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Name | Example |
|---|---|---|
+ |
Addition | 10 + 3 |
- |
Subtraction | 10 - 3 |
* |
Multiplication | 10 * 3 |
/ |
Division | 10 / 3 |
// |
Floor Division | 10 // 3 |
% |
Modulus | 10 % 3 |
** |
Exponentiation | 10 ** 3 |
Addition
a = 10
b = 5
print(a + b)
15
Subtraction
a = 10
b = 5
print(a - b)
5
Multiplication
a = 10
b = 5
print(a * b)
50
Division
The / operator performs normal division and returns
a floating-point result.
a = 10
b = 3
print(a / b)
3.3333333333333335
Floor Division
The // operator performs floor division. It returns
the floor of the division result.
a = 10
b = 3
print(a // b)
3
Modulus
The % operator returns the remainder of a division.
a = 10
b = 3
print(a % b)
1
Exponentiation
The ** operator is used to calculate powers.
a = 2
b = 5
print(a ** b)
32
2. Assignment Operators
Assignment operators are used to assign values to variables. Python also provides compound assignment operators that perform an operation and assignment in a single statement.
| Operator | Example | Equivalent Operation |
|---|---|---|
= |
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 **= 2 |
x = x ** 2 |
x = 10
x += 5
print(x)
x *= 2
print(x)
x -= 4
print(x)
15 30 26
3. Comparison Operators
Comparison operators compare two values and return either
True or False.
| Operator | Meaning |
|---|---|
== |
Equal to |
!= |
Not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
a = 10
b = 20
print(a == b)
print(a != b)
print(a < b)
print(a > b)
print(a <= b)
print(a >= b)
False True True False True False
4. Logical Operators
Logical operators are used to combine conditional expressions.
| Operator | Description |
|---|---|
and |
Returns true when both conditions are true. |
or |
Returns true when at least one condition is true. |
not |
Reverses the Boolean result. |
and Operator
age = 25
print(age >= 18 and age <= 60)
True
or Operator
marks = 85
print(marks >= 90 or marks >= 80)
True
not Operator
x = True
print(not x)
False
5. Bitwise Operators
Bitwise operators perform operations on the binary representation of integer values.
| Operator | Name |
|---|---|
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
~ |
Bitwise NOT |
<< |
Left Shift |
>> |
Right Shift |
Bitwise AND
a = 5
b = 3
print(a & b)
1
Bitwise OR
a = 5
b = 3
print(a | b)
7
Bitwise XOR
a = 5
b = 3
print(a ^ b)
6
Bitwise NOT
a = 5
print(~a)
-6
Left Shift
a = 5
print(a << 1)
10
Right Shift
a = 10
print(a >> 1)
5
6. Membership Operators
Membership operators are used to check whether a value exists in a sequence such as a string, list, tuple, set or dictionary.
| Operator | Meaning |
|---|---|
in |
Returns true if the value is present. |
not in |
Returns true if the value is not present. |
numbers = [10, 20, 30, 40]
print(20 in numbers)
print(50 in numbers)
print(50 not in numbers)
True False True
Membership Operator with String
text = "Python Programming"
print("Python" in text)
print("Java" in text)
True False
7. Identity Operators
Identity operators are used to determine whether two variables refer to the same object in memory.
| Operator | Meaning |
|---|---|
is |
Returns true if two references refer to the same object. |
is not |
Returns true if two references do not refer to the same object. |
a = [10, 20]
b = a
c = [10, 20]
print(a is b)
print(a is c)
print(a == c)
True False True
Here, a and b refer to the same list object,
so a is b is True. The lists referenced by
a and c contain the same values, so
a == c is True, but they are different
objects, so a is c is False.
8. Operator Precedence
When an expression contains more than one operator, Python follows a specific order called operator precedence.
For example, multiplication is performed before addition.
result = 10 + 5 * 2
print(result)
20
The multiplication 5 * 2 is performed first, followed
by the addition.
Using Parentheses
Parentheses can be used to change the normal order of evaluation.
result = (10 + 5) * 2
print(result)
30
Basic Precedence Order
| Priority | Operators |
|---|---|
| 1 | () |
| 2 | ** |
| 3 | +x, -x, ~x |
| 4 | *, /, //, % |
| 5 | +, - |
| 6 | Comparison operators |
| 7 | not |
| 8 | and |
| 9 | or |
9. Example Using Multiple Operators
a = 10
b = 5
c = 2
result = a + b * c
print(result)
20
First, b * c is evaluated, giving 10.
Then a + 10 is evaluated, giving 20.
Important Points
- Operators are used to perform operations on values and variables.
- Arithmetic operators perform mathematical calculations.
- Assignment operators assign and update values.
- Comparison operators return
TrueorFalse. - Logical operators combine Boolean conditions.
- Bitwise operators work on the binary representation of integers.
- Membership operators are
inandnot in. - Identity operators are
isandis not. - Operator precedence determines the order in which an expression is evaluated.
- Parentheses can be used to explicitly control the order of evaluation.