🐍 OPERATORS IN PYTHON
Arithmetic • Relational • Logical • Assignment • Ternary • Bitwise • Increment / Decrement
📘 1. What is an Operator?
An operator is a symbol or keyword used to
perform an operation on one or more values, called
operands.
Example: 10 + 20
Here,
Example: 10 + 20
Here,
+ is the operator and
10 and 20 are operands.
📚 2. Types of Operators in Python
➕ Arithmetic
Mathematical calculations
Mathematical calculations
⚖️ Relational
Compare values
Compare values
🧠 Logical
Combine conditions
Combine conditions
📝 Assignment
Assign values
Assign values
❓ Ternary
Conditional expression
Conditional expression
💻 Bitwise
Operate on bits
Operate on bits
🔄 Increment / Decrement
Value modification
Value modification
1️⃣ ➕ Arithmetic Operators
Arithmetic operators are used to perform mathematical
calculations on numerical values.
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3.333... |
| // | Floor Division | 10 // 3 | 3 |
| % | Modulus | 10 % 3 | 1 |
| ** | Exponentiation | 2 ** 3 | 8 |
a = 10
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)
13
7
30
3.3333333333333335
3
1
1000
7
30
3.3333333333333335
3
1
1000
2️⃣ ⚖️ Relational / Comparison Operators
Relational operators compare two values and return either
True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 10 == 10 | True |
| != | Not equal to | 10 != 5 | True |
| > | Greater than | 10 > 5 | True |
| < | Less than | 10 < 5 | False |
| >= | Greater than or equal | 10 >= 10 | True |
| <= | Less than or equal | 5 <= 10 | True |
a = 10
b = 5
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
False
True
True
False
True
False
True
True
False
True
False
3️⃣ 🧠 Logical / Boolean Operators
Logical operators are used to combine multiple conditions.
They return True or False
when used with Boolean conditions.
| Operator | Meaning | Example |
|---|---|---|
| and | True if both conditions are True | A and B |
| or | True if at least one condition is True | A or B |
| not | Reverses the Boolean result | not A |
Example: and
a = 10
print(a > 5 and a < 20)
True
Example: or
a = 10
print(a < 5 or a > 5)
True
Example: not
a = True
print(not a)
False
4️⃣ 📝 Assignment Operators
Assignment operators are used to assign or update values
stored in variables.
| Operator | Example | Equivalent Expression |
|---|---|---|
| = | x = 10 | x = 10 |
| += | x += 5 | x = x + 5 |
| -= | x -= 5 | x = x - 5 |
| *= | x *= 5 | x = x * 5 |
| /= | x /= 5 | x = x / 5 |
| //= | x //= 5 | x = x // 5 |
| %= | x %= 5 | x = x % 5 |
| **= | x **= 2 | x = x ** 2 |
| &= | x &= 2 | x = x & 2 |
| |= | x |= 2 | x = x | 2 |
| ^= | x ^= 2 | x = x ^ 2 |
| >>= | x >>= 1 | x = x >> 1 |
| <<= | x <<= 1 | x = x << 1 |
x = 10
x += 5
print(x)
x *= 2
print(x)
x -= 5
print(x)
15
30
25
30
25
5️⃣ ❓ Ternary Operator
Python does not have a separate ?: operator
like C or Java. Instead, Python provides a
conditional expression, commonly called
the ternary operator.
Syntax
value_if_true if condition else value_if_false
Example
age = 20
result = "Adult" if age >= 18 else "Minor"
print(result)
Adult
Another Example
a = 10
b = 20
largest = a if a > b else b
print(largest)
20
💡 The ternary expression is useful when a simple
if-else decision needs to be written in one line.
6️⃣ 💻 Bitwise Operators
Bitwise operators work directly on the binary representation
of integer values.
| Operator | Name | Example |
|---|---|---|
| & | Bitwise AND | 5 & 3 |
| | | Bitwise OR | 5 | 3 |
| ^ | Bitwise XOR | 5 ^ 3 |
| ~ | Bitwise NOT | ~5 |
| << | Left Shift | 5 << 1 |
| >> | Right Shift | 5 >> 1 |
Example
a = 5
b = 3
print(a & b)
print(a | b)
print(a ^ b)
print(~a)
print(a << 1)
print(a >> 1)
1
7
6
-6
10
2
7
6
-6
10
2
🔢 7️⃣ Bitwise Operation Example
Consider:
a = 5
b = 3
Binary representations:
| Decimal | Binary |
|---|---|
| 5 | 0101 |
| 3 | 0011 |
Bitwise AND
5 & 3
0101
0011
----
0001
Result = 1
Bitwise OR
0101
0011
----
0111
Result = 7
Bitwise XOR
0101
0011
----
0110
Result = 6
7️⃣ 🔄 Increment and Decrement Operators
Python does not provide the
Instead, Python uses
++ increment or -- decrement
operators found in languages such as C, C++ and Java.
Instead, Python uses
+= 1 and -= 1.
Increment
x = 10
x += 1
print(x)
11
Decrement
x = 10
x -= 1
print(x)
9
⚠️ Remember:
x++ and x-- are invalid syntax in Python.
⚖️ 8️⃣ Python vs C/Java Increment Operators
| Operation | C / Java | Python |
|---|---|---|
| Increment | x++ | x += 1 |
| Decrement | x-- | x -= 1 |
| Increase by 5 | x += 5 | x += 5 |
| Decrease by 5 | x -= 5 | x -= 5 |
🧮 9️⃣ Combined Example
a = 10
b = 5
# Arithmetic
print(a + b)
# Relational
print(a > b)
# Logical
print(a > 5 and b < 10)
# Assignment
a += 2
print(a)
# Ternary
result = "Large" if a > 10 else "Small"
print(result)
# Bitwise
print(a & b)
# Increment
a += 1
print(a)
# Decrement
a -= 1
print(a)
15
True
True
12
Large
4
13
12
True
True
12
Large
4
13
12
✅ Advantages of Operators
- Make mathematical calculations simple.
- Allow comparison between values.
- Help combine multiple conditions.
- Make variable updates easy.
- Support conditional expressions.
- Bitwise operators provide low-level bit manipulation.
- Make programs shorter and more expressive.
❌ Common Problems / Limitations
- Incorrect operator selection can produce wrong results.
- Operator precedence can sometimes be confusing for beginners.
- Bitwise operations require understanding binary numbers.
- Python does not support
++and--. - Division by zero causes an error.
🎯 Important Points for Examination
- + performs addition.
- - performs subtraction.
- * performs multiplication.
- / performs division.
- // performs floor division.
- % returns the remainder.
- ** performs exponentiation.
- Relational operators return True or False.
- and, or, not are logical operators.
- Assignment operators assign or update values.
- Python uses conditional expressions instead of the C-style ?: operator.
- Bitwise operators work on binary representations of integers.
- Python does not support ++ and --.
- Use += 1 for increment.
- Use -= 1 for decrement.
📌 Quick Summary
| Operator Type | Main Operators | Purpose |
|---|---|---|
| Arithmetic | + - * / // % ** | Mathematical calculations |
| Relational | == != > < >= <= | Compare values |
| Logical | and, or, not | Combine conditions |
| Assignment | = += -= *= /= %= **= | Assign/update values |
| Ternary | x if condition else y | Short conditional decision |
| Bitwise | & | ^ ~ << >> | Bit-level operations |
| Increment | += 1 | Increase value |
| Decrement | -= 1 | Decrease value |
No comments:
Post a Comment