Master Python Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Operators

Operators are special symbols and keywords used to perform operations on values and variables. Python provides operators for arithmetic, comparison, logical operations, assignment, membership, identity, and bitwise operations.

CIIT Training Institute : Operators are used in almost every Python program, from simple calculations to conditions, loops, data processing, and application logic.

What is an Operator?

An operator performs an operation on one or more values. The values on which an operator works are called operands.

a = 10
b = 5

result = a + b

print(result)

Here, + is the operator and a and b are the operands.

Operator Concept

Operand
10
↓
Operator
+
↓
Operand
5
↓
Result
15

Types of Python Operators

Operator Type Common Operators Purpose
Arithmetic +, -, *, /, //, %, ** Mathematical calculations
Comparison ==, !=, >, <, >=, <= Compare values
Logical and, or, not Combine conditions
Assignment =, +=, -=, *=, /= Assign and update values
Membership in, not in Check whether a value exists
Identity is, is not Check object identity
Bitwise &, |, ^, ~, <<, >> Perform bit-level operations

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Operator Name Example Result
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2.0
// Floor Division 11 // 5 2
% Modulus 11 % 5 1
** Exponent 2 ** 3 8
a = 10
b = 3

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)

Output:

Output
13
7
30
3.3333333333333335
3
1
1000

2. Comparison Operators

Comparison operators compare two values and return either True or False.

Operator Meaning Example
== Equal to 10 == 10
!= Not equal to 10 != 5
> Greater than 10 > 5
< Less than 5 < 10
>= Greater than or equal to 10 >= 10
<= Less than or equal to 5 <= 10
age = 25

print(age == 25)
print(age > 18)
print(age < 18)
print(age != 30)

Output

True
True
False
True

3. Logical Operators

Logical operators are used to combine multiple conditions.

Operator Description
and Returns True when both conditions are True
or Returns True when at least one condition is True
not Reverses the boolean result
age = 25
has_id = True

print(age >= 18 and has_id)
print(age < 18 or has_id)
print(not has_id)

Output:

Output
True
True
False

4. Assignment Operators

Assignment operators are used to assign values to variables and update existing values.

Operator Example Equivalent To
= x = 10 x = 10
+= x += 5 x = x + 5
-= x -= 5 x = x - 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
//= x //= 5 x = x // 5
%= x %= 5 x = x % 5
**= x **= 2 x = x ** 2
score = 100

score += 20

print(score)

score -= 10

print(score)

Output:

Output
120
110

5. Membership Operators

Membership operators check whether a value exists inside a sequence such as a string, list, tuple, or set.

courses = ["Python", "Java", "C#"]

print("Python" in courses)
print("PHP" in courses)
print("PHP" not in courses)

Output

True
False
True

6. Identity Operators

Identity operators are used to check whether two variables refer to the same object.

Python provides two identity operators: is and is not.

a = None
b = None

print(a is b)
print(a is not b)

Output:

Output
True
False

Identity operators are different from ==. The == operator compares values, while is checks object identity.

7. Bitwise Operators

Bitwise operators work with the binary representation of integer values.

Operator Name
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Left Shift
>> Right Shift
a = 5
b = 3

print(a & b)
print(a | b)
print(a ^ b)

Output:

Output
1
7
6

Operator Precedence

When an expression contains multiple operators, Python follows a specific order to evaluate them.

result = 10 + 5 * 2

print(result)

Output

20

Multiplication is performed before addition, so 5 * 2 is evaluated first.

Using Parentheses

Parentheses can be used when you want to explicitly control the order of evaluation.

result = (10 + 5) * 2

print(result)

Output

30

Example 🖥️🌍

Let's calculate whether a student has passed based on marks.

marks = 75
attendance = 85

passed = marks >= 40 and attendance >= 75

print("Student Passed:", passed)

Output

Student Passed: True
CIIT Learning Point

Operators are the building blocks of program logic. Practice arithmetic, comparison, logical, and assignment operators first. These operators will become especially important when you learn conditional statements and loops.

CIIT Practice Task

Create a Python program that accepts two numbers and displays their:

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Remainder
  • Comparison result
number1 = int(input("Enter first number: "))
number2 = int(input("Enter second number: "))

print("Addition:", number1 + number2)
print("Subtraction:", number1 - number2)
print("Multiplication:", number1 * number2)
print("Division:", number1 / number2)
print("Remainder:", number1 % number2)
print("First number is greater:", number1 > number2)

Output:

Output
Enter first number: 20
Enter second number: 10
Addition: 30
Subtraction: 10
Multiplication: 200
Division: 2.0
Remainder: 0
First number is greater: True

Summary :

Python operators are used to perform calculations, compare values, combine conditions, assign values, check membership and identity, and perform bit-level operations. Arithmetic, comparison, logical, and assignment operators are especially important for building real-world Python program logic.