Python language supports the following types of operators.
- Python Arithmetic Operators.
- Python Comparison Operators
- Python Assignment Operators
- Python Bitwise Operators
- Python Logical Operators
- Python Membership Operators
- Python Identity Operators
Python Arithmetic Operators
Python Arithmetic Expressions comprises operands and operators.
|**||Exponent - Performs exponential (power) calculation on operators |
The below figure shows the precedence order
Python Precedence order
The above figure shows the Exponent has high precedence and Addition and Subtraction has lowest precedence.
If you want to change the order of evaluation use parentheses
If operands of the expression are of same data type, the resulting value is also that
type. However, the addition of two ints data type can produce long int.
Python Mixed-Mode Arithmetic
The calculation which entails both integer and floating-point number is called mixed-mode arithmetic.
When each operand is of a different type, the resulting value is of the more general type and float type is the more general type.
Below figure will clear you doubt.
Example 1 :
In above example 9/2.0, the less general type int (9) is temporarily and automatically converted into float (9.0) then calculation is performed.
Type conversion function converts the data type of operand. See the below figure.
Example 2 :
In above figure 4.77 converts into 4. If you want convert 4.77 into int use int() function.
If you want to convert into float use float() function.
Python Comparison Operators
Python supports comparison Operators. Comparison Operator returns true or false.
|==||To check the equality |
|<||Less than, Return true if left hand side of < is less than right hand side|
|>||Greater than, Return true if left hand side of > is greater than right hand side |
|<=||Less than or equal to, Return true if left hand side of <= is less than or equal to right hand side |
|>=||Greater than or equal to, Return true if left hand side of >= is greater than or equal to right hand side |
|!=||Not Equal to, Return true if Left hand side is not equal to right hand side |
|<>||Not Equal to, Return true if Left hand side is not equal to right hand side |
Example 3 :
Let us discuss different case, expression a < b < = c
, in this case and
operator is used, b
is evaluated only once and c
would be not evaluated if a < b
is found false.
Example 4 :
In above example 11<10
is evaluated first, it is false then next expression would not be evaluated.
If operands are of different types, if both are numbers, they are converted to a common type.
Otherwise, the ==
operators always consider objects of different types to be unequal,
Example 5 :
Comparison between different data type
- Numbers are compared arithmetically.
- Strings are compared alphabetical order using the numeric equivalents.
- Tuples and lists are compared alphabetical order using comparison of corresponding elements,
you will see detail of list, tuple dictionary in their corresponding page
Python Assignment Operators
In previous page Python - Type Variables
you have seen the assigment statement.
Now you will see some variant of assignment operator, mixed with arithmetic operators.
|=||x=y , y is assigned to x |
|+=||x+=y is equivalent to x=x+y |
|-=||x-=y is equivalent to x=x-y |
|*=||x*=y is equivalent to x=x*y |
|/=||x/=y is equivalent to x=x/y |
|**=||x**=y is equivalent to x=x**y |
Example 6 :
Python Bitwise Operators
Python supports digital bitwise operations. You may have done "And", "Or", Complementary operations.
||||Perform binary OR operation |
|&||Perform binary AND operation |
|~||Perform binary XOR operation |
|^||Perform binary one's Complement operation |
|<<||Left shift operator, left side operand bit is moved left by numeric number specified in right side |
|>>||Right shift operator, left side operand bit is moved right by numeric number specified in right side |
Example 7 :
(~x ) will give -241 which is in 2's complement form due to a signed binary number.
Python Logical Operators
Python supports logical operators and, or
|and||Return true if both right side and left side of operator are true. |
|or||Return true if any side, either right side or left side of operator are true. |
|not||If condition in not operator is true , not operator make it false. |
These only evaluate their second argument if needed for their outcome
Example 8 :
Python Membership Operators
Python has two membership operator to test the membership in sequence such as list, tuple etc.
|in||Return true if specified operand found in sequence. |
|not in||Return true if specified operand not found in sequence. |
Example 9 :
Python Identity Operators
|is||Return true, if two variables point the same object and false otherwise. |
|is not||Return false, if two variables point the same object and true otherwise. |
The below figure shows value can be same, but id is different. Return true if Id is same.
Example 10 :
function return the "identity" of an object.
This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. it is like memory addresses in C language.
Python Operator Precedence
Highest precedence at top, lowest at bottom.
|( )||Parentheses |
|x[index],x[index1:index2],f(arg...),x.attribute||Subscription, slicing, call, attribute reference |
|+x, -x, ~x||Positive, negative, bitwise NOT |
|*, /, %||Multiplication, division, remainder |
|+, -||Addition and subtraction |
|<<, >>||Shifts |
|& ||Bitwise AND |
|^ ||Bitwise XOR |
|| ||Bitwise OR |
|in, not in, is, is not, <, <=, >, >=, !=, ==||Comparisons, including membership tests and identity tests |
|not x ||Boolean NOT |
|and ||Boolean AND |
|or||Boolean OR |
|if - else||Conditional expression |
|lambda||Lambda expression |
Operators which all have same precedence and chain evaluates from left to right except for comparisons and exponentiation. Comparisons can be chained arbitrarily.