Operators are special symbols or keywords used to perform operations on values and variables. Python provides different types of operators for mathematical calculations, comparisons, logical operations, assignment, membership, identity, and bitwise operations.
An operator performs an operation on one or more values. For example:
a = 10
b = 5
print(a + b)
Here, + is an arithmetic operator.
Python provides the following major types of operators:
Arithmetic operators are used to perform mathematical calculations.
| Operator | Name | Example |
|---|---|---|
| + | Addition | 10 + 5 |
| - | Subtraction | 10 - 5 |
| * | Multiplication | 10 * 5 |
| / | Division | 10 / 5 |
| % | Modulus | 10 % 3 |
| ** | Exponentiation | 2 ** 3 |
| // | Floor Division | 10 // 3 |
The + operator is used for addition.
a = 10
b = 20
print(a + b)
The + operator can also join strings.
first = "Hello"
second = "World"
print(first + " " + second)
a = 50
b = 20
print(a - b)
a = 10
b = 5
print(a * b)
The multiplication operator can also repeat strings.
print("Hi " * 3)
The / operator performs division and returns a floating-point result.
a = 10
b = 2
print(a / b)
The % operator returns the remainder after division.
print(10 % 3)
Modulus is commonly used to check whether a number is even or odd.
number = 10
print(number % 2)
The ** operator is used to raise a number to a power.
print(2 ** 3)
print(5 ** 2)
The // operator performs floor division. It returns the floor of the division result.
print(10 // 3)
Unlike normal division, floor division does not return the fractional part.
Assignment operators are used to assign values to 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 = 10
print(x)
x = 10
x += 5
print(x)
This is equivalent to:
x = x + 5
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)
Logical operators are used to combine multiple conditions.
| Operator | Description |
|---|---|
| and | True if both conditions are True |
| or | True if at least one condition is True |
| not | Reverses the result |
age = 25
print(age > 18 and age < 30)
Both conditions are True, so the result is True.
age = 25
print(age < 18 or age > 20)
At least one condition is True, so the result is True.
age = 25
print(not age > 18)
The not operator reverses the Boolean result.
Identity operators are used to check whether two variables refer to the same object.
x = None
print(x is None)
print(x is not None)
Membership operators are used to check whether a value exists in a sequence or collection.
fruits = ["Apple", "Banana", "Mango"]
print("Apple" in fruits)
print("Orange" in fruits)
fruits = ["Apple", "Banana", "Mango"]
print("Orange" not in fruits)
print("Apple" not in fruits)
Bitwise operators work with the binary representation of integers.
| Operator | Name |
|---|---|
| & | AND |
| | | OR |
| ^ | XOR |
| ~ | NOT |
| << | Left Shift |
| >> | Right Shift |
a = 5
b = 3
print(a & b)
Bitwise operators are commonly used in advanced programming and low-level operations.
When an expression contains multiple operators, Python follows operator precedence to determine the order of evaluation.
result = 10 + 5 * 2
print(result)
Multiplication is performed before addition.
Parentheses can be used when you want to control the order.
result = (10 + 5) * 2
print(result)
Question: Which operator is used to find the remainder after division?