Skip to content

Operators in Python

Operators are entities that are used to perform operations on variables and values. Python provides the following operator types

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Identity Operators
  6. Membership Operators
  7. Bitwise Operators

Arithmetic Operators

These are used with numeric values to perform numeric calculations on them.

OperatorNameExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Modulusx % y
**Exponentiationx ** y
//Floor Divisionx // y

Assignment Operators

The assignment operators are used to assign values to variables.

OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
//=x //= 3x = x // 3
**=x **= 3x = x ** 3
&=x &= 3x = x & 3
|=x |= 3x = x | 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x <<= 3

Comparison Operators

They are used to compare two values.

OperatorNameExample
==Equalx == y
!=Not Equalx !=
>Greater Thanx > y
<Lesser Thanx < y
>=Greater Than or Equal Tox >= y
<=Lesser Than or Equal Tox <= y

Logical Operators

Logical Operators are used to combine conditional statements.

OperatorDescriptionExample
andReturns true if both statements are truex < 5 and x < 10
orReturns true if any of the statements is truex < 5 or x < 4
notReverses the result - true is false and vice versanot (x < 5)

Identity Operators

Identity operators are used to compare objects if they are the actual same objects. These do not check for the similarity of content within the variable, but the actual memory location.

OperatorDescriptionExample
isReturns true if both variables are the same objectx is y
is notReturns true is both variables are different objectsx is not y

Membership Operators

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

OperatorDescriptionExample
inReturns true if a sequence with the specified value is present in the objectx in y
not inReturns true if a sequence with the specified value is not present in the objectx not in y

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 the two bits is 1
^XORSets each bit to 1 if only one of two bits is 1
~NOTInverts 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