Operators are special symbols in Python that carry out computation. The value that the operator operates on is called the operand.
Here, + is the operator that performs addition. 10 and 5 are the operands and 15 is the output of the operation.
Python has different type of operators which are classified below.
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.
Arithmetic operators in Python:
|+||Add two operands or unary plus.||x + y +2|
|-||Subtract right operand from the left or unary minus||x - y-2|
|*||Multiply two operands||x * y|
|/||Divide left operand by the right one (always results into float)||x / y|
|%||Modulus - remainder of the division of left operand by the right||x % y (remainder of x/y)|
|//||Floor division - division that results into whole number adjusted to the left in the number line||x // y|
|**||Exponent - left operand raised to the power of right||x**y (x to the power y)|
x + y = 10 x - y = 4 x * y = 21 x / y = 2.3333333333333335 x // y = 2 x % y = 1 x ** y = 343
Comparison operators are used to compare values. It either returns True or False according to the condition.
|>||Greater that - True if left operand is greater than the ri||x > y|
|<||Less that - True if left operand is less than the right||x < y|
|==||Equal to - True if both operands are equal||x == y|
|!=||Not equal to - True if operands are not equal||x != y|
|>=||Greater than or equal to - True if left operand is greater than or equal to the right||x >= y|
|<=||Less than or equal to - True if left operand is less than or equal to the right||x <= y|
x > y is False x < y is True x == y is False x != y is True x >= y is False x <= y is True
Logical operators are the and, or, not operators.
Logical operators perform Logical AND, Logical OR and Logical NOT operations.
|and||True if both the operands are true||x and y|
|or||True if either of the operands is true||x or y|
|not||True if operand is false (complements the operand)||not x|
x and y is False x or y is True not x is False
Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.
For example, 2 is 10 in binary and 7 is 111.
In Table: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
Bitwise operators in Python
|&||Bitwise AND||x& y = 0 (0000 0000)|
||||Bitwise OR||x | y = 14 (0000 1110)|
|~||Bitwise NOT||~x = -11 (1111 0101)|
|^||Bitwise XOR||x ^ y = 14 (0000 1110)|
|>>||Bitwise right shift||x>> 2 = 2 (0000 0010)|
|<<||Bitwise left shift||x<< 2 = 40 (0010 1000)|
x& y= 0 x | y= 14 ~x= -11 x ^ y= 14 x>> 2= 2 x<< 2= 40
Assignment operators are used in Python to assign values to variables.
a = 10 is a simple assignment operator that assigns the value 10 on the right to the variable a on the left.
There are various compound operators in Python like a += 10 that adds to the variable and later assigns the same. It is equivalent to a = a + 10.
Assignment operators in Python
|=||x = 5||x = 5|
|+=||x += 5||x = x + 5|
|-=||x -= 5||x = x - 5|
|*=||x *= 5||x = x * 5|
|/=||x /= 5||x = x / 5|
|%=||x %= 5||x = x % 5|
|//=||x //= 5||x = x // 5|
|**=||x **= 5||x = x ** 5|
|&=||x &= 5||x = x & 5|
||=||x |= 5||x = x | 5|
|^=||x ^= 5||x = x ^ 5|
|>>=||x >>= 5||x = x >> 5|
|<<=||x <<= 5||x = x << 5|
Python language offers some special type of operators like the identity operator or the membership operator.
They are described below with examples.
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. Two variables that are equal does not imply that they are identical.
|is||True if the operands are identical (refer to the same object)||x is True|
|is not||True if the operands are not identical (do not refer to the same object)||x is not True|
False True False
Here, we see that x1 and y1 are integers of same values, so they are equal as well as identical. Same is the case with x2 and y2 (strings). But x3 and y3 are list. They are equal but not identical. Since list are mutable (can be changed), interpreter locates them separately in memory although they are equal.
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).
|in||True if value/variable is found in the sequence||5 in x|
|not in||True if value/variable is not found in the sequence||5 not in x|
False True False
Here, ' Program ' is in x but ' program' is not present in x, since Python is case sensitive.