Introduction

Operators in general are used to perform operations on values and variables in Python.
These are standard symbols used for the purpose of logical and arithmetic operations.

Types of Operators

Arithmetic Operators
Comparison ( Relational ) Operators
Assignment Operators
Logical(Boolean) Operators
Unary Operators
Bitwise Operators
Membership Operators
Identity Operators

Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

OperatorNameExample
+Additiona+b
-Subtractiona-b
*Multiplicationa*b
/Divisiona/b
%Modulusa%b
**Exponentiationa**b
//Floor divisiona//b

Comparison Operators

Comparison operators are used to compare two values:

OperatorNameExample
==Equala==b
!=Not Equala!=b
<Less thana<b
>Greater thana>b
<=Less than or equal toa<=b
>=Greater than or equal toa>=b

Assignment Operators

Assignment operators are used to assign values to variables:

OperatorExampleAs same as
=a = 10a = 10
+=a += 10a = a+10
-=a -= 10a = a-10
*=a *= 10a = a*10
/=a /= 10a = a/10
%=a %= 10a = a%10
**=a **= 10a = a**10
//=a //= 10a = a//10
&=a &= 10a = a&10
|=a |= 10a = a|10
^=a ^= 10a = a^10
>>=a >>= 10a = a>>10
<<=a <<= 10a = a<<10

Logical(Boolean) Operators

Logical operators are used to combine conditional statements:

OperatorDescriptionExample
andReturns True if both statements are truea > 5 and a < 10
orReturns True if one of the statements is truea > 10 or a < 5
not Reverse the result, returns False if the result is truenot(a < 10 or a > 20)

Unary Operators

The unary structure implies character, restoring the same value as its operand. The unary structure implies negate, restoring the nullified incentive as its operand: zero to zero, positive to negative, and negative to positive.
Unary positive also known as plus and unary negative also known as minus are unique operators.
The plus and minus when used with a constant value represent the concept that the values are either positive or negative.

print(-3 + +5)

Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of two bits is 1
^XORSets each bit to 1 if only one of two bits is 1
~NOTInverts all the bits
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall off
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

Membership Operators

Membership operators are used to test if a sequence is presented in an object:

OperatorDescriptionExample
inReturns True if a sequence with the specified value is present in the objecta in b
not inReturns True if a sequence with the specified value is not present in the objecta not in b

Identity Operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:

OperatorDescriptionExample
isReturns True if both variables are the same objecta is b
is notReturns True if both variables are not the same objecta is not b