Python Operators


Python Operators Introduction

Operators are the symbols that are used to perform some operations on one or more values. These values are known as the operands.

Python Operators

Types of Operators

In Python, we have the following types of operators:

Arithmetic Operators

OperatorNameExamplePrecedenceAssociativity
+Additiona + b3Left to Right
–Subtractiona-b3Left to Right
*Multiplicationa * b2Left to Right
/Divisiona / b2Left to Right
%Modulusa % b2Left to Right
//Floor Divisiona//b2Left to Right
**Exponentiala ** b1Right to Left

Assignment Operators

OperatorDescriptionExampleEquivalent
=Assignmenta = 2a = 2
+=Addition and Assignmenta += 2a = a + 2
-=Subtraction and Assignmenta -= 2a = a – 2
*=Multiplication and Assignmenta *= 2a = a * 2
/=Division and Assignmenta /= 2a = a / 2
%=Modulus and Assignmenta %= 2a = a % 2
//=Floor Division and assignmenta //= 2a = a // 2
**=Exponential and Assignmenta **= 2a = a ** 2
&=Bitwise AND and Assignmenta &= 2a = a & 2
|=BItwise OR and Assignmenta |= 2a = a | 2
^=BItwise XOR and Assignmenta ^= 2a = a ^ 2
>>=Bitwise Right Shift and Assignmenta >>= 2a = a >> 2
<<=Bitwise Left Shift and Assignmenta <<= 3a = a << 2

Comparison Operators

OperatorNameExampleDescription
==Equal toa == bReturns True if a is equal to b else False.
!=Not Equal toa != bReturns True if a is not equal to b else False.
>Greater thana > bReturns True if a is greater then b else False.
<Less thana < bReturns True if a is less then b else False.
>=Greater than or equal toa >= bReturns True if a is greater then equal to b else False.
<=Less than or equal toa <= b Returns True if a is less then equal to b else False.

Logical Operators

OperatorExampleDetails
anda > 5 and b < 20Returns True only if both statements are True, else False.
ora > 5 or b < 20Returns True if any statement is True, and returns False only if both statements are False.
notnot(a > 5 and b < 20)Returns True if expression inside parenthesis is False.

Identity Operators

OperatorExampleDescription
isa is bReturns True if both variables share same memory location, i.e. are same objects.
is nota is not bReturns True if both variables do not share the same memory location, i.e. are not the same objects.

Membership Operators

OperatorExampleDescription
ina in bReturns True if the variable is present in the sequence, else False.
not ina not in bReturns True if the variable is not present in the sequence, else False.

Bitwise Operators

OperatorNameDescription
&Bitwise ANDReturns binary 1 if both bits are 1, else 0.
|Bitwise ORReturns binary 1 if any of the bit is 1, else 0.
^Bitwise XORReturn binary 1 if only 1 bit is 1.
~Bitwise NOTConvert’s 1s to 0s and 0s to 1s.
<<Bitwise left shiftShifts bits left and fills new bits with 0.
>>Bitwise right shiftShifts bits right and fills new bits with 0.

Resources

Handwritten Notes

Python Operators and Arithmetic Operators

References:

  1. Python Operators.
  2. Examples on Operators.
  3. Python Operators and Expressions.