Theory: Arithmetic operators are used to perform common mathematical operations such as addition, subtraction, multiplication, and division. These are essential when working with numeric data like finance, billing, statistics, etc.
Use Case: Calculate a shopping bill, taxes, or time-based calculations like minutes to hours.
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition | total = price + tax |
Sum of price and tax |
- |
Subtraction | balance = deposit - withdrawal |
Remaining balance |
* |
Multiplication | amount = rate * hours |
Pay for hours worked |
/ |
Division | avg = total / count |
Average value |
// |
Floor Division | minutes // 60 |
Convert to hours |
% |
Modulus | days % 7 |
Leftover days |
** |
Exponent | area = side ** 2 |
Squared value |
savings = income - expenses
Theory: Comparison operators check relationships between values. They return Boolean results (`True` or `False`) and are mainly used in conditions.
Use Case: Validate if a user is old enough, check item availability, or confirm thresholds.
Operator | Description | Example | Returns |
---|---|---|---|
== |
Equal | score == 100 |
True if score is 100 |
!= |
Not equal | age != 18 |
True if age is not 18 |
> |
Greater than | salary > 50000 |
Check for high salary |
< |
Less than | temp < 40 |
Check temperature |
>= |
Greater or equal | marks >= 35 |
Check pass/fail |
<= |
Less or equal | speed <= 60 |
Speed limit |
if age >= 18
Theory: These operators are used to assign values to variables. Combined versions like `+=`, `-=`, `*=` allow updating the variable while assigning.
Use Case: Common in counters, budget adjustments, and game scoring logic.
Operator | Description | Example |
---|---|---|
= |
Assigns value | total = 100 |
+= |
Add and assign | score += 10 |
-= |
Subtract and assign | balance -= 500 |
*= |
Multiply and assign | rate *= 1.5 |
/= |
Divide and assign | amount /= 2 |
salary += bonus
Theory: Logical operators are used to combine multiple conditions. They return Boolean results and are heavily used in validations.
Use Case: Check if a user is an admin and active, or allow login if either email or username is correct.
Operator | Description | Example |
---|---|---|
and |
True if both conditions are true | age >= 18 and has_id |
or |
True if any condition is true | is_admin or is_manager |
not |
Invert boolean | not is_active |
if is_employee and not is_blocked
Theory: Perform bit-level operations. Useful in graphics, hardware, and memory-optimized code.
Operator | Description | Example | Result |
---|---|---|---|
& |
AND | 5 & 3 |
1 |
| |
OR | 5 | 3 |
7 |
^ |
XOR | 5 ^ 3 |
6 |
~ |
NOT | ~5 |
-6 |
<< |
Left shift | 5 << 1 |
10 |
>> |
Right shift | 5 >> 1 |
2 |
Theory: Used to test if a value exists within a container such as a list, string, or set.
Operator | Description | Example | Result |
---|---|---|---|
in |
True if present | 'apple' in fruits |
True |
not in |
True if not present | 'grape' not in fruits |
True |
if item in inventory
Theory: Compare memory location (identity) of two variables. Used to check if two variables point to the same object.
Operator | Description | Example |
---|---|---|
is |
Same object | a is b |
is not |
Different objects | a is not b |
if response is None