Introduction
In Python, operators are special symbols or keywords that perform operations on variables and values. For example, the plus sign (+) adds two numbers, while the equal sign (=) assigns a value to a variable.
In this tutorial, you will learn what Python operators are, their syntax, different types, examples, real-life applications, common mistakes, interview questions, and best practices.
What are Python Operators?
Python operators are symbols or keywords used to perform operations on values and variables.
Example:
x = 10
y = 5
print(x + y)
Output:
In this example:
- + is an operator.
- x and y are operands.
- The operator performs addition.
Syntax:
The general syntax of an operator is:
operand1 operator operand2
Example:
10 + 5
Here:
- 10 is the first operand.
- + is the operator.
- 5 is the second operand.
Types of Python Operators
Python provides the following categories of operators:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
Let’s explore each type in detail.
1. Arithmetic Operators
Arithmetic operators perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | x + y |
| – | Subtraction | x – y |
| * | Multiplication | x * y |
| / | Division | x / y |
| % | Modulus | x % y |
| ** | Exponentiation | x ** y |
| // | Floor Division | x // y |
Addition Operator (+)
x = 10
y = 5
print(x + y)
Output:
Subtraction Operator (-)
print(10 - 5)
Output:
Multiplication Operator (*)
print(10 * 5)
Output:
Division Operator (/)
print(10 / 5)
Output:
Division always returns a float value.
Modulus Operator (%)
Returns the remainder.
print(10 % 3)
Output:
Exponentiation Operator (**)
Calculates powers.
print(2 ** 3)
Output:
Floor Division Operator (//)
Returns the integer part of division.
print(10 // 3)
Output:
2. Assignment Operators
Assignment operators assign values to variables.
| Operator | Example |
|---|---|
| = | x = 5 |
| += | x += 5 |
| -= | x -= 5 |
| *= | x *= 5 |
| /= | x /= 5 |
| %= | x %= 5 |
| **= | x **= 5 |
| //= | x //= 5 |
Simple Assignment (=)
x = 10
Addition Assignment (+=)
x = 10
x += 5
print(x)
Output:
Subtraction Assignment (-=)
x = 10
x -= 3
print(x)
Output:
3. Comparison Operators
Comparison operators compare two values and return True or False.
| Operator | Description |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Equal To (==)
print(10 == 10)
Output:
Not Equal To (!=)
print(10 != 5)
Output:
Greater Than (>)
print(20 > 10)
Output:
Less Than (<)
print(20 < 10)
4. Logical Operators
Logical operators combine multiple conditions.
| Operator | Description |
|---|---|
| and | Returns True if both conditions are True |
| or | Returns True if at least one condition is True |
| not | Reverses the result |
AND Operator
print(20 < 10)
Output:
OR Operator
print(10 > 5 or 5 > 10)
Output:
NOT Operator
print(not True)
Output:
5. Identity Operators
Identity operators check whether two variables refer to the same object.
| Operator | Description |
|---|---|
| is | Returns True if objects are identical |
| is not | Returns True if objects are not identical |
is Operator
x = [1, 2]
y = x
print(x is y)
Output:
is not Operator
x = [1, 2]
y = [1, 2]
print(x is not y)
Output:
6. Membership Operators
Membership operators check whether a value exists in a sequence.
| Operator | Description |
|---|---|
| in | Value exists |
| not in | Value does not exist |
in Operator
text = "Python"
print("P" in text)
Output:
not in Operator
text = "Python"
print("J" not in text)
Output:
7. Bitwise Operators
Bitwise operators work on binary values.
| Operator | Description |
|---|---|
| & | AND |
| | | OR |
| ^ | XOR |
| ~ | NOT |
| << | Left Shift |
| >> | Right Shift |
Bitwise AND (&)
print(5 & 3)
Output:
Bitwise OR (|)
print(5 | 3)
Output:
Left Shift (<<)
print(5 << 1)
Output:
Right Shift (>>)
print(5 >> 1)
Output:
Operator Precedence
Python follows precedence rules when evaluating expressions.
Example:
print(10 + 5 * 2)
Output:
Multiplication is performed before addition.
Using Parentheses
print((10 + 5) * 2)
Output:
Parentheses have the highest priority.
Real-life Examples:
1. Calculate the Total Price
Suppose an online shopping website calculates the total price.
price = 500
quantity = 3
total = price * quantity
print(total)
Output:
Here, the multiplication operator calculates the total amount.
2. Login Validation
username = "admin"
password = "1234"
is_valid = username == "admin" and password == "1234"
print(is_valid)
Output:
This example uses comparison and logical operators.
3. Voting Eligibility
age = 20
can_vote = age >= 18
print(can_vote)
Output:
Comparison operators are commonly used in such applications.
Common Mistakes
1. Using = Instead of ==
Incorrect:
if age = 18:
print("Adult")
Output:
Correct:
if age == 18:
print("Adult")
2. Dividing by Zero
Incorrect:
print(10 / 0)
Output:
3. Confusing is and ==
Incorrect:
x = [1, 2]
y = [1, 2]
print(x is y)
Output:
Use == to compare values.
print(x == y)
Output:
4. Forgetting Operator Precedence
print(10 + 5 * 2)
Output:
Some beginners incorrectly expect 30.
5. Misusing Logical Operators
Incorrect:
age = 20
print(age > 18 and age > 30)
Output:
Both conditions must be True when using and.
Conclusion
Python operators are essential tools that allow programmers to perform calculations, assign values, compare data, evaluate conditions, and manipulate information efficiently. Python provides a wide range of operators, including arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators, each designed for specific tasks.
Python Operators – Interview Questions
Q 1: What are operators in Python?
Q 2: Name some types of Python operators.
Q 3: Which operator is used for exponentiation?
Q 4: What does the == operator do?
Q 5: What is the use of logical operators?
Python Operators – Objective Questions (MCQs)
Q1. Which of the following is the assignment operator in Python?
Q2. What will be the output of the following code?
x = 10
y = 3
print(x // y)
Q3. Which operator is used to compare two values in Python?
Q4. What will be the output of the following code?
x = 5
x += 3
print(x)
Q5. Which of the following is a logical operator in Python?