In python, operators can perform different operations like logical, comparison, arithmetic, etc., on the defined variables and values. For example, the addition (+) operator is useful to perform the addition operation on defined operands.
In python, operators are categorized into different groups. Those are
In python, arithmetic operators perform the necessary arithmetic calculations like addition, subtraction, division, etc., on the defined variables and values.
The following table lists the different types of arithmetic operators in python.
Operator | Name | Description | Example (a = 10, b = 3) |
---|---|---|---|
+ | Addition | It adds two operands. | a + b = 13 |
- | Subtraction | It subtracts two operands. | a - b = 7 |
* | Multiplication | It multiplies two operands. | a * b = 30 |
/ | Division | It divides numerator by de-numerator. | a / b = 3.3333 |
% | Modulus | It will return the remainder as a result. | a % b = 1 |
// | Floor division | It returns the division result by adjusting to the left. | a // b = 3 |
** | Exponent | It returns the exponentiation result (left operand will increase to power of right). | a ** b = 1000 |
Following is the example of using arithmetic operators in the python programming language.
If you observe the above example, we used arithmetic operators (+, -, *, /, %, **, //) to perform different operations on defined operands based on our requirements.
When you execute the above python program, you will get the result as shown below.
In python, comparison operators are useful to compare variables and values to determine whether the variables equal or not, etc., based on our requirements. The comparison operators will return either True
or False
based on the defined condition.
The following table lists the different types of comparison operators in python.
Operator | Name | Example (a = 6, b = 3) |
---|---|---|
== | Equal to | a == b (False) |
> | Greater than | a > b (True) |
< | Less than | a < b (False) |
>= | Greater than or Equal to | a >= b (True) |
<= | Less than or Equal to | a <= b (False) |
!= | Not Equal to | a != b (True) |
Following is the example of using comparison operators in the python programming language.
If you observe the above example, we used comparison operators (==, >, <, >=, <=, !=) to perform different operations on the defined operands based on our requirements.
When you execute the above python program, you will get the result as shown below.
In python, logical operators are useful to combine operands or conditional statements based on our requirements. The logical operators will work with Boolean expressions (True
or False
) and return Boolean values.
The following table lists the different types of logical operators in python.
Operator | Description | Example (a = True, b = False) |
---|---|---|
and | It returns True if both operands are True. | a and b (False) |
or | It returns True if either of the operands is True. | a or b (True) |
not | It will reverse the result. If the operand is True, it will return False and vice-versa. | not(a and b) (True) |
Following is the example of using logical operators in the python programming language.
If you observe the above example, we used logical operators (and, or, not) with conditional statements to evaluate the expressions based on our requirements.
When you execute the above python program, you will get the result as shown below.
In python, bitwise operators will work on bits, and these are useful to perform bit by bit operations such as Bitwise AND (&), Bitwise OR (|), Bitwise Exclusive OR (^), etc., on operands. We can perform bit-level operations on Boolean and integer data.
For example, we have two variables, a = 10, b = 20, and the binary format of these variables will be as shown below.
When we apply Bitwise OR (|) operator on these parameters, we will get the result as shown below.
The following table lists the different types of bitwise operators in python.
Operator | Name | Description | Example (a = 0, b = 1) |
---|---|---|---|
& | Bitwise AND | It compares each bit of the first operand with the corresponding bit of its second operand. If both bits are 1, then the result bit will be 1; otherwise, the result bit will be 0. | a & b (0) |
| | Bitwise OR | It compares each bit of the first operand with the corresponding bit of its second operand. If either of the bit is 1, then the result bit will be 1; otherwise, the result bit will be 0. | a | b (1) |
^ | Bitwise Exclusive OR (XOR) | It compares each bit of the first operand with the corresponding bit of its second operand. If one bit is 0 and the other bit is 1, then the result bit will be 1; otherwise, the result bit will be 0. | a ^ b (1) |
~ | Bitwise Not | It operates on only one operand, and it will invert each bit of operand. It will change bit 1 to 0 and vice versa. | ~(a) (1) |
<< | Bitwise Left Shift) | It shifts the number to the left based on the specified number of bits. The zeroes will be added to the least significant bits. | b << 2 (100) |
>> | Bitwise Right Shift | It shifts the number to the right based on the specified number of bits. The zeroes will be added to the least significant bits. | b >> 2 (001) |
Following is the example of using bitwise operators in the python programming language.
If you observe the above code, we used different bitwise operators (&, |, ^, ~, <<, >>) to perform different operations on defined operands.
When you execute the above python program, you will get the result as shown below.
In python, assignment operators can assign a new value to the operand, and these operators will work with only one operand.
The following table lists the different types of assignment operators in python.
Operator | Name | Example |
---|---|---|
= | Equal to | a = 10 |
+= | Addition Assignment | a += 10 Equals to a = a + 10 |
-= | Subtraction Assignment | a -= 10 Equals to a = a - 10 |
*= | Multiplication Assignment | a *= 10 Equals to a = a * 10 |
/= | Division Assignment | a /= 10 Equals to a = a / 10 |
%= | Modulo Assignment | a %= 10 Equals to a = a % 10 |
**= | Exponent Assignment | a **= 10 Equals to a = a ** 10 |
//= | Floor Division Assignment | a //= 10 Equals to a = a // 10 |
&= | Bitwise AND Assignment | a &= 10 Equals to a = a & 10 |
|= | Bitwise OR Assignment | a |= 10 Equals to a = a | 10 |
^= | Bitwise Exclusive OR Assignment | a ^= 10 Equals to a = a ^ 10 |
>>= | Right Shift Assignment | a >>= 2 Equals to a = a >> 2 |
<<= | Left Shift Assignment | a <<= 2 Equals to a = a << 2 |
In python, membership operators are useful to find whether the specified value exists in a sequence of objects or not.
The following table lists the different types of membership operators in python.
Operator | Description | Example |
---|---|---|
in | It will return True if the value is found in the sequence object. | a in b |
not in | It will return True if the value is not found in the sequence object. | a not in b |
Following is the example of using membership operators in the python programming language.
If you observe the above example, we used membership operators (in
, not in
) to verify whether the specified values exist in the defined objects (a, b) or not.
When you execute the above python program, you will get the result as shown below.
In python, identity operators are useful to compare whether the variables are pointing to the same object and the same memory location or not.
The following table lists the different types of identity operators in python.
Operator | Description | Example |
---|---|---|
is | It will return True if both variables refer to the same object. | a is b |
is not | It will return True if both variables do not refer to the same object. | a is not b |
Following is the example of using identity operators in the python programming language.
If you observe the above example, we created two variables (a, b) with the same content and another variable (c) by referring to the existing variable (a). We used identity operators (is
, is not
) to verify whether the defined variables (a, b, c) referring the same object or not.
When you execute the above python program, you will get the result as shown below.
If you observe the above result, a is b condition return False because the a is not the same object as b, even though they have the same content. Another condition a is c return True because a is the same object as c.