Nearby lessons

34 of 108

Python - Operators

Introduction

An operator is a symbol that performs an operation on one or more operands.

Operators are used to perform calculations, comparisons, logical operations, assignments, and other tasks.

Example - Operator

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
30

Explanation

Component Description
+ Operator
a, b Operands

Types of Operators

Python supports the following types of operators:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Special Operators

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

Operator Meaning
+Addition
-Subtraction
*Multiplication
/Division
%Modulus
//Floor Division
**Exponent

Example - Addition Operator (+)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
30

Example - Subtraction Operator (-)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10

Example - Multiplication Operator (*)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
50

Example - Division Operator (/)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
5.0
3.3333333333333335

Important Note - Division

The division operator (/) always returns a floating-point value.

Example - Floor Division Operator (//)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
5
3
5.0

Explanation

The floor division operator (//) returns only the integer part of the result.

Example - Modulus Operator (%)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
0
1

Explanation

The modulus operator returns the remainder after division.

Example - Exponent Operator (**)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
8

Explanation

The exponent operator calculates the power of a number.

2. Relational Operators

Relational operators compare two values.

The result is always True or False.

Operator Meaning
>Greater Than
>=Greater Than or Equal To
<Less Than
<=Less Than or Equal To
==Equal To
!=Not Equal To

Example - Relational Operators

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
False
True
False
True

3. Logical Operators

Logical operators are used to combine multiple conditions.

Operator Meaning
andLogical AND
orLogical OR
notLogical NOT

Example - AND Operator

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Explanation

The and operator returns True only when both conditions are True.

Example - OR Operator

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Explanation

The or operator returns True if at least one condition is True.

Example - NOT Operator

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
False
True

Explanation

The not operator reverses the Boolean value.

4. Bitwise Operators

Bitwise operators perform operations at the binary (bit) level.

Operator Meaning
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise Complement
<<Left Shift
>>Right Shift

Example - Bitwise Operators

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
4
5

5. Assignment Operators

Assignment operators are used to assign or update variable values.

Operator Meaning
=Assign
+=Add and Assign
-=Subtract and Assign
*=Multiply and Assign
/=Divide and Assign

Example - Assignment Operator

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
15

6. Special Operators

Python provides two special operators:

  1. Identity Operators
  2. Membership Operators

Identity Operators

Identity operators compare whether two variables refer to the same object.

Operator Meaning
isChecks whether both variables refer to the same object.
is notChecks whether variables refer to different objects.

Example - Identity Operator

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Membership Operators

Membership operators check whether a value exists in a sequence.

Operator Meaning
inPresent
not inNot Present

Example - Membership Operator

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Operator Precedence

Python follows operator precedence rules while evaluating expressions.

Example - Operator Precedence

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
16

Explanation

Multiplication has higher priority than addition.

First:

2 * 3 = 6
10 + 6 = 16
        

Real World Usage of Operators

Operators are commonly used in:

  • Calculations
  • Conditions
  • Loops
  • Data validation
  • Business logic

Important Notes

  • Operators perform operations on operands.
  • Different operators are used for different purposes.
  • Relational and logical operators return Boolean values.
  • Operator precedence affects the order of execution.

Summary of Operators

Operator Type Examples
Arithmetic+, -, *, /, %, //, **
Relational>, <, ==, !=
Logicaland, or, not
Bitwise&, |, ^, ~, <<, >>
Assignment=, +=, -=, *=, /=
Identityis, is not
Membershipin, not in

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Arithmetic Operators