Linux

Python Operators

Python language supports the following types of operators.
  1. Python Arithmetic Operators.
  2. Python Comparison Operators
  3. Python Assignment Operators
  4. Python Bitwise Operators
  5. Python Logical Operators
  6. Python Membership Operators
  7. Python Identity Operators

Python Arithmetic Operators

Python Arithmetic Expressions comprises operands and operators.
OperatorDescription
**Exponent - Performs exponential (power) calculation on operators
*Multiplication
/Division
%Modulus
+Addition
-Subtraction

The below figure shows the precedence order
Python Operator 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 expression are of same data type, then resulting value is also that type. However 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 more general type. Below figure will clear you doubt.

Example 1 :

Python Operator Mixed-Mode conversion
Mixed-Mode conversion
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 Conversions

Type conversion function converts the data type of operand. See the below figure.

Example 2 :

Python Operator Type conversion
Type conversion
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.
OperatorDescription
==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 :

Python Comparison Operator
Comparison Operator
Let us discuss different case, expression a < b < = c is equivalent to a<b and 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 :

Comparison Operator
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 == and != operators always consider objects of different types to be unequal,

Example 5 :

Python Comparison between different data type
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.
OperatorDescription
=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 Assignment Operator
Assignment Operator


Python Bitwise Operators

Python supports digital bitwise operations. You may have done "And", "Or", Complementary operations.
OperatorDescription
|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 :

python Bitwise operator
Bitwise Operator
(~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 not
OperatorDescription
andReturn true if both right side and left side of operator are true.
orReturn true if any side, either right side or left side of operator are true.
notIf 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 bitwise logical operator
Bitwise Operator


Python Membership Operators

Python has two membership operator to test the membership in sequence such as list, tuple etc.
OperatorDescription
inReturn true if specified operand found in sequence.
not inReturn true if specified operand not found in sequence.

Example 9 :

python Membership Operator
Membership Operator


Python Identity Operators

OperatorDescription
isReturn true, if two variables point the same object and false otherwise.
is notReturn 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 :

id(x)
Identity Operator
The id() 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.
OperatorDescription
( )Parentheses
x[index],x[index1:index2],f(arg...),x.attributeSubscription, slicing, call, attribute reference
**Exponentiation
+x, -x, ~xPositive, 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
orBoolean OR
if - elseConditional expression
lambdaLambda expression
Operators which all have same precedence and chain evaluates from left to right except for comparisons and exponentiation. Comparisons can be chained arbitrarily.





admin