After completing this chapter, you will be able to:
- Understand Operators in Python.
- Explain why operators are used.
- Learn different types of operators.
- Perform arithmetic and logical operations.
- Write programs using operators.
- Understand operator precedence.
In Python, operators are special symbols used to perform operations on variables and values.
For example,
- Add two numbers.
- Compare two values.
- Check multiple conditions.
- Assign values to variables.
- Perform bit-level operations.
Without operators, performing calculations and making decisions in programs would not be possible.
An Operator is a symbol that performs a specific operation on one or more operands and produces a result.
Example
a = 10
b = 20
print(a + b)Output
30
Here,
+is the operator.10and20are operands.
Operators are used to:
- Perform mathematical calculations.
- Compare values.
- Assign values.
- Combine multiple conditions.
- Manipulate binary values.
- Check object identity.
- Check membership.
Python provides the following types of operators.
Operators
│
├── Arithmetic Operators
├── Assignment Operators
├── Comparison (Relational) Operators
├── Logical Operators
├── Bitwise Operators
├── Membership Operators
└── Identity Operators
Arithmetic operators perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | 10 + 5 |
| - | Subtraction | 10 - 5 |
| * | Multiplication | 10 * 5 |
| / | Division | 10 / 5 |
| // | Floor Division | 10 // 3 |
| % | Modulus | 10 % 3 |
| ** | Exponent | 2 ** 3 |
a = 20
b = 10
print("Addition :", a + b)
print("Subtraction :", a - b)
print("Multiplication :", a * b)
print("Division :", a / b)
print("Floor Division :", a // b)
print("Modulus :", a % b)
print("Power :", a ** 2)Addition : 30
Subtraction : 10
Multiplication : 200
Division : 2.0
Floor Division : 2
Modulus : 0
Power : 400
Assignment operators assign values to variables.
| Operator | Example |
|---|---|
| = | x = 10 |
| += | x += 5 |
| -= | x -= 5 |
| *= | x *= 5 |
| /= | x /= 5 |
| //= | x //= 5 |
| %= | x %= 5 |
| **= | x **= 2 |
x = 10
x += 5
print(x)Output
15
Comparison operators compare two values and return either True or False.
| Operator | Description |
|---|---|
| == | Equal to |
| != | Not Equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or Equal |
| <= | Less than or Equal |
a = 10
b = 20
print(a == b)
print(a != b)
print(a < b)
print(a > b)False
True
True
False
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 |
age = 22
citizen = True
print(age >= 18 and citizen)True
Bitwise operators perform operations at the binary level.
| Operator | Description |
|---|---|
| & | AND |
| | | OR |
| ^ | XOR |
| ~ | NOT |
| << | Left Shift |
| >> | Right Shift |
print(5 & 3)
print(5 | 3)1
7
Membership operators check whether a value exists in a sequence.
| Operator | Description |
|---|---|
| in | Returns True if value exists |
| not in | Returns True if value does not exist |
language = "Python"
print("P" in language)
print("Z" in language)True
False
Identity operators compare whether two variables refer to the same object.
| Operator | Description |
|---|---|
| is | Same object |
| is not | Different object |
a = [1, 2]
b = a
print(a is b)
print(a is not b)True
False
Python follows precedence rules while evaluating expressions.
Highest to Lowest
()
**
*, /, //, %
+, -
<, >, <=, >=
==, !=
not
and
or
Operators are used in:
- Banking Systems
- Billing Software
- Payroll Systems
- Student Management Systems
- Inventory Systems
- E-Commerce Applications
- Simplifies calculations.
- Supports decision making.
- Reduces code complexity.
- Improves program readability.
An operator is a symbol that performs an operation on one or more operands.
Seven major types:
- Arithmetic
- Assignment
- Comparison
- Logical
- Bitwise
- Membership
- Identity
==
=is an assignment operator.==is a comparison operator.
% (Modulus Operator)
- Operators perform operations on operands.
- Python provides seven major categories of operators.
- Arithmetic operators perform calculations.
- Logical operators combine conditions.
- Membership operators work with sequences.
- Identity operators compare object references.
In this chapter, you learned:
- What operators are.
- Types of operators.
- Arithmetic operators.
- Assignment operators.
- Comparison operators.
- Logical operators.
- Bitwise operators.
- Membership operators.
- Identity operators.
- Operator precedence.
- Practical examples.
- Interview questions.