Learn all Python operators with examples—arithmetic, comparison, logical, bitwise, assignment, identity, and membership operators. Includes actionable tips and tool recommendations.
In Python, operators are special symbols that perform operations on variables and values. Understanding them is fundamental for building logic in your programs.
This article covers all operator types in Python, complete with structured breakdowns, practical examples, best practices, and tool suggestions.
Operators allow you to perform actions such as calculations, comparisons, and logical evaluations. Python categorizes operators into different types based on their function.
Example:
x = 10
y = 5
print(x + y) # Output: 15
2. Types of Python Operators
Python operators are broadly divided into:
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
Assignment Operators
Membership Operators
Identity Operators
3. Arithmetic Operators
Used for mathematical calculations.
Operator
Description
Example (x=10, y=5)
Result
+
Addition
x + y
15
-
Subtraction
x - y
5
*
Multiplication
x * y
50
/
Division
x / y
2.0
%
Modulus
x % y
0
**
Exponentiation
x ** y
100000
//
Floor Division
x // y
2
Actionable Tip: Use // when you need an integer result from division.
4. Comparison Operators
Used to compare two values, returning True or False.
Operator
Description
Example (x=10, y=5)
Result
==
Equal
x == y
False
!=
Not equal
x != y
True
>
Greater than
x > y
True
<
Less than
x < y
False
>=
Greater or equal
x >= y
True
<=
Less or equal
x <= y
False
Best Practice: Use comparison operators in conditionals for decision-making.
5. Logical Operators
Used to combine conditional statements.
Operator
Description
Example
Result
and
True if both true
(x > 5 and y < 10)
True
or
True if one is true
(x > 5 or y > 20)
True
not
Reverses the result
not(x > 5)
False
6. Bitwise Operators
Used for binary operations on integers.
Operator
Description
Example (x=5, y=3)
Result
&
AND
x & y
1
`
`
OR
`x
^
XOR
x ^ y
6
~
NOT
~x
-6
<<
Left shift
x << 1
10
>>
Right shift
x >> 1
2
Actionable Tip: Use bitwise operations for performance in low-level data processing.
7. Assignment Operators
Used to assign values to variables.
Operator
Example (x=5)
Equivalent to
=
x = 5
x = 5
+=
x += 3
x = x + 3
-=
x -= 3
x = x - 3
*=
x *= 3
x = x * 3
/=
x /= 3
x = x / 3
%=
x %= 3
x = x % 3
**=
x **= 3
x = x ** 3
//=
x //= 3
x = x // 3
8. Membership Operators
Used to check membership in sequences.
Operator
Example
Result
in
"a" in "apple"
True
not in
"b" not in "apple"
True
9. Identity Operators
Used to compare object memory locations.
Operator
Example
Result
is
x is y
True if same object
is not
x is not y
True if different objects
Example:
a = [1, 2, 3]
b = a
print(a is b) # True
10. Best Practices
Use parentheses to ensure clarity in complex expressions.
Avoid chaining too many logical conditions without readability.
Use is and is not only for identity checks, not equality.
Optimize arithmetic operations for performance-sensitive applications.
Conclusion
Operators are the foundation of logic in Python. By mastering them, you can write cleaner, more efficient, and more readable code.
Learn how garbage collection works in Python. You’ll learn the core ideas (reference counting and generational GC), explore the gc module, diagnose cyclic references, use weakref safely, and adopt practical patterns to keep memory usage healthy in real-world apps.
Learn Web Scraping with Python in 2025 with this complete step-by-step tutorial. Includes practical examples, code snippets, tools, and best practices for safe and efficient scraping.
This website uses cookies to enhance your browsing experience. By continuing to use this site, you consent to the use of cookies. Please review our Privacy Policy for more information on how we handle your data. Cookie Policy
These cookies are essential for the website to function properly.
These cookies help us understand how visitors interact with the website.
These cookies are used to deliver personalized advertisements.