| id | title | sidebar_label | sidebar_position | tags | |||||
|---|---|---|---|---|---|---|---|---|---|
python-operators |
Python Operators |
Python Operators |
5 |
|
In Python, operators are special symbols used to perform operations on variables and values. Python supports a wide range of operators categorized based on their functionality.
Used to perform basic mathematical operations:
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition | 10 + 5 |
15 |
- |
Subtraction | 10 - 5 |
5 |
* |
Multiplication | 10 * 5 |
50 |
/ |
Division | 10 / 5 |
2.0 |
// |
Floor Division | 10 // 3 |
3 |
% |
Modulus (remainder) | 10 % 3 |
1 |
** |
Exponentiation | 2 ** 3 |
8 |
Used to compare two values and return a Boolean result (True or False).
| Operator | Description | Example | Result |
|---|---|---|---|
== |
Equal to | 5 == 5 |
True |
!= |
Not equal to | 5 != 3 |
True |
> |
Greater than | 5 > 3 |
True |
< |
Less than | 5 < 3 |
False |
>= |
Greater than or equal | 5 >= 5 |
True |
<= |
Less than or equal | 5 <= 3 |
False |
Used to combine conditional statements here.
| Operator | Description | Example | Result |
|---|---|---|---|
and |
True if both operands are true | True and False |
False |
or |
True if at least one is true | True or False |
True |
not |
Reverses the result | not True |
False |
Used to assign values to variables.
| Operator | Example | Same as |
|---|---|---|
= |
x = 5 |
Assign 5 to x |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 2 |
x = x - 2 |
*= |
x *= 4 |
x = x * 4 |
/= |
x /= 2 |
x = x / 2 |
//= |
x //= 2 |
x = x // 2 |
%= |
x %= 2 |
x = x % 2 |
**= |
x **= 2 |
x = x ** 2 |
Used to perform bit-level operations.
| Operator | Description | Example | Result |
|---|---|---|---|
& |
AND | 5 & 3 |
1 |
| ` | ` | OR | `5 |
^ |
XOR | 5 ^ 3 |
6 |
~ |
NOT | ~5 |
-6 |
<< |
Left Shift | 5 << 1 |
10 |
>> |
Right Shift | 5 >> 1 |
2 |
Used to test if a sequence contains a value.
| Operator | Description | Example | Result |
|---|---|---|---|
in |
Value exists in the sequence | "a" in "apple" |
True |
not in |
Value not in sequence | "z" not in "apple" |
True |
Used to compare the memory location of two objects.
| Operator | Description | Example | Result |
|---|---|---|---|
is |
Returns True if same object |
x is y |
True |
is not |
Returns True if not same object |
x is not y |
True |
📌 Use Case: Shopping Cart Total
price = 150
quantity = 3
total = price * quantity # ➜ 450
discount = 0.10
final_amount = total - (total * discount) # ➜ 405.0Explanation: Calculates the total bill with a discount using * and -.
📌 Use Case: Age Verification for Voting
age = 17
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible")Explanation: Compares age using >= to determine eligibility.
📌 Use Case: Login System Authentication
username = "admin"
password = "1234"
if username == "admin" and password == "1234":
print("Login successful")
else:
print("Invalid credentials")Explanation: Combines two conditions using and.
📌 Use Case: Updating Game Score
score = 0
score += 10 # Player scored
score += 5 # Bonus
# Final score = 15Explanation: Increments the score using +=.
📌 Use Case: File Permission System (Read = 4, Write = 2, Execute = 1)
read = 4
write = 2
execute = 1
permission = read | write # ➜ 6 (read + write)
has_write = permission & write # ➜ 2 (True)Explanation: Combines permissions using | and checks with &.
📌 Use Case: Search Term Filtering
query = "python"
if "python" in ["java", "python", "c++"]:
print("Result found")Explanation: Checks if a word exists in a list using in.
📌 Use Case: Comparing Object Identity
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y) # True
print(x is z) # FalseExplanation: Uses is to check if variables point to the same object in memory.
📌 Use Case: Evaluating an Expression
result = 10 + 5 * 2 # ➜ 10 + (5 * 2) = 20Explanation: * is evaluated before + due to higher precedence.
| Operator Type | Example Use Case |
|---|---|
| Arithmetic | Calculating total cost |
| Comparison | Validating age for access |
| Logical | Checking login credentials |
| Assignment | Updating scores or counters |
| Bitwise | Managing file permissions (bits) |
| Membership | Search and filter operations |
| Identity | Verifying object references |
| Precedence | Proper expression evaluation order |
Operators are the core building blocks of logic and calculation in Python. Understanding how they work is crucial to writing effective Python code.