Content
Operators in Python
Python has arithmetic, relational, logical, bit by bit, assignment, membership, and identity operators
Arithmetic operators
Python supplies basic operations with the only additions of the remaining operators, whole quotient and exponentiation
Arithmetic operators | ||
Operator | Operation | Description |
+ | Addition | Add two operands |
– | Subtraction |
Subtract the value of the right operand from the left operand Used on a single operand, it changes the sign |
* | Multiplication | Multiplication of two operands |
/ | Division | Divide the operand from the left to the right (the result is always a float) |
% | Rest | Gets the rest of dividing the operand on the left by the one on the right |
// | Integer quotient | Gets the integer quotient of dividing the operand from the left to the right operand |
** | Exponentiation | The result is the left operand raised to the power of the right operand |
Note In Python, using the + operator applied to strings concatenates both strings into a single
Relational operators
Are used typically in the conditional expression
The relational operators return boolean values
The operands can be numerical or strings
Relational operators | ||
Operator | Operation | Description |
> | Greater than |
True if the operand on the left is strictly greater than that on the right False otherwise |
< | Less than |
True if the operand on the left is strictly less than the one on the right False otherwise |
> = | Greater than or equal to |
True whether the operand on the left is greater than or equal to that of the right False otherwise |
< = | Less than or equal to |
True if the operand on the left is less than or equal to that of the right False otherwise |
! = | Other than that |
True if the operands are different False otherwise |
= = | Like |
True if the operand on the left is the same as the one on the right False otherwise |
Objects of different types, except numeric types, are never compared the same
The operator == it is always defined, but for some types of objects (for example, class objects) it is equivalent to is
Non identical instances of a class are typically compared as non equal unless the class defines the method __eq__()
Instances of a class cannot be sorted with respect to other instances of the same class or other object types, unless the class defines the methods __lt__() and __gt__()
Logical operators
The logical operands is related to the relational as they are normally the operands used are the result of expressions in relational
The resulting values are boolean
Logical operators | |
Operator | Description |
AND | True if both are True |
OR | True if one of them is True |
NOT | It was True moves on to False and vice versa |
Bitwise operators
The way of working of these operators is to convert to the binary operands and then operate with them bitwise
Bitwise operators | Operator | Operation | Description |
& | AND | Change the bit to 1 if both were 1 |
| | OR | Change the bit to 1 if one of them was 1 |
^ | XOR | Change the bit to 1 if one of them was 1, but not both |
~ | NOT |
Change the bit to 1 if it was 0 Change the bit to 0 if it was 1 |
<< | Propagation to the left | Shift the value to the left by entering zeros, if it goes out of range, values are lost |
>> | Spread to the right | Moves the value to the right entering by the left, the sign bit and eliminating the values that are out by the right |
Note Propagation operators take two operands: the first is the variable to propagate and the second is the number of positions to propagate
Assignment operators
The assignment is also an operator that returns the variable modified
The mapping operator in Python is =
The assignment operators shown below are but abbreviations that make expressions more comfortable and simple, even if they are sometimes more unreadable
Assignment operators | ||
Operator | Expression | Equivalence |
= |
A = B = C D = ‘Text’ |
A = C B = C D = ‘Text’ |
+ = | A + = 4 | A = A + 4 |
– = | A – = 3 * B | A = A – (3 * B) |
* = | A * = 2 | A = A * 2 |
** = | A ** = 2 | A = A ** 2 |
/ = | A / = 35 + B | A = A / (35 + B) |
% = | A % = B | A = A % B |
// = | A // = B | A = A // B |
>> = | A >> = 1 | A = A >> 1 |
<< = | A << = B | A = A << B |
& = | A & = (C + = 3) | C = C +3 A = A & C |
^ = | A ^ = 2 | A = A ^ 2 |
| = | A | = C | A = A | C |
Membership operators
Membership operators are used to check whether a value or variable is in a sequence (list, tuple, dict, set or str)
Membership operators | ||
Operator | Expression | Equivalence |
in | Included |
Returns True if the value is in a sequence False otherwise |
not in | Not included |
Returns True if the value is not in a sequence False otherwise |
Identity operators
These operators will be used to check whether or not two variables are the same object
Membership operators | ||
Operator | Expression | Equivalence |
is | Equal objects |
Returns True if both operands refer to the same object False otherwise |
is not | Different objects |
Returns True if both operands do not refer to the same object False otherwise |
Preference
The operator precedence will determine the order in which they are running in a given expression
Using parentheses will check that the operations are carried out according to us we want to
In Python, the preference for operators from highest to lowest is as follows:
Preference | |||||||||||||
Parenthesis | ( | ) | |||||||||||
Exponentiation | ** | ||||||||||||
Unaries | ~ | + | – | ||||||||||
Mul / Div / Rest / Div integer | * | / | % | // | |||||||||
Addition / Subtract | + | – | |||||||||||
Spread | << | >> | |||||||||||
Bit by bit | & | ||||||||||||
Bit by bit | ^ | | | |||||||||||
Relational | < | < = | > | > = | |||||||||
Equality | = = | != | |||||||||||
Assignment | = | + = | – = | * = | * * = | / = | // = | % = | & = | | = | ^ = | >> = | << = |
Identity | is | is not | |||||||||||
Membership | in | not in |