Introduction
Operators are one of the most important concepts in Python programming. Operators are special symbols used to perform operations on variables, values, and expressions.
Whenever you perform calculations, compare values, assign data, or evaluate conditions in Python, operators are being used.
In Artificial Intelligence, Data Science, Machine Learning, and software development, operators are frequently used for data processing, calculations, decision-making, feature engineering, and algorithm implementation.
Learning Objectives
- Understand Python operators.
- Learn different categories of operators.
- Use arithmetic operators.
- Understand assignment operators.
- Learn comparison operators.
- Apply logical operators.
- Use membership, identity, and bitwise operators.
What are Operators in Python?
Operators are symbols that tell Python to perform specific operations.
These operations can include:
- Mathematical calculations
- Value comparison
- Conditional evaluation
- Data assignment
- Membership testing
Example:
x = 10
y = 5
print(x + y)
Output:
15
In this example:
- x and y are operands.
- + is the operator.
Categories of Python Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
1. Arithmetic Operators
Arithmetic operators perform mathematical operations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | 10 + 5 |
| – | Subtraction | 10 – 5 |
| * | Multiplication | 10 * 5 |
| / | Division | 10 / 5 |
| % | Modulus | 10 % 3 |
| ** | Exponent | 2 ** 4 |
| // | Floor Division | 10 // 3 |
Addition Operator (+)
print(15 + 5)
Output:
20
Subtraction Operator (-)
print(20 - 5)
Output:
15
Multiplication Operator (*)
print(6 * 4)
Output:
24
Division Operator (/)
print(20 / 4)
Output:
5.0
Modulus Operator (%)
Returns the remainder after division.
print(10 % 4)
Output:
2
Exponent Operator (**)
print(2 ** 3)
Output:
8
Floor Division Operator (//)
print(10 // 3)
Output:
3
2. Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Description |
|---|---|
| = | Assign Value |
| += | Add and Assign |
| -= | Subtract and Assign |
| *= | Multiply and Assign |
| /= | Divide and Assign |
Basic Assignment
x = 100
print(x)
Output:
100
Add and Assign Example
x = 10
x += 5
print(x)
Output:
15
3. Comparison Operators
Comparison operators compare 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 |
Example:
a = 10
b = 20
print(a == b)
print(a < b)
print(a > b)
Output:
False
True
False
4. Logical Operators
Logical operators combine conditions.
| Operator | Description |
|---|---|
| and | Both conditions must be True |
| or | One condition must be True |
| not | Reverses the result |
Example:
age = 25
print(age > 18 and age < 60)
Output:
True
5. Identity Operators
Identity operators compare memory locations of objects.
Operators:
- is
- is not
Example:
x = [1,2]
y = x
print(x is y)
Output:
True
6. Membership Operators
Membership operators check whether a value exists in a sequence.
Operators:
- in
- not in
Example:
courses = ["Python","AI","ML"]
print("Python" in courses)
Output:
True
7. Bitwise Operators
Bitwise operators work with binary numbers.
Common bitwise operators:
- &
- |
- ^
- ~
- <<
- >>
Example:
print(5 & 3)
Output:
1
Operator Precedence
Operator precedence determines which operation is executed first.
Example:
result = 10 + 5 * 2
print(result)
Output:
20
Multiplication happens before addition.
Real-World Examples
E-Commerce Order Calculation
price = 1500
quantity = 2
total = price * quantity
print(total)
Output:
3000
Login Validation Example
username = "admin"
print(username == "admin")
Output:
True
Python Example
x = 20
y = 4
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)
Output:
24
16
80
5.0
0
Interview Questions
1. What are operators in Python?
Operators are symbols used to perform operations on variables and values.
2. Which operator performs exponentiation?
The ** operator performs exponentiation.
3. What is the difference between "=" and "=="?
"=" assigns a value while "==" compares values.
4. Which operator checks membership?
The "in" operator checks membership.
Assignment
- Create two variables and perform all arithmetic operations.
- Use comparison operators with two numbers.
- Create a logical operator example.
- Create a list and test membership operators.
- Write a small shopping cart program using operators.
Quiz
Q1. Which operator is used for multiplication?
- A. +
- B. *
- C. %
- D. /
Answer: B. *
Q2. Which operator compares equality?
- A. =
- B. ==
- C. and
- D. in
Answer: B. ==
Summary
In this tutorial, you learned Python Operators and their importance in programming. You explored arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators.
Operators are essential for calculations, comparisons, decision making, and data processing in Artificial Intelligence and software development.
Next Tutorial
Tutorial 13: Lists and Tuples
```
