Basic Operators in Python
Operators in Python:
Python Operators are the constructs that are used to manipulate the value of operands.
Types of operators in Python:
There are mainly 9 types of Python Operators available :
1. Arithmetic operators in Python:
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. Relational operators in Python:
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 in Python:
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:
In Python, there are only three logical operators are existing:
(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 are true then the whole condition is true.
(ii). Logical OR (||): Logical OR operator is used 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 in Python:
Unary Operators act as single operands. Python does not support prefix and postfix operators, Python supports unary minus operators. 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:
In Python, there are two shift operators that exist – shift left (<<) and shift right (>>). These operators a 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 in Python:
Bitwise operators perform operations at the bit level. In Python, there are four types of Bitwise Operators exist:
(i). Bitwise AND (&): Bitwise AND (&) operator, the bit in the first operand is ANDed with the corresponding bit in the second operand. The 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. The 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 in Python:
Membership Operators 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
- in Operator: This operator returns True if a variable is found in the specified sequence and Otherwise it returns False.
- not in Operator: This operator returns True if a variable is not found in the specified sequence and Otherwise, it returns False.
9. Identity Operators in Python:
Identity Operators are used to compare the memory locations of two objects. In Python, there are two types of Identity Operators – is Operator and is not Operator
- is Operator: This operator returns True if operands on both sides of the operator point to the same object Otherwise it returns False.
- is not Operator: This operator returns True if operands on both sides of the operator don’t point to the same object Otherwise, it returns False.
- not in Operator: This operator returns True if a variable is not found in the specified sequence and Otherwise, it returns False.