Thursday, November 26, 2015

Operators used in Python

Types of Operator

Python language supports the following types of operators.
Arithmetic Operators
Comparison (Relational) Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators

Python Arithmetic Operators:
Operator
Description
+ Addition
Adds values on either side of the operator.
- Subtraction
Subtracts right hand operand from left hand operand.
* Multiplication
Multiplies values on either side of the operator
/ Division
Divides left hand operand by right hand operand
% Modulus
Divides left hand operand by right hand operand and returns remainder
** Exponent
Performs exponential (power) calculation on operators
//
Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed.

Python Comparison Operators:

These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators.

Operator
Description
==
If the values of two operands are equal, then the condition becomes true.
!=
If values of two operands are not equal, then condition becomes true.
<> 
If values of two operands are not equal, then condition becomes true.
> 
If the value of left operand is greater than the value of right operand, then condition becomes true.
< 
If the value of left operand is less than the value of right operand, then condition becomes true.
>=
If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.
<=
If the value of left operand is less than or equal to the value of right operand, then condition becomes true.

Python Assignment Operators: 

Operator
Description
=
Assigns values from right side operands to left side operand
+= Add AND
It adds right operand to the left operand and assign the result to left operand
-= Subtract AND
It subtracts right operand from the left operand and assign the result to left operand
*= Multiply AND
It multiplies right operand with the left operand and assign the result to left operand
/= Divide AND
It divides left operand with the right operand and assign the result to left operand
%= Modulus AND
It takes modulus using two operands and assign the result to left operand
**= Exponent AND
Performs exponential (power) calculation on operators and assign value to the left operand
//= Floor Division
It performs floor division on operators and assign value to the left operand

Python Bitwise Operators:
Bitwise operator works on bits and performs bit by bit operation.

Operator
Description
& Binary AND
Operator copies a bit to the result if it exists in both operands
| Binary OR
It copies a bit if it exists in either operand.
^ Binary XOR
It copies the bit if it is set in one operand but not both.
~ Binary Ones Complement
It is unary and has the effect of 'flipping' bits.
<< Binary Left Shift
The left operands value is moved left by the number of bits specified by the right operand.
>> Binary Right Shift
The left operands value is moved right by the number of bits specified by the right operand.

Python Logical Operators: 
There are following logical operators supported by Python language.

Operator
Description
and Logical AND
If both the operands are true then condition becomes true.
or Logical OR
If any of the two operands are non-zero then condition becomes true.
not Logical NOT
Used to reverse the logical state of its operand.

Python Membership Operators:
Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples.

Operator
Description
in
Evaluates to true if it finds a variable in the specified sequence and false otherwise.
not in
Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.

Python Identity Operators:
Identity operators compare the memory locations of two objects. There are two Identity operators explained below:

Operator
Description
is
Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.
is not
Evaluates to false if the variables on either side of the operator point to the same object and true otherwise.

Python Operators Precedence:
The following table lists all operators from highest precedence to lowest.
Operator
Description

**
Exponentiation (raise to the power)

~ + -
Ccomplement, unary plus and minus (method names for the last two are +@ and -@)

* / % //
Multiply, divide, modulo and floor division

+ -
Addition and subtraction

>> <<
Right and left bitwise shift

&
Bitwise 'AND'
^ |
Bitwise exclusive `OR' and regular `OR'
<= < > >=
Comparison operators
<> == !=
Equality operators
= %= /= //= -= += *= **=
Assignment operators
is is not
Identity operators
in not in
Membership operators
not or and
Logical operators

No comments:

Post a Comment