Python Operators
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
- Logical Operators
- Assignment Operators
- Comparison Operators
- Membership Operators
- Identity Operators
- Bitwise Operators
Arithmetic Operators
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
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
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 |
Comparison Operators
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 Operator
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 Operator
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 Operator
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:
- Bitwise AND (&): The bitwise & operator is used to perform a bitwise AND operation. It is a binary operator. It is denoted by & symbol. This operator returns 1 if both of the bits are 1, else it gives 0.
- Bitwise OR (|): The bitwise | operator performs a bitwise inclusive OR operation. It is denoted by | symbol. This operator returns 1 if either of the bits is 1, else it gives 0.
- Bitwise XOR (^): The bitwise ^ operator performs a bitwise exclusive OR operation. It is a binary operator. It is denoted by ^ symbol. It gives 1 if corresponding bits are different, else it gives 0.
- Bitwise Compliment (~): The unary bitwise complement operator ~ inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte value of 8 bits whose bit pattern is "00000000" would change to "11111111". It is denoted by ~ symbol.
- Signed left shift operator (<< ): The signed left shift operator "<<" shifts a bit pattern to the left by the number of times specified by the right side of the operand. After the left shift, the empty space in the right is filled with 0. Left shifting a number by 1 is equavalent to multiplying it by 2.
- Signed right shift operator (>> ): The signed right shift operator ">>" shifts all bits to the right.
Example
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)
Example
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)
Example
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)
Example
a = 4 (in decimal) = 0100 (in binary)
Bitwise Compliment Operation of 4
~ 0100
1011 = 11 (in decimal)
Example
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.)
Example
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.)