♦ 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.Operator | Working |
---|---|
+ | 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.Operator | Working |
---|---|
= | 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.Operator | Working |
---|---|
== | 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.Operator | Working |
---|---|
and | returns True if all variables/statements are True |
or | returns True if atleast one variables/statements are True |
not | returns 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.Operator | Working |
---|---|
is | returns true if both variables share the same memory-location(and also the value, ofcourse) |
is not | returns 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.Operator | Working |
---|---|
in | returns True if a particular value/sequence is present in an Object |
not in | returns 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.Operator | Working |
---|---|
& | 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: