Operators
Dictionary
Operators : used to perform arithmetic and logical operations on data.
Operators enable us to manipulate and interpret data to produce useful outputs.
Python’s operators follow in-fix notation. In-fix operators appear between two operands.
Python divides the operators into the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication and division.
Below, we can find the basic arithmetic operators in order of precedence
Operator | Purpose |
---|---|
() |
Parentheses |
** |
Exponent |
% , * , / , // |
Modulo, Multiplication, Division, Floor Division |
+ , - |
Addition, Subtraction |
Addition
Adds two objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Result
1 2 3 4 |
|
Info
Using the +
operator between two strings/characters is a concatenation operation. You cannot add a string type with an integer type.
Subtraction
Gives the subtraction result of two numbers.
1 2 3 4 5 6 7 8 9 |
|
Result
1 2 3 |
|
Multiplication
For numeric types, it outputs the multiplication of two numbers. For sequence types, the sequence extends over by n
times (where n is numeric type).
1 2 3 4 5 6 7 8 9 10 |
|
Result
1 2 3 4 |
|
Division
Divide x by y.
1 2 3 4 5 6 7 |
|
Result
1 2 3 |
|
A result of division operation always results in a decimal number.
Bug
1.2222222222222223
can be considered as a bug in Python, but it is not. This has little to do with Python, and much more to do with how the underlying hardware handles floating-point numbers.
Check floating point section in the documentation for more such examples.
Floor Division
The result is floored to the nearest smaller integer.
1 2 3 4 5 6 7 |
|
Result
1 2 3 |
|
Tip
Unlike normal division, floor division between two integers results in an integer.
Modulo
Returns the remainder of the division.
1 2 3 4 5 6 7 8 |
|
Result
1 2 3 4 5 |
|
Exponent
Returns x to the power of y.
1 2 3 4 |
|
Result
1 2 |
|
Precedence
Whenever operators have equal precedence, the expression is computed from the left side.
1 2 3 4 5 6 |
|
Result
1 2 3 |
|
Parentheses
An expression that is enclosed inside parentheses will be computed first, regardless of operator precedence.
1 2 |
|
Result
1 2 |
|
Assignment Operators
This is a category of operators which is used to assign values to a variable.
Below, we can find the basic assignment operators
Operator | Purpose |
---|---|
= |
Assign |
+= |
Add and Assign |
-= |
Subtract and Assign |
*= |
Multiply and Assign |
/= |
Divide and Assign |
//= |
Floor Divide and Assign |
**= |
Raise Power and Assign |
%= |
Modulo and Assign |
|= |
Bitwise OR and Assign |
&= |
Bitwise AND and Assign |
^= |
Bitwise XOR and Assign |
>>= |
Bitwise Right-Shift and Assign |
<<= |
Bitwise Left-Shift and Assign |
Assigning Values
Let's go through few examples
1 2 3 4 5 6 7 8 |
|
Result
1 2 3 |
|
Copying the value of a variable to another variable.
1 2 3 4 5 6 7 |
|
Result
1 2 |
|
Other Operators
Let's perform operations using above-defined operators. We'll play a game. Let's say I ask you to think of a number, I'll tell you the steps/operations you need to perform and whatever your number is, the answer will always be 10.
Let's assume you thought of number 55,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Result
1 2 3 4 5 6 |
|
Voila, the answer is 10. You can try modulo and exponent operators. I'll explain bitwise operations in the Bitwise Operators section below.
Comparision Operators
Comparison operators can be used to compare values in mathematical terms. The result is either True or False according based on the condition.
Below, we can find the basic assignment operators,
Operator | Purpose |
---|---|
> |
Greater Than |
< |
Less Than |
>= |
Greater Than or Equal to |
<= |
Less Than or Equal to |
== |
Equal to |
!= |
Not Equal to |
is |
Equal to (Identity) |
is not |
Not Equal to (Identity) |
Info
is
and is not
are the identity operators both are used to check if two values are located on the same part of the memory.
Two variables that are equal do not indicate that they are identical.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Result
1 2 3 4 5 6 |
|
Note
We will understand is
and is not
utility later on.
Logical Operators
Logical operators are used to manipulate the logic of Boolean expressions.
Below, we can find the logical operators,
Operator | Purpose |
---|---|
and |
True if both operands are True |
or |
True if either of the operands is True |
not |
True if an operand is False |
1 2 3 4 5 6 7 8 9 10 11 |
|
Result
1 2 3 |
|
Info
In bit terms, the value of True corresponds to 1 and False corresponds to 0.
1 2 3 4 |
|
Result
1 2 |
|
Bitwise Operators
Bitwise operators are used to perform bitwise calculations on integer data type. The integers are first converted into binary and then operations are performed bit by bit. The result is returned in decimal format.
Tip
All data is made up of 0s and 1s known as bits.
Below, we can find the bitwise operators,
Operator | Purpose |
---|---|
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
~ |
Bitwise NOT |
>> |
Shift bits right |
<< |
Shift bits left |
Bitwise AND Operator
Returns 1 if both the bits are 1 else 0.
1 2 3 4 5 6 7 8 9 10 11 |
|
Result
1 |
|
Bitwise OR Operator
Returns 1 if either of the bit is 1 else 0.
1 2 3 4 5 6 7 8 9 10 11 |
|
Result
1 |
|
Bitwise XOR Operator
Returns 1 if one of the bits is 1 and the other is 0 else returns false.
1 2 3 4 5 6 7 8 9 10 11 |
|
Result
1 |
|
Bitwise NOT Operator
Returns one’s complement of the number.
1 2 3 4 5 6 7 8 9 |
|
Result
1 |
|
Bitwise Shift Operators
These operators are used to shift the bits of a number left side or right side. It has similar effects when we are multiplying or dividing the number by two.
Right-Shift Operator
Shifts the bits of the number to the right and fills 0 on voids left.
Tip
Similar effect as of dividing the number with some power of two.
1 2 3 4 5 6 7 8 9 10 11 |
|
Result
1 |
|
Left-Shift Operator
Shifts the bits of the number to the left and fills 0 on voids left.
Tip
Similar effect as of multiplying the number with some power of two.
1 2 3 4 5 6 7 8 9 10 |
|
Result
1 |
|
Precedence and Associativity
Operator precedence and associativity determine the priorities of the operator.
Operator | Purpose | Associativity |
---|---|---|
() |
Parentheses | left-to-right |
** |
Exponent | right-to-left |
% , * , / , // |
Modulo, Mutiplication, Division, Floor Division |
left-to-right |
+ , - |
Addition, Subtraction | left-to-right |
>> , << |
Bitwise shift right Bitwise shift left |
left-to-right |
< , <= > , >= |
Relational less than, less than or equal to greater than, greater than or equal to |
left-to-right |
== , != |
Relational equal to and not equal to | left-to-right |
Let us understand what is right-to-left associativity means,
1 2 3 4 5 6 7 8 |
|
Result
1 |
|
In the next section, we will start with Conditional Statements.