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.
A SOC Analyst (Security Operations Center Analyst) is one of the frontline defenders of an organization’s cybersecurity…
Most image upload systems assume one thing: the user has a stable internet connection. That assumption…
Imagine a user submits a form while their internet connection suddenly disappears. Normally, the request…
Cybercriminals don't always announce their presence. Many compromises are designed to remain unnoticed for weeks…
For years, jQuery was everywhere. If you were building websites in the 2010s, there was a…
Few things halt a developer’s flow faster than seeing the dreaded word: CONFLICT. A Git merge…