ere's a concise comparison of the main operators in C++ and Python, focusing on syntax and behavior across key categories:
✅ 1. Arithmetic Operators
| Operation | C++ | Python | Notes |
|---|
| Addition | a + b | a + b | Same |
| Subtraction | a - b | a - b | Same |
| Multiplication | a * b | a * b | Same |
| Division | a / b | a / b | Python always returns float |
| Integer Division | a / b | a // b | C++ truncates if both ints |
| Modulus | a % b | a % b | Same |
| Power | pow(a,b) | a ** b | pow() exists in both too |
✅ 2. Assignment Operators
| Operation | C++ | Python |
|---|
| Assignment | a = b | a = b |
| Add and assign | a += b | a += b |
| Subtract and assign | a -= b | a -= b |
| Multiply and assign | a *= b | a *= b |
| Divide and assign | a /= b | a /= b |
| Modulus and assign | a %= b | a %= b |
| Power and assign | N/A | a **= b |
✅ 3. Comparison Operators
| Operation | C++ | Python | Notes |
|---|
| Equal | a == b | a == b | Same |
| Not equal | a != b | a != b | Same |
| Greater than | a > b | a > b | Same |
| Less than | a < b | a < b | Same |
| Greater or equal | a >= b | a >= b | Same |
| Less or equal | a <= b | a <= b | Same |
✅ 4. Logical Operators
| Operation | C++ | Python | Notes |
|---|
| Logical AND | && | and | Python uses words |
| Logical OR | ` | | ` |
| Logical NOT | !a | not a | |
✅ 5. Bitwise Operators
| Operation | C++ | Python | Notes |
|---|
| AND | a & b | a & b | Same |
| OR | `a | b` | `a |
| XOR | a ^ b | a ^ b | Same |
| NOT | ~a | ~a | Same |
| Shift left | a << b | a << b | Same |
| Shift right | a >> b | a >> b | Same |
✅ 6. Membership & Identity Operators (Only in Python)
| Operator | Python only | Description |
|---|
in | x in y | True if x is in y |
not in | x not in y | True if x is not in y |
is | x is y | True if x and y reference same obj |
is not | x is not y | True if x and y reference different |
✅ 7. Pointer and Memory Operators (Only in C++)
| Operator | C++ only | Description |
|---|
| Address-of | &a | Returns memory address of a |
| Dereference | *a | Access value at pointer a |
new / delete | Memory management | Manual memory control |
✅ 8. Ternary Operator
| C++ | Python | Notes |
|---|
a ? b : c | b if a else c | Python uses readable syntax |
No comments:
Post a Comment