Operators in Python

In Python, operators are special symbols or keywords that perform operations on values and variables. They are the foundation of any computation โ€” helping you perform tasks like addition, comparison, or logical evaluation.

This article explains different types of operators in Python โ€” including Arithmetic, Assignment, Comparison, Logical, and Bitwise operators โ€” along with an overview of operator precedence.

What Are Operators?

An operator is a symbol that tells the interpreter to perform a specific action. The values or variables that operators act upon are called operands.

a = 10
b = 5
result = a + b
Here:
a and b are operands
+ is an operator
The expression a + b evaluates to 15

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, and division.

Operator Description Example Result
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2.0
// Floor Division 10 // 3 3
% Modulus (Remainder) 10 % 3 1
** Exponentiation 2 ** 3 8

Example:

x = 10
y = 3
print(x + y)  # 13
print(x ** y) # 1000
print(x // y) # 3

Assignment Operators

Assignment operators are used to assign values to variables. Python supports compound assignment โ€” meaning you can perform an operation and assignment in one step.

Operator Description Example Equivalent To
= Assigns value x = 10 โ€”
+= Add and assign x += 5 x = x + 5
-= Subtract and assign x -= 3 x = x - 3
*= Multiply and assign x *= 2 x = x * 2
/= Divide and assign x /= 4 x = x / 4
//= Floor divide and assign x //= 2 x = x // 2
%= Modulus and assign x %= 3 x = x % 3
**= Power and assign x **= 2 x = x ** 2

Example:

x = 10
x += 5
print(x)  # 15

x **= 2
print(x)  # 225

Comparison Operators

Comparison operators are used to compare two values. They return either True or False (a Boolean result).

Operator Description Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 7 > 4 True
< Less than 7 < 4 False
>= Greater than or equal to 5 >= 5 True
<= Less than or equal to 4 <= 6 True

Example:

a = 10
b = 20
print(a > b)   # False
print(a != b)  # True

Logical Operators

Logical operators combine multiple conditions and return a Boolean result (True or False).

Operator Description Example Result
and Returns True if both conditions are true (5 > 2 and 10 > 3) True
or Returns True if at least one condition is true (5 > 10 or 3 < 7) True
not Reverses the result not(5 > 2) False

Example:

a = 10
b = 5

print(a > 5 and b < 10)  # True
print(a > 5 or b > 10)   # True
print(not(a == 10))      # False

Bitwise Operators

Bitwise operators perform operations on binary representations of numbers (bit level). These are mostly used in low-level programming, such as hardware or performance-based applications.

Operator Description Example Binary Operation Result
& AND 5 & 3 0101 & 0011 1
| OR 5 | 3 0101 | 0011 7
^ XOR 5 ^ 3 0101 ^ 0011 6
~ NOT ~5 โ€” -6
<< Left Shift 5 << 1 0101 โ†’ 1010 10
>> Right Shift 5 >> 1 0101 โ†’ 0010 2

Example:

a = 5   # 0101
b = 3   # 0011

print(a & b)  # 1
print(a | b)  # 7
print(a ^ b)  # 6
print(a << 1) # 10

Operator Precedence in Python

When multiple operators are used in a single expression, Python follows a specific order of evaluation โ€” known as operator precedence. Operators with higher precedence are executed first.

Precedence Level Operator Description
1 () Parentheses
2 ** Exponentiation
3 +x, -x, ~x Unary plus, minus, bitwise NOT
4 *, /, //, % Multiplication, Division, Modulus
5 +, - Addition, Subtraction
6 <<, >> Bitwise Shift
7 & Bitwise AND
8 ^ Bitwise XOR
9 | Bitwise OR
10 ==, !=, >, <, >=, <= Comparison
11 not Logical NOT
12 and Logical AND
13 or Logical OR

Example:

x = 10 + 3 * 2
print(x)  # 16 โ†’ Multiplication (*) has higher precedence than addition (+)

y = (10 + 3) * 2
print(y)  # 26 โ†’ Parentheses override precedence

Summary

Understanding operators in Python is essential for writing efficient and logical code.

Operator Type Description
Arithmetic Perform basic math operations
Assignment Assign and update values
Comparison Compare two values and return Boolean results
Logical Combine multiple conditions
Bitwise Work with binary data
Operator Precedence Defines the order in which operations are evaluated
By mastering arithmetic, comparison, logical, and bitwise operations, youโ€™ll be able to create dynamic and powerful expressions.

In the next article, we'll explore Input and Output in Python โ€” learning how to take user input and display formatted output effectively.
Share this Article