Python operators
- An operator is a symbol or a character in programming which takes two operands to perform an action or a computation.
- For example, in the expression "
4 + 5
" , "+
" is an operator It will perform sum of numbers "4" and "5" and returns result "9".
Types of operators in python
- Arithmetic Operators
- Comparison Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Arithmetic Operators
- consider the values a = 10 and b = 21
Operator | Name | Example | Output |
+ | Addition | print(a + b) | 31 |
- | Subtraction | print(a - b) | -11 |
* | Multiplication | print(a * b) | 210 |
/ | Division | print(b / a) | 2.1 |
% | Modulus | print(a % b) | 10 |
** | Exponentiation | print(2**5) | 32 |
// | Floor division | print(b // a) | 2 |
Comparison Operators
- consider the values a = 10 and b = 20.
- comparison operators always returns a boolean value either True or False
Operator | Description | Example | Output |
> | Greater than | print(b > a) | True |
< | Less than | print(b < a) | False |
>= | Greater than or equal | print(b >= a) | True |
<= | Less than or equal | print(b <= a) | False |
Assignment Operators
- Consider the value of b = 20
Operator | Description | Example |
= | Assigns the value | a = 100 |
+= | Performs addition & assignment | b += 23 equivalent to b = b + 23 |
-= | Performs subtraction & assignment | b -= 13 equivalent to b = b - 13 |
*= | Performs multiplication & assignment | b *= 3 equivalent to b = b * 3 |
/= | Performs division & assignment | b /= 13 equivalent to b = b / 13 |
%= | Performs modulus operation & assign | b %= 3 equivalent to b = b % 3 |
//= | Performs floor div & assignment | b //= 13 equivalent to b = b // 13 |
**= | Performs exp calculation & assignment | b **= 3 equivalent to b = b ** 3 |
Logical Operators
Operator | Description | Example |
and | Returns boolean value if given conditions are true ⇒ True Otherwise ⇒ False | a = 100 a > 90 and a < 110 ⇒ True a > 90 and a > 110 ⇒ False |
or | Returns boolean value At least one of the given conditions are true ⇒ True Otherwise ⇒ False | a = 100 a > 190 and a < 110 ⇒ True a > 190 and a > 110 ⇒ True |
not | It inverts the boolean value | not True ⇒ False not False ⇒ True |
Bitwise Operators
Operator | Description |
& | Operator copies a bit to the result if it exists in both operands |
| | It copies a bit if it exists in either operand. |
^ | It copies the bit if it is set in one operand but not both. |
~ | It is unary and has the effect of 'flipping' bits. |
>> | The left operands value is moved left by the number of bits specified by the right operand. |
<< | The left operands value is moved right by the number of bits specified by the right operand. |
Membership Operators
Operator | Description | Example |
in | Returns boolean value if it finds the var in given sequence returns true | a = 10 a in [1, 2, 10, 20] ⇒ True a in [1, 2, 110, 20] ⇒ False |
not in | Returns boolean value | a = 10 a not in [1, 2, 10, 20] ⇒ False a not in [1, 2, 110, 20] ⇒ True |
Identity Operators
Operator | Description | Example |
is | Returns boolean value True if both variables points to the same memory location in the RAM | a = 100; b = 100; c = 101; a is b ⇒ True a is c ⇒ False |
Is not | Returns boolean value | a = 100 b = 100 c = 101 a is not b ⇒ False a is not c ⇒ True |
Note: Try to remember all operators. we will use it very frequently.