Operators are special symbols like +, −, *, /, %, etc, which are used to perform operations on operands (values).
These are the basic list of operators in Python:
Arithmetic operators are used to perform calculations, such as addition, subtraction, multiplication, and division.
The following table shows the lists of arithmetic operators supported by Python:
Operators | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds values. |
a = 4 b = 6 a + b = 10 |
- | Substraction | Substracts values. |
a = 10 b = 6 a - b = 4 |
* | Multiplication | Multiplies values. |
a = 2 b = 6 a * b = 12 |
/ | Division | Divides values. |
a = 10 b = 2 a / b = 5.0 |
% | Modulus | Returns remainder after division. |
a = 10 b = 2 a % b = 0 |
** | Exponentiation | Perform exponential operation. |
a = 10 b = 2 a ** b = 100 |
// | Floor Division | Returns floor values after division. |
a = 51 b = 2 a // b = 51 |
Logical operators are used to determine the logic between variables(values).
The following table shows the lists of logical operators supported by Python:
Operators | Name | Description | Example |
---|---|---|---|
and | Logical AND | Returns true, if and only if both statements are true. | 175 > 156 and 253 > 130 |
or | Logical OR | Returns true, if one of the given statements is true. | 147 < 112 or 350> 330 |
not | Logical NOT | Change the result to the opposite, returns true if the result is false. | not(2 > 5) |
Assignment operators are used to assign a value to a variable on the left. The sign used for assignment operator is =.
The following table shows the lists of assignment operators supported by Python:
Operators | Description | Example |
---|---|---|
= | Assign value from the right to a variable on the left. | a = 10 |
+= | Add right operand with the left operand and assign result to the left operand. | a += b |
-= | Substract the right operand from the left operand and assign the result to the left operand. | a -= b |
*= | Multiplies the operand on the right with the operand on the left and assign the result to the left operand. | a *= b |
/= | Divide the left operand by the right operand and assign the result to the left operand. | a /= b |
%= | Modulus and assign operator. It calulates the remainder out of two operand and assign the result to the left operand. | a %= b |
//= | Divide the left operand by the right operand and assign the floor value result to the left operand. | a //= b |
**= | Perform exponential operation by the right operand and assign the result to the left operand. | a **= b |
&= | Bitwise AND assignment operator. | a &= b |
^= | Bitwise exclusive OR and assignment operator. | a ^= b |
|= | Bitwise inclusive OR and assignment operator. | a |= b |
<<= | Left shift and assignment operator. | a <<= b |
>>= | Right shift and assignment operator. | a >>= b |
The Comparison operators are used to compare two values.
The following table shows the lists of comparision operators supported by Python:
Operators | Name | Description | Example |
---|---|---|---|
== | Equal | Compares and return True if the left operand is equal to the right operand. | a == 10 |
!= | Not equal | Compares and return True if the left operand is not equal to the right operand. | a != 10 |
> | Greater than | Compares and return True if the left operand is greater than the right operand. | a > 10 |
<< /td> | Less than | Compares and return True if the left operand is lesser than the right operand. | a < 10 |
>= | Greater than or equal to | Compares and return True if the left operand is greater than or equal to the right operand. | a >= 10 |
<= | Less than or equal to | Compares and return True if the left operand is lesser than or equal to the right operand. | a <= 10 |
Membership operators are used used to test the membership in a sequence.
The following table shows the lists of membership operators supported by Python:
Operators | Description | Example |
---|---|---|
in | Return True if there is a value in the specified sequence. | a in b |
not in | Return True if there is no value in the specified sequence. | a not in b |
Identity operators are used to compare objects if they are the same object and share the same memory location.
The following table shows the list of identity operators supported by Python:
Operators | Description | Example |
---|---|---|
is | Return True if the compared objects are the same object. | a is b |
is not | Return True if the compared objects are not the same object. | a is not b |
Bitwise operators are used to perform manipulation on individual bits of integer types, long, int, short, byte and char values. Bitwise operations are generally faster than division, multiplication, and sometimes faster than addition.
Following are the lists of Bitwise operators supported by Python:
a = 4 (in decimal) = 0100 (in binary)
b = 7 (in decimal) = 0111 (in binary)
Bitwise AND Operation of 4 and 7
0100
& 0111
0100 = 4 (in decimal)
a = 4 (in decimal) = 0100 (in binary)
b = 7 (in decimal) = 0111 (in binary)
Bitwise OR Operation of 4 and 7
0100
| 0111
0111 = 7 (in decimal)
a = 4 (in decimal) = 0100 (in binary)
b = 7 (in decimal) = 0111 (in binary)
Bitwise XOR Operation of 4 and 7
0100
^ 0111
0011 = 3 (in decimal)
a = 4 (in decimal) = 0100 (in binary)
Bitwise Compliment Operation of 4
~ 0100
1011 = 11 (in decimal)
14 (decimal value) = 00001110 (binary value)
(14 << 2) Move 14 by 2 places to the left
00001110
00111000 = 56 (in decimal) (After moving 00001110 by 2 places to the left.)
14 (decimal value) = 00001110 (binary value)
(14 >> 2) Move 14 by 2 places to the right
00001110
00000011 = 3 (in decimal) (After moving 00001110 by 2 places to the right.)