Python Operators Introduction
Operators are the symbols that are used to perform some operations on one or more values. These values are known as the operands.
Types of Operators
In Python, we have the following types of operators:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
Arithmetic Operators
Operator | Name | Example | Precedence | Associativity |
---|---|---|---|---|
+ | Addition | a + b | 3 | Left to Right |
– | Subtraction | a-b | 3 | Left to Right |
* | Multiplication | a * b | 2 | Left to Right |
/ | Division | a / b | 2 | Left to Right |
% | Modulus | a % b | 2 | Left to Right |
// | Floor Division | a//b | 2 | Left to Right |
** | Exponential | a ** b | 1 | Right to Left |
Assignment Operators
Operator | Description | Example | Equivalent |
---|---|---|---|
= | Assignment | a = 2 | a = 2 |
+= | Addition and Assignment | a += 2 | a = a + 2 |
-= | Subtraction and Assignment | a -= 2 | a = a – 2 |
*= | Multiplication and Assignment | a *= 2 | a = a * 2 |
/= | Division and Assignment | a /= 2 | a = a / 2 |
%= | Modulus and Assignment | a %= 2 | a = a % 2 |
//= | Floor Division and assignment | a //= 2 | a = a // 2 |
**= | Exponential and Assignment | a **= 2 | a = a ** 2 |
&= | Bitwise AND and Assignment | a &= 2 | a = a & 2 |
|= | BItwise OR and Assignment | a |= 2 | a = a | 2 |
^= | BItwise XOR and Assignment | a ^= 2 | a = a ^ 2 |
>>= | Bitwise Right Shift and Assignment | a >>= 2 | a = a >> 2 |
<<= | Bitwise Left Shift and Assignment | a <<= 3 | a = a << 2 |
Comparison Operators
Operator | Name | Example | Description |
---|---|---|---|
== | Equal to | a == b | Returns True if a is equal to b else False. |
!= | Not Equal to | a != b | Returns True if a is not equal to b else False. |
> | Greater than | a > b | Returns True if a is greater then b else False. |
< | Less than | a < b | Returns True if a is less then b else False. |
>= | Greater than or equal to | a >= b | Returns True if a is greater then equal to b else False. |
<= | Less than or equal to | a <= b | Returns True if a is less then equal to b else False. |
Logical Operators
Operator | Example | Details |
---|---|---|
and | a > 5 and b < 20 | Returns True only if both statements are True, else False. |
or | a > 5 or b < 20 | Returns True if any statement is True, and returns False only if both statements are False. |
not | not(a > 5 and b < 20) | Returns True if expression inside parenthesis is False. |
Identity Operators
Operator | Example | Description |
---|---|---|
is | a is b | Returns True if both variables share same memory location, i.e. are same objects. |
is not | a is not b | Returns True if both variables do not share the same memory location, i.e. are not the same objects. |
Membership Operators
Operator | Example | Description |
---|---|---|
in | a in b | Returns True if the variable is present in the sequence, else False. |
not in | a not in b | Returns True if the variable is not present in the sequence, else False. |
Bitwise Operators
Operator | Name | Description |
---|---|---|
& | Bitwise AND | Returns binary 1 if both bits are 1, else 0. |
| | Bitwise OR | Returns binary 1 if any of the bit is 1, else 0. |
^ | Bitwise XOR | Return binary 1 if only 1 bit is 1. |
~ | Bitwise NOT | Convert’s 1s to 0s and 0s to 1s. |
<< | Bitwise left shift | Shifts bits left and fills new bits with 0. |
>> | Bitwise right shift | Shifts bits right and fills new bits with 0. |
Resources
Handwritten Notes
These are the Free Notes shared with the YouTube Video.
**Note: All links are Password Protected. For the PASSWORD, please follow the steps in the DESCRIPTION of the respective Video.
Chapter 2 - Operators
1 file(s) 5.73 MB
Linked video : https://youtu.be/ZSb6qsWnL_0