An operator is a construct which performs some operation: arithmetic or logical, on operands.
Operators in Python
Python has following operators:
- Arithmetic Operators
- Comparison/Relational Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Python Arithmetic Operators
Operator | Description | Example [x=33, y=10] |
---|---|---|
Addition (+) | Binary addition operator which adds both operands. | x+y=33+10=43 |
Subtraction (-) | Binary operator which subtracts right hand operand from left hand operand. | x–y=33-10=23 |
Multiplication (*) | Two Operands are multiplied | x*y=33*10=330 |
Division (/) | Binary operator which divides left hand operand by right hand operand | x/y=33/10=3.3 |
Modulus (%) | Binary operator divides left hand operand by right hand operand and returns remainder | 33%10=3 |
Exponent (**) | Performs exponential (power) calculation on operators | 33**10=> 33 to the power 10 |
Comparison/Relational Operators
Relational Operators return the relationship of operands.
Operator | Description | Example [x=33, y=10] |
---|---|---|
== | If the values of two operands are equal, returns true otherwise false. | (x==y) is false. |
!= | If values of two operands are not equal, returns true otherwise false. | |
> | If the value of left operand is greater than the value of right operand, returns true otherwise false. | (x>y) is true. |
< | If the value of left operand is less than the value of right operand, returns true otherwise false. | (x<y) is false. |
>= | If the value of left operand is greater than or equal to the value of right operand, returns true otherwise false. | (x>=y) is true. |
<= | If the value of left operand is less than or equal to the value of right operand, returns true otherwise false. | (x<=y) is false. |
Assignment Operators
Python supports composite assignment operators also.
Operator | Description | Example [x=33, y=10, z=5] |
---|---|---|
= | Binary operator, assigns value of right side operand to left side operand | z=x+y => z=33+10 => z=43 |
+= | Binary operator, adds right and left operand and assign the result to left operand | z+=y => z=z+y => z=5+10=15 => z=15 |
-= | Binary operator, subtracts right operand from the left operand and assign the result to left operand | z-=y => z=z-y => z=5-10=-5 => z=-5 |
*= | Binary operator, multiplies right and left operand and assign the result to left operand | z*=y => z=z*y => z=5*10=50 => z=50 |
/= | Binary operator, divides left operand with the right operand and assign the result to left operand | z/=y => z=z/y => z=5/10=0.5 => z=0.5 |
%= | Binary operator, devides left operand with right operand and assign remainder to left operand | z%=y => z=z%y => z=5%10=5 => z=5 |
**= | Binary operator, assign the result of left operand to the power right operand to left operand | z**=y => z=z**y => z=5**10 |
Bitwise Operators
Bitwise operators operate on binary operands bit by bit.
We consider x=33, y=10 for all the examples in the below table. First of all x and y are converted to binary as:
x=100001, y=001010
Operator | Description | Example |
---|---|---|
Binary AND (&) | 1&1=1, 1&0=0, 0&1=0, 0&0=0 | (x&y)=>33&10=0 |
Binary OR (|) | 1|1=1, 1|0=1, 0|1=1, 0|0=0 | (x|y)=>(33)|(10)=43(101011) |
Binary XOR (^) | It gives 1, if both operand’s bits are not same, otherwise gives 0 | (x^y)=>(33)^(10)=43(101011) |
Binary Negation (~) | It is unary. also called as ones complement. Inverts a bit. | (~x) = ~(33)=-34 |
Binary Left Shift (<<) | The left operands value is moved left by the number of bits specified by the right operand. | x<<2= (33<<2)=10000100=132 |
Binary Right Shift(>>) | The left operands value is moved right by the number of bits specified by the right operand. | x>>2=8 (001000) |
Logical Operators
In python there are three logical operators available.
Operator | Description | Example |
---|---|---|
Logical AND (and) | If both the operands are true/non-zero/false then condition becomes true. | (2 and 1)=1, (3 and 0)=0, ((2==3) and 1)= (False and 1)= False |
Logical OR (or) | If any of the two operands are 1/non-zero/true then condition becomes true. | (2 and 1)=1, (3 and 0)=1, ((2==3) and 1)= (False and 1)= 1 |
Logical NOT (not) | reverse the logical state of its operand. | not 0 = True, not 1 = False, not (2==3) = True |
Python Operators Precedence
Operators from highest precedence to lowest, same precedence in a row:
Operator | Description | |
---|---|---|
** | Exponentiation (power) | |
~, +, – | Complement, unary plus and minus | |
*, /, % | Multiply, divide, modulo | |
+, – | Addition and subtraction | |
>>, << | Right and left bitwise shift | |
& | Bitwise ‘AND’ | |
^, | | Bitwise XOR and OR | |
<=, <, >, >= | Comparison operators | |
==, != | Equality operators | |
=, %=, /=, -=, +=, *=, **= | Assignment operators | |
is is not | Identity operators |