Friday, April 4, 2025

Comparison of Main Operators: C++ vs. Python

 ere's a concise comparison of the main operators in C++ and Python, focusing on syntax and behavior across key categories:


1. Arithmetic Operators

OperationC++PythonNotes
Additiona + ba + bSame
Subtractiona - ba - bSame
Multiplicationa * ba * bSame
Divisiona / ba / bPython always returns float
Integer Divisiona / ba // bC++ truncates if both ints
Modulusa % ba % bSame
Powerpow(a,b)a ** bpow() exists in both too

2. Assignment Operators

OperationC++Python
Assignmenta = ba = b
Add and assigna += ba += b
Subtract and assigna -= ba -= b
Multiply and assigna *= ba *= b
Divide and assigna /= ba /= b
Modulus and assigna %= ba %= b
Power and assignN/Aa **= b

3. Comparison Operators

OperationC++PythonNotes
Equala == ba == bSame
Not equala != ba != bSame
Greater thana > ba > bSame
Less thana < ba < bSame
Greater or equala >= ba >= bSame
Less or equala <= ba <= bSame

4. Logical Operators

OperationC++PythonNotes
Logical AND&&andPython uses words
Logical OR``
Logical NOT!anot a

5. Bitwise Operators

OperationC++PythonNotes
ANDa & ba & bSame
OR`ab``a
XORa ^ ba ^ bSame
NOT~a~aSame
Shift lefta << ba << bSame
Shift righta >> ba >> bSame

6. Membership & Identity Operators (Only in Python)

OperatorPython onlyDescription
inx in yTrue if x is in y
not inx not in yTrue if x is not in y
isx is yTrue if x and y reference same obj
is notx is not yTrue if x and y reference different

7. Pointer and Memory Operators (Only in C++)

OperatorC++ onlyDescription
Address-of&aReturns memory address of a
Dereference*aAccess value at pointer a
new / deleteMemory managementManual memory control

8. Ternary Operator

C++PythonNotes
a ? b : cb if a else cPython uses readable syntax

No comments:

Post a Comment

Basic Comparison of C++ and HTML/JavaScript

 Here’s a basic comparison of C++ and HTML/JavaScript for common programming concepts and  simple code examples for each. This will help ...