Operators are used to perform operations on variables and values
The variables or objects on either side of an operator can be referred as operands
Operators are special symbols that carry out computation which may be arithmetic, logical, assignment etc
Operand : it the value that the operator operates on
For eg, in a = 3 + 10
, +
is the operator and 3
and 10
are operands
Operators in Python can be divided into :
Arithmetic operators are used with numeric values to perform common mathematical operations:
Assume following variables are declared -
>>>a=10
>>>b=3
>>>c=50
Operator | Description | Example |
---|---|---|
+ | Addition - Adds values of either side of the operator | |
- | Subtraction - Subtracts right operand from left operand | |
* | Multiplication - Multiplies values of either side of the operator | |
/ | Division - Divides left operand by right operand | |
% | Modulus - Divides left operand by right operand and returns remainder | |
** | Exponent - Performs exponential (raise to power) calculation on operators | |
// | Floor Division - Divides left operand by right and returns the nearest integer whose value is less than or equal to the quotient | |
Assignment operators are used to assign value to the left operand by evaluating the expression on the right
Operator | Description | Example |
---|---|---|
= | Assigns values from right side operands to left side operand c = a + b assigns value of a + b into c | |
+= | Adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a | |
-= | Subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c - a | |
*= | Multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a | |
/= | Divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / a | |
%= | Takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a | |
**= | Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a | |
//= | It performs floor division on operators and assigns value to the left operand c //= a is equivalent to c = c // a | |
These operators compare the values on either sides of them and decide the relation among them
They are also called Relational operators
Assume following variables are declared -
>>>a=10
>>>b=3
>>>c=50
Operator | Description | Example |
---|---|---|
== | Returns true if values of the two operands are equal | |
!= | Returns true if values of the operands are not equal | |
> | Returns true if the value of left operand is greater than the value of right operand | |
< | Returns true if the value of left operand is less than the value of right | |
>= | Returns true if value of left operand is greater than or equal to the value of right operand | |
<= | Returns true if the value of left operand is less than or equal to the value of right operand |
|
Logical operators are used to combine conditional statements or expressions :
Assume following variables are declared -
>>>a=10
>>>b=3
>>>c=50
Operator | Description | Example |
---|---|---|
and | Returns True if both expressions are true | |
or | Returns True if one of the expressions is true | |
not | Returns the boolean opposite of the right operand Since strings and numbers except 0 are considered as | |
in
and not in
are the membership operators in Python
They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary)
Operator | Description | Example |
---|---|---|
in | Evaluates to true if a value/variable is found in the specified sequence |
|
not in | Evaluates to true if a value/variable is not found in the specified sequence | |
In case these are used for a dictionary object, then presence of key is tested, not the value
Although, presence can be tested in its list of values which can be retrieved by using dictionary.values() method
is
and is not
are the identity operators in Python
They are used to check if two values (or variables) are located on the same part of the memory
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object with the same memory location:
Operator | Description | Example |
---|---|---|
is | Evaluates to true if the variables on either side of the operator point to the same object | |
is not | Evaluates to false if the variables on either side of the operator point to the same object | |
In python, id()
function returns the object id of an object and can be used to check if two variables refer the same object
>>> a = "test"
>>> id(a)
140585882204680
>>> id("test")
140585882204680
Bitwise operator works on bits and performs bit by bit operation
Following examples use bin()
method to print binary value of an integer
bin(2)
returns '0b10', where binary value of 2 is 10
and '0b' in the string denotes that it is binary
Assume following variables are declared -
>>>a=17
>>>b=13
>>> bin(a)
'0b10001'
>>> bin(b)
'0b1101'
Following Bitwise operators are supported by Python
Operator | Description | Example |
---|---|---|
& | Binary And: compares corresponding bits of two integers and returns 1 if both are 1 else returns 0 | |
| | Binary Or: compares corresponding bits of two integers and returns 0 if both are 0 else returns 1 | |
^ | Binary XOR: compares corresponding bits of two integers and returns 0 if both are same | |
~ | Binary One's Complement: It is unary and returns the one's complement of a number | |
<< | Binary Left Shift: The left operand's value is moved left by the number of bits specified by the right operand | |
>> | Binary Right Shift: The left operands value is moved right by the number of bits specified by the right operand | |
Assume if a = 17; and b = 13; then they can be interpreted in binary format as -
a = 10001
b = 01101
a&b = 00001
a|b = 11101
a^b = 11100
~a = -10010
The following table lists all operators from highest precedence to lowest
Sr.No | Operator | Description |
---|---|---|
1 | | Exponentiation (raise to the power) |
2 | | One's complement |
3 | | Multiply, Divide, modulo and floor division |
4 | + - | Addition and subtraction |
5 | | Right and left bitwise shift |
6 | | Bitwise AND |
7 | | Bitwise exclusive `OR' and regular `OR' |
8 | | Comparison operators |
9 | | Equality operators |
10 | | Assignment operators |
11 | | Identity operators |
12 | | Membership operators |
13 | | Logical operators |