operators in python
Operators are used to perform operations on variables and values. There are 7 types of basic operators in python. They are as folluws :
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Python Arithmetic Operators
Operator | Name | Description | eg: |
+ | Addition | Adds values on either side of the operator | a+b |
- | Subtraction | Subtracts values on either side of the operator | a-b |
* | Multiplication | Multiplies values on either side of the operator | a*b |
/ | Division | Divides values on either side of the operator | a/b |
% | Modulus | Divides left hand operand by right hand operand and returns remainder | a%b |
** | Exponentiation | Performs exponential calculation on operators | a**b |
// | Floor Division | The division of operands where the result is quotient in which the digits after the decimal points are removed. | a//b |
Python Assignment Operators
Operator | Name | Description | eg: |
= | Equal | Assigns values from right side operands to left side operand | a=b |
+= | Addition And | Adds right operand to the left operand and assign the result to left operand | a+=b |
-= | Subtraction And | Subtracts right operand from the left operand and assign the result to left operand | a-=b |
*= | Multiplication And | Multiplies right operand with the left operand and assign the result to left operand | a*=b |
/= | Division And | Divides left operand with the right operand and assign the result to left operand | a/=b |
%= | Modulus And | Takes modulus using two operands and assign the result to left operand | a%=b |
**= | Exponent And | Performs exponential calculation on operators and assign value to the left of the operand | a**=b |
//= | Floor Division | Performs floor division on operators and assign value to the left of the operand | a//=b |
Python Comparison Operators
Operator | Name | Description | eg: |
== | Double Equal to | True if LHS=RHS | a==b |
!= | Not equal to | True if LHS not equal to RHS | a!=b |
< | Less than | True if LHS less than RHS | a<b |
> | Greater than | True if LHS greater than RHS | a>b |
<= | Less than or equal to | True if LHS less than or equal to RHS | a<=b |
>= | Greater than or equal to | True if LHS greater than or equal to RHS | a>=b |
<> | Not Equal to | Same as != | a<>b |
Python Logical Operators
Operator | Name | Description | eg: |
and | Logical AND | True if both operands are true | a and b |
or | Logical OR | True if one of the statement is true | a or b |
not | Logical NOT | True if the result is false | a not b |
Python Identity Operators
Operator | Name | Description | eg: |
is | is | True if both variables point to the same object | a is b |
is not | is not | False if both variables point to the same object | a is not b |
Python Membership Operators
Operator | Name | Description | eg: |
in | is | True if it finds a variable in the specified sequence | a in b |
not in | is not | True if it does not finds a variable in the specified sequence | a not in b |
Python Bitwise Operators
Operator | Name | Description | eg: |
& | AND | Sets each bit to 1 if both bits are 1 | a==b |
| | OR | Sets each bit to 1 if one of two bits are 1 | a!=b |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 | a<b |
~ | NOT | Inverts all the bits | a>b |
<< | Left Shift | Shifts left by pushing zero in from right and let the leftmost bits fall off | a<=b |
>> | Right Shift | Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off | a>=b |
⇐Prev
Next⇒
Comments
Post a Comment