Operator is a symbol that performs a specific operations between two operands. There are many kinds of operators, these are:
- Arithmetic operators
- Comparison operators
- Assignment operators
- Logical operators
- Bitwise operators
- Membership operators &
- Identity operators
Operator |
Description |
Addition (+) |
It is used to add two operands. For
example: A=10, B=20, A+B=30 |
Subtraction (-) |
It is used to subtract the second operand
from the first operand. For example: A=10, B=20, A-B=-10 |
Division (/) |
It is used to divide the first operand by
the second operand. If A=70, B=5, A/B=14 |
Multiplication (*) |
It is used to multiply both the operands.
A=70, B=5, A*b=350 |
Remainder (%) |
It returns the remainder after dividing
the first operand by the second operand. A=37, B=9, A%B=1 |
Exponent (**) |
It calculate the first operand’s power to
the second operand. |
Floor Division (//) |
It provides the quotient’s floor value,
which is obtained by dividing the two operands. |
a=99 b=6 print('Addition of two numbers:', a+b) print('Subtraction of two numbers:',a-b) print('Multiplication of two numbers:',a*b) print('Division of two numbers:',a/b) print('Remainder of two numbers:',a%b) print('Exponent of two numbers:',a**b) print('Floor division of two numbers:',a//b) |
---|
Addition of two numbers: 105 Subtraction of two numbers: 93 Multiplication of two numbers: 594 Division of two numbers: 16.5 Remainder of two numbers: 3 Exponent of two numbers: 941480149401 Floor division of two numbers: 16 |
---|
Operator |
Description |
== |
If the value of two operands is equal, then
the condition becomes true |
!= |
If the value of two operands is not equal,
then the condition becomes true |
<= |
If the value of first operand is smaller
than or equal to the second operand, the condition is true. |
>= |
If the value of first operand is greater
than or equal to the second operand, the condition is true. |
> |
If the first operand is greater than the
second operand, the condition is true |
< |
If the first operand is smaller than the
second operand, the condition is true |
a=30 b=4 print('Two numbers are equal.' ,a==b) print('Two numbers are not equal.' ,a!=b) print('a is less than or equal to b.',a<=b) print('a is greater than or equal to b.',a>=b) print('a is greater than b.',a>b) print('a is less than b.',a<b) |
---|
Two numbers are equal. False Two numbers are not equal. True a is less than or equal to b. False a is greater than or equal to b. True a is greater than b. True a is less than b. False |
---|
Operator |
Description |
= |
It assigns the value of the right
expression to the left operand. |
+= |
Add and Assign: Add right side operand
with left side operand and then assign to left operand. |
-= |
Subtract And: Subtract right operand from
left operand and then assign to left operand. True if both operands are equal. |
*= |
Multiply And: Multiply left operand with
right operand and then assign to the left operand. |
/= |
Divide And: Divide left operand with
right operand and then assign to the left operand. |
%= |
Modulus And: Takes modulus using left and
right operand and assign value to left operand. |
//= |
Divide And: Divide left operand with
right operand and then assign floor value to the left operand. |
**= |
Exponent And: Calculate exponent value using
operand and assign value to the left operand. |
&= |
Performs bitwise AND on operands and
assign value to left operand. |
I= |
Performs bitwise OR on operands and
assign value to left operand. |
˄= |
Performs bitwise xOR on operands and
assign value to left operand. |
>>= |
Performs bitwise right side on operands
and assign value to left operand. |
a=30 b=4 print('a=b:',a==b) print('a+=b:',a+b) print('a-=b:',a-b) print('a*=b:',a*b) print('a%=b:',a%b) print('a**=b:',a**b) print('a//=b:',a//b) |
---|
Output:
a=b: False a+=b: 34 a-=b: 26 a*=b: 120 a%=b: 2 a**=b: 810000 a//=b: 7 |
---|
Operator |
Name |
Description |
Syntax |
& |
Bitwise AND |
If both operands are bits 1, result bit 1 otherwise results bit 0. |
a&b |
| |
Bitwise OR |
If any of the two operands is bits 1, result bit 1 otherwise results bit 0. |
A|b |
~ |
Bitwise NOT |
Inverts individual bits |
~a |
^ |
Bitwise XOR |
If any of the two operands is bits 1, but not the both are 1, result bit 1
otherwise results bit 0. |
a^b |
>> |
Bitwise right shift |
The left operand’s value is moved towards
right by number of bits specified by the right operand. |
a>> |
<< |
Bitwise left shift |
The left operand’s value is moved towards
left by number of bits specified by the right operand. |
a<< |
a=6 b=7 print('a&b:',a&b) print('a|b:',a|b) print('a^b:',a^b) print('~a:',~a) print('a<<b:',a<<b) print('a>>b:',a>>b) |
---|
Output:
6 7 1 -7 768 0 |
---|
Operator |
Name |
Description |
Syntax |
and |
And |
Returns True if both statements are true. |
x>8 and x<16 |
or |
Or |
Returns True if any one statement is true. |
X<20 or x>15 |
not |
Not |
Reverse the result, return False if the result
is true. |
Not(x<5 and x<9) |
a=6 print('Are these expressions true:',a>6 and a<5) print('Is any of the two statements true:',a>5 or a>6) print('Each statement is true then return False and vice versa:',(not(a>2 and a>5))) |
---|
Output:
Are these expressions true: False Is any of the two statements true: True Each statement is true then return False and vice versa: False |
---|
Operator |
Description |
Syntax |
in |
Returns True if the given value is found in the
sequence. |
x=[“Rose”, “Lotus”] print(‘Is value present?’, ”Rose” in x) |
not in |
Returns True if the given value is not found in
the sequence. |
x=[“Rose”, “Lotus”] print(‘Is value not present?’, ”Rice” not in x) |
a=["pen","pencil"] print('Is value present?', "pencil" in a) print('Is value not present?', "notebook" not in a) |
---|
Output:
Is value present? True Is value not present? True |
---|
Operator |
Description |
Example |
Is |
Returns True if both variables are the
same object |
x is y |
Is not |
Returns True if both variables are not the
same object |
x is not y |
x=["apple", "banana"] y=["apple", "banana"] z=x print(x is z) print(x is y) print(x==z) |
---|
True False True |
---|