Wednesday, 20 December 2017

Pyhton :Operators


Python Operators:
Operators are the symbols which perform the operation on the some values. These values are known as operands. Python have following operators –

Arithmetic Operators
Relational Operators
Assignment Operators
Logical Operators
Membership Operators
Identity Operators
Bitwise Operators
Arithmetic Operators:

OperatorNameExampleResult
+Additionx+ySum of x and y.
-Subtractionx-yDifference of x and y.
*Multiplicationx*yProduct of x and y.
/Divisionx/yQuotient of x and y.
%Modulusx%yRemainder of x divided by y.
**Exponentx**yx**y will give x to the power y
//Floor Divisionx/ yThe division of operands where the result is the quotient in which the digits after the decimal point are removed.
Relational Operators :  It is also known as comparison operator because it compares the values. After comparison it returns the Boolean value i.e. either true or false.

OperatorNameExampleResult
==Equalx==yTrue if x is exactly equal to y.
!=Not equalx!=yTrue if x is exactly not equal to y.
>Greater thanx>yTrue if x (left-hand argument) is greater than y (right-hand argument).
<Less thanx<yTrue if x (left-hand argument) is less than y (right-hand argument).
>=Greater than or equal tox>=yTrue if x (left-hand argument) is greater than or equal to y (left-hand argument).
<=Less than or equal tox<=yTrue if x (left-hand argument) is less than or equal to y (right-hand argument).
Assignment Operators:

OperatorShorthandExpressionDescription
+=x+=yx = x + yAdds 2 numbers and assigns the result to left operand.
-=x-= yx = x -ySubtracts 2 numbers and assigns the result to left operand.
*=x*= yx = x*yMultiplies 2 numbers and assigns the result to left operand.
/=x/= yx = x/yDivides 2 numbers and assigns the result to left operand.
%=x%= yx = x%yComputes the modulus of 2 numbers and assigns the result to left operand.
**=x**=yx = x**yPerforms exponential (power) calculation on operators and assign value to the equivalent to left operand.
//=x//=yx = x//yPerforms floor division on operators and assign value to the left operand
Logical Operators:
OperatorExampleResult
and(x and y)is True if both x and y are true.
or(x or y)is True if either x or y is true.
not(x not y)If a condition is true then Logical not operator will make false.
Membership Operators:

OperatorDescriptionExample
inIt returns true if it finds a variable in the sequence otherwise returns falseList = [1,2,3,4,5,6,7,8];i=1;If(i in List):print (‘i is available in list’)Else print (‘i is not available in list’)Output – i is available in list
not inIt returns true if it does not find a variable in the sequence otherwise returns falseList = [1,2,3,4,5,6,7,8];j=10;If(j not in List):print (‘j is not available in list’)Else print (‘j is available in list’)Output – j is not available in list
Identity Operators:
These operators are used to compare the memory address of two objects.
OperatorDescriptionExample
isIt returns true if both operand ‘s identity is same otherwise falseI = 20J = 20If(I is J):print ‘I and J have same identity’Elserint ‘I and J have not same identity’Output – I and J have same identity
is notIt returns true if both operand ‘s identity is not same otherwise falseI = 20J = 230If(I is not J):print (‘I and J have not same identity’)Else print (‘I and J have same identity’)Output – I and J have not same identity
Bitwise Operators:

OperatorShorthandExpressionDescription
&Andx & yBits that are set in both x and y are set.
|Orx | yBits that are set in either x or y are set.
^Xorx ^ yBits that are set in x or y but not both are set.
~Not~xBits that are set in x are not set, and vice versa.
<<Shift leftx <<yShift the bits of x, y steps to the left
>>Shift rightx >>yShift the bits of x, y steps to the right.
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 xBoolean NOT
andBoolean 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.





No comments:

Post a Comment