OPERATORS AND THEIR OPERATIONS IN PYTHON
Introduction
In Python, operators are special symbols that perform
operations on variables and values. They are the backbone
of programming logic, enabling mathematical calculations,
comparisons, logical decisions, and manipulations of data
structures. Understanding operators and their behavior is
crucial for mastering Python programming.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical
operations such as addition, subtraction, multiplication, and
division.
+ : Addition – Adds two operands. Example: 5 + 3 = 8
- : Subtraction – Subtracts the right operand from the left.
Example: 5 - 3 = 2
* : Multiplication – Multiplies two operands. Example: 5 *
3 = 15
/ : Division – Divides the left operand by the right.
Example: 6 / 3 = 2.0
% : Modulus – Returns the remainder. Example: 5 % 2 = 1
** : Exponentiation – Raises to the power. Example: 2 ** 3
= 8
// : Floor Division – Returns the quotient without decimals.
Example: 5 // 2 = 2
Example: 5 // 2 = 2
2. Comparison Operators
Comparison operators are used to compare two values. The
result of a comparison operation is always a Boolean value:
True or False.
== : Equal to – Returns True if both operands are equal.
!= : Not equal to – Returns True if operands are not equal.
> : Greater than – Returns True if the left operand is greater
than the right.
< : Less than – Returns True if the left operand is less than
the right.
>= : Greater than or equal to – Returns True if left operand
≥ right operand.
<= : Less than or equal to – Returns True if left operand ≤
right operand.
3. Logical Operators
Logical operators combine conditional statements and
return Boolean results.
and : Returns True if both statements are true. Example: (5
> 3 and 8 > 6) → True
or : Returns True if at least one statement is true. Example:
(5 < 3 or 8 > 6) → True
not : Reverses the result. Example: not(5 > 3) → False
4. Assignment Operators
Assignment operators are used to assign values to variables
and also perform operations in a compact form.
= : Assigns value. Example: x = 5
+= : Adds and assigns. Example: x += 3 → x = x + 3
-= : Subtracts and assigns. Example: x -= 2 → x = x - 2
*= : Multiplies and assigns. Example: x *= 4 → x = x * 4
/= : Divides and assigns. Example: x /= 2 → x = x / 2
%= : Modulus and assigns. Example: x %= 3
**= : Exponentiation and assigns. Example: x **= 2
//= : Floor divide and assigns. Example: x //= 2
5. Bitwise Operators
Bitwise operators act on binary numbers at the bit level.
They are used in low-level programming and optimization.
& : Bitwise AND – Sets each bit to 1 if both bits are 1.
| : Bitwise OR – Sets each bit to 1 if at least one bit is 1.
^ : Bitwise XOR – Sets each bit to 1 if only one bit is 1.
~ : Bitwise NOT – Inverts all bits.
<< : Left shift – Shifts bits to the left by a specified number
of positions.
>> : Right shift – Shifts bits to the right by a specified
number of positions.
6. Identity Operators
Identity operators check whether two variables refer to the
same object in memory.
is : Returns True if both variables point to the same object.
is not : Returns True if variables point to different objects.
Example:
x = [1, 2, 3]
y = x
z = [1, 2, 3]
(x is y) → True
(x is z) → False
7. Membership Operators
Membership operators test whether a value or variable is
found in a sequence (such as a list, tuple, or string).
in : Returns True if a value exists in the sequence.
not in : Returns True if a value does not exist in the
sequence.
Example:
x = [1, 2, 3, 4]
(2 in x) → True
(5 not in x) → True
8. Operator Precedence in Python
Operator precedence determines the order in which
operations are performed. Python follows a hierarchy to
evaluate expressions correctly.
Highest to Lowest:
1. Parentheses ()
2. Exponentiation **
3. Unary +, - , ~
4. Multiplication, Division, Floor Division, Modulus
(*, /, //, %)
5. Addition and Subtraction (+, -)
6. Bitwise Shifts (<<, >>)
7. Bitwise AND (&)
8. Bitwise XOR (^)
9. Bitwise OR (|)
10. Comparison Operators (<, >, <=, >=, ==, !=)
11. Logical Operators (not, and, or)
9. Applications of Operators in Python
• Used in mathematical computations and data analysis.
• Enable logical decision-making in control structures like
if-else.
• Facilitate string and list manipulations.
• Used in loops for iteration control.
• Support memory-efficient bitwise operations in embedded
systems.
• Assist in building complex algorithms in AI, ML, and
data science.
Conclusion
Operators in Python are powerful tools that simplify code
execution and logic building. Mastering them allows
programmers to write concise, efficient, and optimized
code for various real-world applications.