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.
Example:
x = 10
y = 3
print(x + y)
print(x ** y)
print(x // y)
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.
Example:
x = 10
x += 5
print(x)
x **= 2
print(x)
Comparison Operators
Comparison operators are used to compare two values. They return either True or False (a Boolean result).
Example:
a = 10
b = 20
print(a > b)
print(a != b)
Logical Operators
Logical operators combine multiple conditions and return a Boolean result (True or False).
Example:
a = 10
b = 5
print(a > 5 and b < 10)
print(a > 5 or b > 10)
print(not(a == 10))
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.
Example:
a = 5
b = 3
print(a & b)
print(a | b)
print(a ^ b)
print(a << 1)
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.
Example:
x = 10 + 3 * 2
print(x)
y = (10 + 3) * 2
print(y)
Summary
Understanding operators in Python is essential for writing efficient and logical code.
In the next article, we'll explore Input and Output in Python — learning how to take user input and display formatted output effectively.
Comments
No comments yet. Ask a question or share what helped.