Lesson 15 of 70 – Python Operators
21%

Python Operators

Operators are special symbols or keywords used to perform operations on values and variables. Python provides different types of operators for mathematical calculations, comparisons, logical operations, assignment, membership, identity, and bitwise operations.

Note: Operators are commonly used with variables and expressions to perform calculations and make decisions in Python programs.
What are Operators?

An operator performs an operation on one or more values. For example:

a = 10
b = 5

print(a + b)
Output:
15

Here, + is an arithmetic operator.

Types of Python Operators

Python provides the following major types of operators:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Identity Operators
  • Membership Operators
  • Bitwise Operators
Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Operator Name Example
+ Addition 10 + 5
- Subtraction 10 - 5
* Multiplication 10 * 5
/ Division 10 / 5
% Modulus 10 % 3
** Exponentiation 2 ** 3
// Floor Division 10 // 3
Addition Operator +

The + operator is used for addition.

a = 10
b = 20

print(a + b)
Output:
30

The + operator can also join strings.

first = "Hello"
second = "World"

print(first + " " + second)
Output:
Hello World
Subtraction Operator -
a = 50
b = 20

print(a - b)
Output:
30
Multiplication Operator *
a = 10
b = 5

print(a * b)
Output:
50

The multiplication operator can also repeat strings.

print("Hi " * 3)
Output:
Hi Hi Hi
Division Operator /

The / operator performs division and returns a floating-point result.

a = 10
b = 2

print(a / b)
Output:
5.0
Modulus Operator %

The % operator returns the remainder after division.

print(10 % 3)
Output:
1

Modulus is commonly used to check whether a number is even or odd.

number = 10

print(number % 2)
Output:
0
Exponentiation Operator **

The ** operator is used to raise a number to a power.

print(2 ** 3)
print(5 ** 2)
Output:
8
25
Floor Division Operator //

The // operator performs floor division. It returns the floor of the division result.

print(10 // 3)
Output:
3

Unlike normal division, floor division does not return the fractional part.

Assignment Operators

Assignment operators are used to assign values to variables.

Operator Example Meaning
= x = 10 Assign
+= x += 5 x = x + 5
-= x -= 5 x = x - 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
Assignment Operator =
x = 10

print(x)
Output:
10
Addition Assignment +=
x = 10

x += 5

print(x)
Output:
15

This is equivalent to:

x = x + 5
Comparison Operators

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

Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Comparison Examples
a = 10
b = 20

print(a == b)
print(a != b)
print(a < b)
print(a > b)
Output:
False
True
True
False
Logical Operators

Logical operators are used to combine multiple conditions.

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

print(age > 18 and age < 30)
Output:
True

Both conditions are True, so the result is True.

or Operator
age = 25

print(age < 18 or age > 20)
Output:
True

At least one condition is True, so the result is True.

not Operator
age = 25

print(not age > 18)
Output:
False

The not operator reverses the Boolean result.

Identity Operators

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

  • is
  • is not
x = None

print(x is None)
print(x is not None)
Output:
True
False
Membership Operators

Membership operators are used to check whether a value exists in a sequence or collection.

  • in
  • not in
fruits = ["Apple", "Banana", "Mango"]

print("Apple" in fruits)
print("Orange" in fruits)
Output:
True
False
not in Operator
fruits = ["Apple", "Banana", "Mango"]

print("Orange" not in fruits)
print("Apple" not in fruits)
Output:
True
False
Bitwise Operators

Bitwise operators work with the binary representation of integers.

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

print(a & b)
Output:
1

Bitwise operators are commonly used in advanced programming and low-level operations.

Operator Precedence

When an expression contains multiple operators, Python follows operator precedence to determine the order of evaluation.

result = 10 + 5 * 2

print(result)
Output:
20

Multiplication is performed before addition.

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

result = (10 + 5) * 2

print(result)
Output:
30
Key Points
  • Operators are used to perform operations on values and variables.
  • Arithmetic operators perform mathematical calculations.
  • Assignment operators assign and update variable values.
  • Comparison operators return True or False.
  • Logical operators combine conditions.
  • Identity operators are is and is not.
  • Membership operators are in and not in.
  • Bitwise operators work with integer binary representations.
  • Parentheses can be used to control the order of operations.

🧠 Quick Quiz

Question: Which operator is used to find the remainder after division?