Python Tutorial
Python Operators
Learn Python operators with clear tables and examples for arithmetic, comparison, assignment, logical, bitwise, membership, identity, and precedence rules.
What are Python operators?
Operators are symbols or words that perform an operation on one or more values. The values being operated on are called operands. For example, in total = price + tax, + adds two operands and = assigns the result to total.
Types of Python operators
| Category | Operators | Typical use |
|---|---|---|
| Arithmetic | + - * / // % ** | Calculations with numbers. |
| Comparison | == != < <= > >= | Compare values and produce a Boolean result. |
| Assignment | = += -= *= /= //= %= **= | Assign or update a variable. |
| Logical | and or not | Combine or reverse conditions. |
| Bitwise | & | ^ ~ << >> | Operate on the bits of integers. |
| Membership | in not in | Check whether a value exists in a collection. |
| Identity | is is not | Check whether two names refer to the same object. |
1. Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 15 + 4 | 19 |
- | Subtraction | 15 - 4 | 11 |
* | Multiplication | 15 * 4 | 60 |
/ | True division | 15 / 4 | 3.75 |
// | Floor division | 15 // 4 | 3 |
% | Remainder | 15 % 4 | 3 |
** | Exponentiation | 2 ** 3 | 8 |
a = 15
b = 4
print(a + b)
print(a / b)
print(a // b)
print(a % b)
print(2 ** 3)Python’s / operator returns a floating-point result. Floor division rounds down, which is important for negative values as well as positive values.
2. Comparison operators
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | age == 18 |
!= | Not equal to | status != "closed" |
< | Less than | score < 50 |
<= | Less than or equal to | items <= 10 |
> | Greater than | price > 100 |
>= | Greater than or equal to | score >= 50 |
Comparison expressions normally produce True or False and are commonly used in conditions:
score = 72
if score >= 50:
print("Pass")3. Assignment operators
The basic assignment operator stores a value. Augmented assignment combines an operation with assignment:
| Operator | Equivalent form | Example |
|---|---|---|
= | Assign | total = 10 |
+= | total = total + 5 | total += 5 |
-= | total = total - 5 | total -= 5 |
*= | total = total * 5 | total *= 5 |
/= | total = total / 5 | total /= 5 |
//= | total = total // 5 | total //= 5 |
%= | total = total % 5 | total %= 5 |
**= | total = total ** 5 | total **= 5 |
4. Logical operators
| Operator | Meaning | Example |
|---|---|---|
and | True when both expressions are truthy. | age >= 18 and active |
or | True when at least one expression is truthy. | admin or owner |
not | Reverses a truth value. | not completed |
Python’s logical operators use short-circuit evaluation. For example, the right side of and may not run if the left side is already false.
5. Bitwise operators
Bitwise operators work on the binary representation of integers. They are useful for flags, masks, permissions, and low-level protocols.
| Operator | Meaning | Example |
|---|---|---|
& | Bitwise AND | 6 & 3 |
| | Bitwise OR | 6 | 3 |
^ | Bitwise XOR | 6 ^ 3 |
~ | Bitwise inversion | ~6 |
<< | Shift bits left | 6 << 1 |
>> | Shift bits right | 6 >> 1 |
6. Membership operators
in and not in test whether a value is contained in a string, list, tuple, set, or dictionary. For a dictionary, membership checks keys by default:
languages = ["Python", "Java"]
print("Python" in languages)
print("Go" not in languages)
student = {"name": "Asha"}
print("name" in student)7. Identity operators
is and is not test whether two names refer to the same object. They are different from ==, which compares values:
first = [1, 2]
second = [1, 2]
same = first
print(first == second) # True: values are equal
print(first is second) # False: different list objects
print(first is same) # True: same objectUse is None and is not None for None. Use == when you want to compare ordinary values.
Operator precedence
Precedence determines which operation is evaluated first. Parentheses make the intended order clear and are recommended when an expression could be misunderstood.
| Higher priority | Operators |
|---|---|
| 1 | ** |
| 2 | Unary +x, -x, ~x |
| 3 | * / // % |
| 4 | + - |
| 5 | << >> |
| 6 | &, then ^, then | |
| 7 | Comparisons, in, is |
| 8 | not, then and, then or |
result = (10 + 5) * 2
print(result)Operator best practices
- Use parentheses when they improve readability.
- Use
==for value comparison andismainly for identity checks such asNone. - Be careful when mixing strings and numbers; convert values explicitly when needed.
- Use bitwise operators only when the bit-level behavior is intentional and documented.
Further Reading
Continue learning with these related Python tutorials:
Continue learning
