What are Operators?
Operators are special symbols or keywords in Python that are used to perform operations on values or
variables. For example, +
is an operator used for addition, and ==
is used
to compare values.
Python supports different types of operators for various kinds of tasks like mathematical operations, comparisons, logic operations, and more.
Types of Operators in Python:
- Arithmetic Operators
- Assignment Operators
- Comparison (Relational) Operators
- Logical Operators
- Bitwise Operators
- Identity Operators
- Membership Operators
1. Arithmetic Operators
These are used to perform mathematical calculations like addition, subtraction, multiplication, etc.
Click the next page for knowing the types of arithmetic operators.
🧑‍💻 Example:
a = 10
b = 3
print("Sum of a and b = ",a + b)
print("Multiplies a and b = ",a * b)
print("Modular of a and b = ",a % b)
print("Exponentiation of a and b = ",a ** b)
Output:
Sum of a and b = 13
Multiplies a and b = 30
Modular of a and b = 1
Exponentiation of a and b = 1000
Explanation:
- a + b: 10 + 3 = 13 → Adds the two numbers.
- a * b: 10 × 3 = 30 → Multiplies both values.
- a % b: 10 divided by 3 gives remainder 1 → Modulus operator.
- a ** b: 10 raised to power 3 = 1000 → Exponentiation.
2. Assignment Operators
Assignment operators are used to assign values to variables. They can also combine assignment with other operations. Click the next page for knowing more about assignment operators.
= Assign value
a += 1 Same as a = a + 1
a -= 1 Same as a = a - 1
a *= 2 Same as a = a * 2
a /= 2 Same as a = a / 2
3. Comparison (Relational) Operators
Used to compare two values. The result is always True or False. Click the next page for knowing more about comparision operators.
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
4. Logical Operators
Used to combine multiple conditions. Returns True or False. Click the next page for knowing more about logical operators.
and Returns True if both conditions are True
or Returns True if at least one condition is True
not Reverses the result (True becomes False, False becomes True)
5. Bitwise Operators
These work on binary bits and perform bit-by-bit operations. Click the next page for knowing more about bitwise operators.
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Left Shift
>> Right Shift
6. Identity Operators
Used to check whether two variables refer to the same object in memory. And this operators are used to compare the memory locations of two objects.
Click the next page for knowing more about identity operators.
is Returns True if both variables point to the same object
is not Returns True if they do not point to the same object
7. Membership Operators
Membership operators are used to check if a value is present in a sequence
(like string, list, tuple, set,
or dictionary). They return True
if the value is found, otherwise
False
.
Click the next page for knowing more about membership operators.
in Returns True if value is found in the sequence
not in Returns True if value is not found in the sequence