Python Operator - Types of Operators in Python
Python Operator - Types of Operators in Python
Operators in Python
by DataFlair Team · Published December 21, 2017 · Updated March 16, 2019
Let us revise syntax in python before we start with the operator in Python. Also, it is
recommended to revise variable types in Python for proper programming.
a. Addition(+)
1. >>> 3+4
Output: 7
b. Subtraction(-)
Subtracts the value on the right from the one on the left.
1. >>> 3-4
Output: -1
c. Multiplication(*)
1. >>> 3*4
Output: 12
d. Division(/)
Divides the value on the left by the one on the right. Notice that division results in a
floating-point value.
1. >>> 3/4
Output: 0.75
e. Exponentiation(**)
1. >>> 3**4
Output: 81
We recommend you to learn about Python Operator Precedence
f. Floor Division(//)
Divides and returns the integer value of the quotient. It dumps the digits after the
decimal.
1. >>> 3//4
2. >>> 4//3
Output: 1
1. >>> 10//3
Output: 3
g. Modulus(%)
1. >>> 3%4
Output: 3
1. >>> 4%3
Output: 1
1. >>> 10%3
Output: 1
1. >>> 10.5%3
Output: 1.5
If you face any query in Python Operator with examples, ask us in the comment.
Relational Python Operator carries out the comparison between operands. They tell
us whether an operand is greater than the other, lesser, equal, or a combination of
those.
a. Less than(<)
This operator checks if the value on the left of the operator is lesser than the one on
the right.
1. >>> 3<4
Output: True
b. Greater than(>)
It checks if the value on the left of the operator is greater than the one on the right.
1. >>> 3>4
Output: False
It checks if the value on the left of the operator is lesser than or equal to the one on the
right.
1. >>> 7<=7
Output: True
It checks if the value on the left of the operator is greater than or equal to the one on
the right.
1. >>> 0>=0
Output: True
e. Equal to(= =)
This operator checks if the value on the left of the operator is equal to the one on the
right. 1 is equal to the Boolean value True, but 2 isn’t. Also, 0 is equal to False.
1. >>> 3==3.0
Output: True
1. >>> 1==True
Output: True
1. >>> 7==True
Output: False
1. >>> 0==False
Output: True
1. >>> 0.5==True
Output: False
It checks if the value on the left of the operator is not equal to the one on the right.
The Python operator <> does the same job, but has been abandoned in Python 3.
When the condition for a relative operator is fulfilled, it returns True. Otherwise, it
returns False. You can use this return value in a further statement or expression.
1. >>> 1!=-1.0
Output: False
1. >>> -1<>-1.0
a. Assign(=)
Assigns a value to the expression on the left. Notice that = = is used for comparing,
but = is used for assigning.
1. >>> a=7
2. >>> print(a)
Output: 7
Adds the values on either side and assigns it to the expression on the left. a+=10 is the
same as a=a+10.
1. >>> a+=2
2. >>> print(a)
Output: 9
Subtracts the value on the right from the value on the left. Then it assigns it to the
expression on the left.
1. >>> a-=2
2. >>> print(a)
Output: 7
Divides the value on the left by the one on the right. Then it assigns it to the
expression on the left.
1. >>> a/=7
2. >>> print(a)
Output: 1.0
Multiplies the values on either sides. Then it assigns it to the expression on the left.
Recommended Reading – Python Range Function
1. >>> a*=8
2. >>> print(a)
Output: 8.0
Performs modulus on the values on either side. Then it assigns it to the expression on
the left.
1. >>> a%=3
2. >>> print(a)
Output: 2.0
Performs exponentiation on the values on either side. Then assigns it to the expression
on the left.
1. >>> a**=5
2. >>> print(a)
Output: 32.0
Performs floor-division on the values on either side. Then assigns it to the expression
on the left.
1. >>> a//=3
2. >>> print(a)
Output: 10.0
a. and
If the conditions on both the sides of the operator are true, then the expression as a
whole is true.
1. >>> a=7>7 and 2>-1
2. >>> print(a)
Output: False
b. or
The expression is false only if both the statements around the operator are false.
Otherwise, it is true.
Output: True
‘and’ returns the first False value or the last value; ‘or’ returns the first True value or
the last value
1. >>> 7 and 0 or 5
Output: 5
c. not
This inverts the Boolean value of an expression. It converts True to False, and False
to True. As you can see below, the Boolean value for 0 is False. So, not inverts it to
True.
1. >>> a=not(0)
2. >>> print(a)
Output: True
a. in
This checks if a value is a member of a sequence. In our example, we see that the
string ‘fox’ does not belong to the list pets. But the string ‘cat’ belongs to it, so it
returns True. Also, the string ‘me’ is a substring to the string ‘disappointment’.
Therefore, it returns true.
1. >>> pets=[‘dog’,’cat’,’ferret’]
2. >>> ‘fox’ in pets
Output: False
Output: True
Output: True
b. not in
Output: True
These operators test if the two operands share an identity. We have two identity
operators- ‘is’ and ‘is not’.
a. is
If two operands have the same identity, it returns True. Otherwise, it returns False.
Here, 2 is not the same as 20, so it returns False. Also, ‘2’ and “2” are the same. The
difference in quotes does not make them different. So, it returns True.
1. >>> 2 is 20
Output: False
Output: True
b. is not
Output: True
a. Binary AND(&)
It performs bit by bit AND operation on the two values. Here, binary for 2 is 10, and
that for 3 is 11. &-ing them results in 10, which is binary for 2. Similarly, &-ing
011(3) and 100(4) results in 000(0).
1. >>> 2&3
Output: 2
1. >>> 3&4
Output: 0
b. Binary OR(|)
It performs bit by bit OR on the two values. Here, OR-ing 10(2) and 11(3) results in
11(3).
1. >>> 2|3
Output: 3
c. Binary XOR(^)
It performs bit by bit XOR(exclusive-OR) on the two values. Here, XOR-ing 10(2)
and 11(3) results in 01(1).
1. >>> 2^3
Output: 1
It returns the one’s complement of a number’s binary. It flips the bits. Binary for 2 is
00000010. Its one’s complement is 11111101. This is binary for -3. So, this results in
-3. Similarly, ~1 results in -2.
1. >>>~-3
Output: 2
e. Binary Left-Shift(<<)
It shifts the value of the left operand the number of places to the left that the right
operand specifies. Here, binary of 2 is 10. 2<<2 shifts it two places to the left. This
results in 1000, which is binary for 8.
1. >>> 2<<2
Output: 8
f. Binary Right-Shift(>>)
It shifts the value of the left operand the number of places to the right that the right
operand specifies. Here, binary of 3 is 11. 3>>2 shifts it two places to the right. This
results in 00, which is binary for 0. Similarly, 3>>1 shifts it one place to the right.
This results in 01, which is binary for 1.
1. >>> 3>>2
2. >>> 3>>1
Output: 1