Nearby lessons
34 of 108Python - 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
30
Explanation
| Component | Description |
|---|---|
+ |
Operator |
a, b |
Operands |
Types of Operators
Python supports the following types of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- 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
30
Example - Subtraction Operator (-)
main.py
10
Example - Multiplication Operator (*)
main.py
50
Example - Division Operator (/)
main.py
5.0 3.3333333333333335
Important Note - Division
The division operator (/) always returns a floating-point value.
Example - Floor Division Operator (//)
main.py
5 3 5.0
Explanation
The floor division operator (//) returns only the integer part of the result.
Example - Modulus Operator (%)
main.py
0 1
Explanation
The modulus operator returns the remainder after division.
Example - Exponent Operator (**)
main.py
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
False True False True
3. Logical Operators
Logical operators are used to combine multiple conditions.
| Operator | Meaning |
|---|---|
and | Logical AND |
or | Logical OR |
not | Logical NOT |
Example - AND Operator
main.py
True False
Explanation
The and operator returns True only when both conditions are True.
Example - OR Operator
main.py
True False
Explanation
The or operator returns True if at least one condition is True.
Example - NOT Operator
main.py
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
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
15
6. Special Operators
Python provides two special operators:
- Identity Operators
- Membership Operators
Identity Operators
Identity operators compare whether two variables refer to the same object.
| Operator | Meaning |
|---|---|
is | Checks whether both variables refer to the same object. |
is not | Checks whether variables refer to different objects. |
Example - Identity Operator
main.py
True
Membership Operators
Membership operators check whether a value exists in a sequence.
| Operator | Meaning |
|---|---|
in | Present |
not in | Not Present |
Example - Membership Operator
main.py
True False
Operator Precedence
Python follows operator precedence rules while evaluating expressions.
Example - Operator Precedence
main.py
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 | >, <, ==, != |
| Logical | and, or, not |
| Bitwise | &, |, ^, ~, <<, >> |
| Assignment | =, +=, -=, *=, /= |
| Identity | is, is not |
| Membership | in, not in |