codeGK
Code it ALL
Python Operators - CodeGK
♦ Python Operators

♣ About Python Operators

Operators in Python are fundamentally used to perform operations on Variables and Values.
Various types of Operators exist in Python.
  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Identity Operators
  • Membership Operators
  • Bitwise Operators

♣ Example1: Arithmetic Operators in Python

Arithmetic Operations in Python are used to perform Mathematical Operations.
Table Info
OperatorWorking
+Addition
-Subtraction
*Multiplication
/Division
%Modulo
**Exponentiation
//Flooring
Example
Input:
Output:

♣ Example2: Assignment Operators in Python

Assignment Operations in Python are used for assignment of (values)/(variable values) to variables.
Table Info
OperatorWorking
=Data Assignment
+=Addition Increment
-=Subtraction Decrement
*=Multiplication Increment
/=Division Decrement
%=Modulo Assignment
//=Flooring Assignment
**=Exponentiation Assignment
&=AND Assignment
|=OR Assignment
^=XOR Assignment
~=NOT Assignment
>>=Zero fill left shift Assignment
<<=Signed right shift Assignment
Example
Input:
Output:

♣ Example3: Comparison Operators in Python

Comparison Operators in Python are used to compare two Values/Variables.
Table Info
OperatorWorking
==Equal to
!=Not Equal to
> Greater than
<Less than
=>Greater than or Equal to
=<Less than or Equal to
Example
Input:
Output:

♣ Example4: Logical Operators in Python

Logical Operators in Python are used to evaluate conditional statements and produce Boolean output.
Table Info
OperatorWorking
andreturns True if all variables/statements are True
orreturns True if atleast one variables/statements are True
notreturns the reverse of Boolean value
Example
Input:
Output:

♣ Example5: Identity Operators in Python

Identity Operators in Python are used to check whether a one or more variables share the same memory location, that is same value is assigned to multiple variables.
Table Info
OperatorWorking
isreturns true if both variables share the same memory-location(and also the value, ofcourse)
is notreturns true if both variables do not share the same memory-location(but may possibly have same value)
Example
Input:
Output:

♣ Example6: Membership Operators in Python

Membership Operators in Python are used to check if a particular value/sequence is present in an Object.
Table Info
OperatorWorking
inreturns True if a particular value/sequence is present in an Object
not inreturns True if a particular value/sequence is not present in an Object
Example
Input:
Output:

♣ Example7: Bitwise Operators in Python

Bitwise Operators in Python are used to compare numbers which are represented in Binary format.
Table Info
OperatorWorking
&Outputs 1 if both bit's value is 1
|Outputs 1 if atleast one of bit's value is 1
^Outputs 1 if only one of bit's value is 1
~Reverses the bit value
<<Left Shift of 0s
>>Right Shift of 0s
Example
Input:
Output:
Prev_LessonNext_Lesson