Python Operators
Operators are the constructs that are used to manipulate the value of operands. In Python, there are 9 types of operators are available :
1. Arithmetic Operators :
Operator | Description | Example |
+ | Addition - Adds the operands | print(a+b) |
- | Subtraction - Subtracts the operands | print(a-b) |
* | Multiplication -Multiplies the operands | print(a*b) |
/ | Division - Divides the operands. | print(a/b) |
% | Modulus - The modulus operator returns the remainder. | print(a%b) |
// | Floor Division - Divides the operands and returns the quotient. It also removes the digits after the decimal point. If one operand is negative, the result is floored. | print(-10//3) Output: -3 |
** | Exponent: It performs exponential calculation. | print(a**b) |
2. Comparison or Relational Operators :
Operator | Description | Example |
(==) | Returns true if the two values are exactly equal | print(a==b) |
!= | Returns true if the two values are not equal | print(a!=b) |
> | Returns true if the value at the operand on the left side of the operator is greater than the value on the right side operator. | print(a>b) |
< | Returns true if the value at the operand on the right side of the operator is greater than the value on the left side operator. | print(a |
>= | Returns true if the value at the operand on the left side of the operator is either greater than or equal to the value on the right side operator. | print(a>=b) |
<= | Returns true if the value at the operand on the right side of the operator is either greater than or equal to the value on the left side operator. | print(a<=b) |
3. Assignment Operators :
Operator | Description | Example |
= | Assign value of the operand on the right side of the operator to the operand on the left. | a=b |
+= | Add and assign - Adds the operand on the left and right side of the operator and assigns the result to the operand on the left. | a+=b is same as a=a+b |
-= | Subtract and assign - Subtracts operand on the right from the operand on the left of the operator and assigns the result to the operand on the left. | a-=b is same as a=a-b |
*= | Multiply and assign - Multiplies the operands and assigns result to the operand on the left side of the operator. | a*=b is same as a=a*b |
/= | Divide and assign - Divides operand on the left side of the operator with the operand on its right. The division operator returns the quotient. | a/=b is same as a=a/b |
%= | Modulus and assign - Divides operand on the left side of the operator with the operator on its right. The modulus operator returns the remainder which is then assigned to the operand on the left of the operator. | a%=b is same as a=a%b |
//= | Floor Division - Divides the operands and returns the quotient. It also removes the digits after the decimal point. If one operand is negative, the result is floored and the result is assigned to the operand on the left of the operator. | a//=b is same as a=a//b |
**= | Exponent and assign : It performs exponential calculation and assigns the result in the left operand. | a**=b is same as a=a**b |
4. Logical Operators : In Python, there are only three logical operators are exists-
(i). Logical AND (&&) : Logical AND operator is used to simultaneously evaluate two conditions with relational operators. If conditions on both sides of the logical operator is true then the whole condition is true.
(ii). Logical OR (||) : Logical OR operator is used to to simultaneously evaluate two conditions with relational operators. If one or both conditions of the logical operator is true then the whole condition is true.
(iii). Logical NOT (!) : Logical NOT operator takes a single expression and negates the value of the expression. Logical NOT produces a zero if the expression evaluates to a non-zero value and produces 1 if the expression produces a zero.
Example :
a=5 b=!a;
5. Unary Operators : Unary Operators act as on single operands. Python does not support prefix and postfix operators, Python supports unary minus operator. This operator is slightly different from the arithmetic operator that operates on two operands and subtracts the second operand from the first operand. When an operand is preceded by a minus sign, the unary operator negates its value.
Example :
b=10 a=-(b);
6. Shift Operators : In Python, there are two shift operators are exists – shift left (<<) and shift right (>>). These operators are used to shift bits to the left or to the right. It has the following syntax :
operand op num
Example :
x=11001001 x<<4 gives result= 10011100
7. Bitwise Operators : Bitwise operators perform operations at the bit level. In Python, there are four types Bitwise Operators are exists:
(i). Bitwise AND (&) : Bitwise AND (&) operator, the bit in the first operand is ANDed with the corresponding bit in the second operand. Bitwise AND (&) operator compares each bit of its first operand with the corresponding bit of its second operand.
10101010 & 01010101 = 00000000
(ii). Bitwise OR (|) : Bitwise OR (|) the bit in the first operand is ORed with the corresponding bit in the second operand. Bitwise OR (|) operator compares each bit of its first operand with the corresponding bit of its second operand.
10101010 | 01010101 = 11111111
(iii). Bitwise XOR (^) : Bitwise XOR (^) the bit in the first operand is XORed with the corresponding bit in the second operand. Bitwise XOR (^) operator compares each bit of its first operand with the corresponding bit of its second operand.
10101010 ^ 01010101 = 11111111
(iv). Bitwise NOT (~) : This operator performs logical negation on each bit of the operand. It is a unary operation. Bitwise NOT (~) operator sets the bit to 1, if it was initially 0 and sets it to 0, if it was initially 1.
~11011011 = 00100100
8. Membership Operators : Membership Operators is used to test for membership in a sequence such as string, list or tuple. In Python, there are two types of Membership Operators – in Operator and not in Operator
9. Identity Operators : Identity Operators is used to compare the memory locations of two objects. In Python, there are two types of Identity Operators – is Operator and is not Operator