Introduction:
Python operators are the topic of this article. One definition states that an operator is a symbol that carries out a certain operation between two operands. Operators are the building blocks of logic in a program written in a certain programming language. Every programming language has operators that can do many jobs. Like other languages, Python contains a number of operators, which are listed below.
Arithmetic Operators +,−,∗,/,∗∗,//,%: Arithmetic operators that are applied for a specific operation between two operands. The number of arithmetic operators is large. Along with the + (addition), – (subtraction), * (multiplication), / (divide), % (reminder), and // (floor division) operators, it also contains the exponent (**) operator.
Operator | Description |
+ (Addition) | Two operands are added with its help. For instance, if a = 10 and b = 10, then a plus b equals 20. |
– (Subtraction) | The second operand is subtracted from the first operand using this technique. The value is negative if the first operand is smaller than the second operand. For instance, if b = 5 and a = 20, then a – b = 15. |
/ (divide) | After dividing the first operand by the second operand, it yields the quotient. For instance, if b = 10 and a = 20, then a/b = 2.0. |
* (Multiplication) | It is applied when multiplying one operand by another. For instance, if b = 4 and a = 20, then a * b = 80. |
% (reminder) | Once the first operand has been divided by the second operand, the reminder is returned. For instance, if b = 10 and a = 20, then a%b = 0. |
** (Exponent) | It is an exponent operator since it determines the power of the first operand to the second operand. |
// (Floor division) | It gives the floor value of the quotient, which is the result of dividing the two operands. |
Comparison operator
The primary usage of comparison operators is in comparison. Comparison operators return a true or false Boolean value based on their comparison of the values of the two operands. The comparison operators ==,!=, \=, >=, >, and \ are some examples. We describe the operators’ works in the table below.
Operator | Description |
== | The condition is satisfied if the values of the two operands are equal. |
!= | The condition gets true if two operands have non-equal values. |
<= | If the first operand is less than or equal to the second operand, the condition is satisfied. |
>= | If the first operand is bigger than or equal to the second operand, the condition is satisfied. |
> | The condition is satisfied if the first operand is larger than the second operand. |
< | The condition is satisfied if the first operand is smaller than the second operand. |
Assignment Operators
The value of the right expression is assigned to the left operand using the assignment operators. Assignment operators include the following examples: =, +=, -=, *=, %=, **=, and //=. We describe the operators’ works in the table below.
Operator | Description |
= | It gives the left operand the value of the right expression.. |
+= | The left operand is given a new value by multiplying the value of the right operand by the value of the left operand. For instance, if a = 10 and b = 20, then a+ = b will equal a = a+ b, and a = 30 as a result. |
-= | It returns the updated value to the left operand after reducing the value of the left operand by the value of the right operand. As an illustration, if a = 20 and b = 10, then a- = b will equal a = a- b, and as a result, a = 10. |
*= | The adjusted value is then assigned back to the left operand after multiplying the value of the left operand by the value of the right operand. For instance, if a = 10 and b = 20, then a* = b will equal a = a* b, and a = 200 as a result. |
%= | The reminder is then assigned back to the left operand by dividing the value of the left operand by the value of the right operand. As an illustration, if a = 20 and b = 10, then a % = b will equal a = a % b, and as a result, a = 0. |
**= | For example, if a = 4 and b = 2, then a**=b will assign 4**2 = 16 to a. a**=b will be equal to a=a**b. |
//= | A//=b will assign 4//3 = 1 to an if a = 4 and b = 3. For example, a//=b will assign a = a// b. |
Logical Operators
Logical operators are often employed in the evaluation of expressions to reach conclusions. The logical operators and, or, and not are instances of them. If the first one in a logical AND situation is 0, it is independent of the second one. If the first one is 1, there is no relationship between it and the second in a logical OR situation. The following logical operators are supported by Python. We describe the functions of the logical operators in the table below.
Operator | Description |
and | If the phrase is true, then the condition will also be true. Both a and b must be true if they are the same terms, a and b. |
or | If any one of the phrases is true, then the condition will be true. If the two expressions are a and b, then if and is true and b is false, then either an or b must be true. |
not | Not (a) will be false if an expression an is true, and vice versa. |
Membership Operators
Python membership operators can be used to confirm if a value is a member of a data structure in Python. If the value is present in the data structure, the result is true; if not, it returns false.
Operator | Description |
in | In case the second operand does not contain the first operand, it is considered true (list, tuple, or dictionary). |
not in | The assessment is true (list, tuple, or dictionary) if the first operand is absent from the second operand. |
Identity Operators
Identity Operators in Python are used to compare two objects’ locations in memory. Two identity operators are present:
- is: This operator determines if two variables refer to the same memory object. If they do, it returns True; if not, it returns False.
- Is not: This operator determines if two variables refer to distinct memory items. If they don’t relate to the same object, it returns False; otherwise, it returns True.
Bitwise Operators
The bitwise operators process the values of the two operands one at a time. Bitwise OR (|), bitwise AND (&), bitwise XOR (^), negation (~), Left shift (\<), and Right shift (>>) are a few instances of bitwise operators. Think about the following scenario.
Operator | Description |
& (binary and) | If both bits in two operands at the same position are 1, a 1 is copied to the result. 0 is copied if not. |
| (binary or) | In the event that both bits are zero, the resultant bit will be zero; if not, it will be one. |
^ (binary xor) | The result bit will be 1 if the two bits vary, and 0 otherwise. |
~ (negation) | Since the operand’s bits are determined by their negations, a bit will always be 1 after it is set to 0, and vice versa. |
<< (left shift) | The leftward shift of the left operand’s value multiplies the amount of bits in the right operand. |
>> (right shift) | The amount of bits in the right operand is used to shift the left operand to the right. |
Operator Precedence (Priority Of Operators):
It is important to understand the order in which the operators are assessed since it indicates which operator should be taken into consideration first. The precedence tables for the Python operators are listed below.
Operator | Description |
** | Overall other operators applied in the expression, the exponent operator is given precedence. |
~ + – | the minus, unary plus, and negation. |
* / % // | The floor is divided, the modules are divided, and they are multiplied. |
+ – | Binary plus, and minus |
>> << | Make a left shift. and the right shift |
& | Binary and. |
^ | | Binary xor, and or |
<= < > >= | Operators for comparison (greater than, more then equal to, less than, less than equal to). |
<> == != | Equality operators. |
= %= /= //= -= +=*= **= | Assignment operators |
is is not | Identity operators |
in not in | Membership operators |
not or and | Logical operators |