Operators in Python

To know the bitwise operation, you can read here.

1 . Arithmetic operators.

These operators are used to perform mathematical operations.

OperatorNameExample
+Addition3 + 2 = 5
Subtraction3 – 2 = 1
*Multiplication3 * 2 = 6
/Division3 / 2 = 1.5
%Modulus3 % 2 = 1
**Exponentiation3 ** 2 = 9
//Floor Division3 // 2 = 1

2. Assignment Operators

These operators are used to assign values to variables. It works from right to left.

OperatorExample
=x = 5 (Assigning value 5 to x)
+=x += 3 or x = x +3
-=x -= 5 or x = x – 5
*=x *= 5 or x = x * 5
/=x /= 5 or x = x / 5
%=x %= 5 or x = x % 5
//=x //= 5 or x = x // 5
**=x **= 5 or x = x ** 5
&=x &= 5 or x = x & 5
|=x |=5 or x = x | 5
^=x ^= 5 or x = x ^ 5
>>=x >>= 3 or x = x >> 5
<<=x <<= 5 or x = x << 5

3. Comparison Operator

Comparison operators are used to comparing two values.

OperatorNameExampleDescription
==Equalx == yReturn True if both the operands have the same value.
!= Not Equalx != yReturn True if both the operand have different values.
>Greater thanx > yReturn True if the value of x is greater than the value of y.
<Less thanx < yReturn True if the value of x is less than value of y.
>=Greater than or equal tox >= yReturn True if the value of x is greater than or the same as the value of y.
<=Less than or equal to x <= yReturn True if the value of x is lesser than or the same as the value of y.

4. Logical Operator

Logical operator are used to combine conditional statements like if else, elif.

OperatorExampleDescription
andx < 5 and x > 2Returns True if both statements are true
or x < 5 or x < 4Returns True if any one of the statements is true.
notnot(x < 5 and x < 10)Reverse the result, returns False if the result is true.

5. Membership Operators

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

OperatorExampleDescription
inx in yReturn True if a sequence with the specified value is present in the object.
not inx not in yReturns True if a sequence with the specified value is not present in the object

6. Bitwise operators

OperatorNameDescription
&ANDSet each bit to 1 if both bits are 1
|ORSets each bit to 1 if any one of the two bits is 1
^XORSets each bit to 1 if only two of its bits is 1
~NOT Inverse all the bits
<<Zero Fill Left ShiftShift left by pushing zeros in from 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

7. Identity Operators

Identity operator is used to compare to object. It will check whether they are actually the same object having the same memory location.

OperatorExampleDescription
isx is yReturns True if both x and y having same memory location
is notx is not y Returns True if both x and y have at different memory location

Please share and comment.

Contribute article and earn : contribute.articles@tech-bloggers.in

1 thought on “Operators in Python”

Leave a Comment

Your email address will not be published. Required fields are marked *