Python operators in software development are special symbols or keywords used to perform operations on values and variables. These operations can be arithmetic, comparisons, logical decisions, bit manipulations, assignments, and more.
Learn how software engineering in Abuja
Python groups operators into seven major categories:
Let’s break each one down with simple examples.
Used for mathematical operations.
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | 3 + 2 → 5 |
- | Subtraction | 5 - 1 → 4 |
* | Multiplication | 4 * 3 → 12 |
/ | Division | 10 / 2 → 5.0 |
% | Modulus (remainder) | 10 % 3 → 1 |
** | Exponentiation | 2 ** 3 → 8 |
// | Floor division | 7 // 2 → 3 |
Example:
a = 7
b = 3
print(a + b) # 10
print(a % b) # 1
print(a ** b) # 343
Used to compare values. They return True or False.
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | 5 == 5 → True |
!= | Not equal | 5 != 3 → True |
> | Greater than | 4 > 2 → True |
< | Less than | 3 < 1 → False |
>= | Greater or equal | 4 >= 4 → True |
<= | Less or equal | 2 <= 3 → True |
Example:
x = 10
y = 20
print(x < y) # True
print(x == y) # False
Used for boolean logic operations.
| Operator | Meaning |
|---|---|
and | True if both conditions are true |
or | True if at least one is true |
not | Negates a boolean |
Example:
x = 5
print(x > 2 and x < 10) # True
print(x < 2 or x == 5) # True
print(not(x == 5)) # False
Used to assign values and perform compound assignment.
| Operator | Meaning | Example |
|---|---|---|
= | Assign | x = 10 |
+= | Add and assign | x += 3 |
-= | Subtract and assign | x -= 2 |
*= | Multiply and assign | x *= 4 |
/= | Divide and assign | x /= 2 |
%= | Modulus assign | x %= 3 |
**= | Power assign | x **= 2 |
//= | Floor divide assign | x //= 3 |
Example:
x = 10
x += 5 # x = x + 5
print(x) # 15 Used to perform operations on binary numbers.
| Operator | Meaning | Example |
|---|---|---|
& | AND | 5 & 3 → 1 |
| ` | ` | OR |
^ | XOR | 5 ^ 3 → 6 |
~ | NOT | ~5 → -6 |
<< | Left shift | 5 << 1 → 10 |
>> | Right shift | 5 >> 1 → 2 |
Example:
a = 5 # 101 in binary
b = 3 # 011 in binary
print(a & b) # 1
print(a | b) # 7 Check if a value exists in a sequence (list, string, tuple).
| Operator | Meaning |
|---|---|
in | True if value is present |
not in | True if value is not present |
Example:
nums = [1, 2, 3, 4]
print(3 in nums) # True
print(5 not in nums) # True Check if two variables reference the same object in memory.
| Operator | Meaning |
|---|---|
is | True if identical objects |
is not | True if not identical |
Example:
a = [1, 2]
b = a
c = [1, 2]
print(a is b) # True (same object)
print(a is c) # False (same content, different object) Python operators allow you to:
They form the foundation of logical and mathematical operations in Python programming.
Latest tech news and coding tips.
Database migration is one of the most challenging tasks in software engineering. While both PostgreSQL…
Modern JavaScript isn’t just let, const, arrow functions, and promises anymore. Over the years, the language has…
Software development is one of the most rewarding careers in technology, but it is also…
Printing a document in JavaScript usually means triggering the browser’s print dialog and controlling what…
The display property controls how an element behaves in the layout and how its children are arranged. Access software…
JavaScript is one of the most flexible programming languages ever created. That flexibility is powerful,…