Nearby lessons
29 of 159Python - Operators
- 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
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 (+)
Example - Subtraction Operator (-)
Example - Multiplication Operator (*)
Example - Division Operator (/)
Important Note - Division
The division operator (/) always returns a floating-point value.
Example - Floor Division Operator (//)
Explanation
The floor division operator (//) returns only the integer part of the result.
Example - Modulus Operator (%)
Explanation
The modulus operator returns the remainder after division.
Example - Exponent Operator (**)
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
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
Explanation
The and operator returns True only when both conditions are True.
Example - OR Operator
Explanation
The or operator returns True if at least one condition is True.
Example - NOT Operator
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
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
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
Membership Operators
Membership operators check whether a value exists in a sequence.
| Operator | Meaning |
|---|---|
in | Present |
not in | Not Present |
Example - Membership Operator
Operator Precedence
Python follows operator precedence rules while evaluating expressions.
Example - Operator Precedence
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
- 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