By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Operators can be defined as the constructs which can manipulate the value of operands. Consider the expression 9 - 4 = 5. Here, 9 and 4 are known as operands and - is known as operator. Types of Operator In Python language, following are the operators that are supported. - Arithmetic Operators - Assignment Operators - Bitwise Operators - Comparison (Relational) Operators - Identity Operators - Logical Operators - Membership Operators
Let us have a look on all above Python operators one by one.
Arithmetic Operators in Python Assume variable x holds 30 and variable y holds 30, then −
Operator
Description
Example
(+) Addition
It is a binary operator that adds values on either side of the operator.
x + y = 60
(-) Subtraction
It is a binary operator that subtracts right hand operand from left hand operand.
x – y = 0
(*) Multiplication
It is a binary operator that multiplies values on either side of the operator.
x * y = 900
(/) Division
It is a binary operator that divides left hand operand by right hand operand.
y / x = 1
(%) Modulus
It is a binary operator that divides left hand operand by right hand operand and returns remainder.
y % x = 0
() Exponent
It is a binary operator that performs exponential (power) calculation on operators.
xy =30 to the power 30
(//) Floor Division
It is a floor Division operator. The division of operands where the result is the quotient and the digits after the decimal point are removed. But in the case of the operands which are negative, the result is floored and rounded away from zero (towards negative infinity).
7//2 = 3 and 5.0//2.0 = 2.0, -11//3 = -4, -11.0//3 = -4.0
Assignment Operators in Python In the below example, let us assume variable x holds a value of 10 and variable y holds a value of 20. Variable z is the result operand.
=
It assigns values from right side operands to left side operand.
z = x + y assigns value of x + y into z which is equal to 30.
+= Add AND
It adds the value of right operand to the value of the left operand and assign the result to left operand.
z += x is equivalent to z = z+ x.
-= Subtract AND
It subtracts the value of right operand from the value of left operand and assign the result to left operand.
z -= x is equivalent to z = z – x.
*= Multiply AND
It multiplies the value of right operand with the value of left operand and assign the result to left operand.
z *= x is equivalent to z = z * x.
/= Divide AND
It divides the value of left operand with the value of right operand and assign the result to left operand.
z /= x is equivalent to z = z / x.
%=Modulus AND
It takes modulus on the values using two operands and assign the result to left operand.
z %= x is equivalent to z = z % x.
=Exponent AND
It performs exponential (power) calculation on the operators and assigns the result to the left operand.
z = x is equivalent to z = z x.
//= Floor Division
It performs floor division on the operators and assigns the result to the left operand.
z //= x is equivalent to z= z // x.
Bitwise Operators in Python Bitwise operator are operators that work on the bits and performs bit by bit operation. For example, if variable x = 60; and variable y = 13; then their equivalent binary format will be as follows. x = 0011 1100; y = 0000 1101. In the below example, binary AND, OR, XOR and Ones complement operations are demonstrated using Python bitwise operators.
Python language supports the following Bitwise operators.
& Binary AND
Binary AND operator copies a bit to the result if it is present in both operands.
(x & y) will give the result as 12. (0000 1100 in binary).
| Binary OR
Binary OR operator copies a bit if it is present in either operand.
(x | y) will give the result as 61. (0011 1101 in binary).
^ Binary XOR
Binary XOR operator copies the bit if it is set in one operand but not both.
(x ^ y) will give the result as 49. (0011 0001 in binary).
~ Binary Ones Complement
Binary Ones Complement operator is an unary and has the effect of 'flipping' bits.
(~x) will give the result as -61. (1100 0011 in binary). 2's complement form due to a signed binary number.
<< Binary Left Shift
In Binary Left Shift operator, the left operands value is moved left by the number of bits specified by the right operand.
x <<2 will give the result as 240 (1111 0000 in binary).
>> Binary Right Shift
In Binary Right Shift operator, the left operands value is moved right by the number of bits specified by the right operand.
x >>2 will give the result as 15 (0000 1111 in binary).
Comparison (Relational) Operators in Python Comparison operators in Python language compare the values on either sides of them and decide whether the relation among them is true or false. They are also known as relational operators.
In the below example, variable x holds 20 and variable y holds 30.
==
For this relational operator, if the values of two operands are equal, then the condition becomes true.
(x == y) is false as both have different values.
!=
For this relational operator, if values of two operands are not equal, then condition becomes true.
<>
(x <> y) is true. This is similar to (!=) operator.
>
For this relational operator, if the value of left operand is greater than the value of right operand, then condition becomes true.
(x > y) is false as the value of x is less than the value of y.
<
For this relational operator, if the value of left operand is less than the value of right operand, then condition becomes true.
(x < y) is true as the value of x is less than the value of y.
>=
For this relational operator, if the value of left operand is greater than or equal to the value of right operand, then condition becomes true.
(x >= y) is false as the value of x is neither greater nor equal to the value of y.
<=
For this relational operator, if the value of left operand is less than or equal to the value of right operand, then condition becomes true.
(x <= y) is true as the value of x is less than the value of y. Although they are not equal yet the result is true as the first condition is true.
Identity Operators in Python Python language has two identity operators (is and is not). Identity operators are operators that compare the memory locations of two objects.
Both of the identity operators are explained below.
Is
This identity operator evaluates to true if the variables on either side of the operator point to the same object (memory location reference). Otherwise it evaluates to false.
x is y, in this case the results is 1 if ref(x) equals ref(y).
is not
This identity operator evaluates to false if the variables on either side of the operator point to the same object (memory location reference). Otherwise it evaluates to true.
x is not y, in this case the result is 1 if ref(x) is not equal to ref(y).
Logical Operators in Python Python supports three logical operators and, or and not. Following are their description with example.
and (Logical AND)
If both the operands are true then condition becomes true.
If x and y are true then the condition becomes true else false.
or (Logical OR)
If any of the two operands are non-zero then condition becomes true.
If x or y are true, then the condition becomes true else false.
not (Logical NOT)
It is used to reverse the logical state of its operand.
If x is true, then Not (x) will be false and vice-versa.
Membership Operators in Python In Python language, the membership operators test for membership in a sequence, such as lists, tuples, or strings. Both of the membership operators are explained below.
In
This membership operator evaluates to true if it finds that a variable is the member in the specified sequence and otherwise it evaluates to false.
x in y, in this case the results is 1 if x is a member of sequence y.
not in
This membership operator evaluates to true if it does not find a variable is the member in the specified sequence and otherwise it evaluates to false.
x not in y, in this the result is 1 if x is not a member of sequence y.
Operators Precedence in Python Below table has a lists of all operators from highest precedence to lowest precedence in Python language.
Exponentiation (raise to the power)
~ + -
Ones complement, unary plus and minus.
* / % //
Multiply, divide, modulo and floor division.
+ -
Addition and subtraction.
>> <<
Right and left bitwise shift.
&
Bitwise 'AND'.
^ |
Bitwise exclusive `OR' and regular `OR'.
<= < > >=
Comparison operators.
<> == !=
Equality operators.
= %= /= //= -= += *= =
Assignment operators.
is, is not
Identity operators.
in, not in
Membership operators.
not, or, and
Logical operators.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.