Nearby lessons

29 of 159

Python - Operators

📌 What You Will Learn
  • Define operators and operands in Python
  • Identify the six categories of operators
  • Use arithmetic, relational, and logical operators
  • Use bitwise, assignment, and special operators
  • Apply operator precedence while evaluating expressions

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

🐍Code Cell
1a = 10
2b = 20
3 
4c = a + b
5 
6print(c)
Output
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 (+)

🐍Code Cell
1a = 10
2b = 20
3 
4print(a + b)
Output
30

Example - Subtraction Operator (-)

🐍Code Cell
1print(20 - 10)
Output
10

Example - Multiplication Operator (*)

🐍Code Cell
1print(10 * 5)
Output
50

Example - Division Operator (/)

🐍Code Cell
1print(10 / 2)
2print(10 / 3)
Output
5.0
3.3333333333333335

Important Note - Division

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

Example - Floor Division Operator (//)

🐍Code Cell
1print(10 // 2)
2print(10 // 3)
3print(10.0 // 2)
Output
5
3
5.0

Explanation

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

Example - Modulus Operator (%)

🐍Code Cell
1print(10 % 2)
2print(10 % 3)
Output
0
1

Explanation

The modulus operator returns the remainder after division.

Example - Exponent Operator (**)

🐍Code Cell
1print(2 ** 3)
Output
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

🐍Code Cell
1a = 10
2b = 20
3 
4print(a > b)
5print(a < b)
6print(a == b)
7print(a != b)
Output
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

🐍Code Cell
1print(True and True)
2print(True and False)
Output
True
False

Explanation

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

Example - OR Operator

🐍Code Cell
1print(True or False)
2print(False or False)
Output
True
False

Explanation

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

Example - NOT Operator

🐍Code Cell
1print(not True)
2print(not False)
Output
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

🐍Code Cell
1print(4 & 5)
2print(4 | 5)
Output
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

🐍Code Cell
1a = 10
2 
3a += 5
4 
5print(a)
Output
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

🐍Code Cell
1a = 10
2b = 10
3 
4print(a is b)
Output
True

Membership Operators

Membership operators check whether a value exists in a sequence.

Operator Meaning
inPresent
not inNot Present

Example - Membership Operator

🐍Code Cell
1s = "python"
2 
3print('p' in s)
4print('z' in s)
Output
True
False

Operator Precedence

Python follows operator precedence rules while evaluating expressions.

Example - Operator Precedence

🐍Code Cell
1print(10 + 2 * 3)
Output
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
📝 Key Takeaways
  • An operator is a symbol that performs an operation on operands
  • Python supports six categories of operators
  • Arithmetic operators perform mathematical operations
  • Relational and logical operators return True or False
  • Bitwise operators perform operations at the binary bit level
  • Multiplication has higher precedence than addition

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8