Python Operator
Operators are special symbols that is used to perform some arithmetic or logical computation.
Operators are used with operand like 1+2 here 1 and 2 is operand and + is an operator.
There are following types of operator in Python:
- Arithmetic operators
- Comparison operators
- Logical operators
- Bitwise operators
- Assignment operators
- Special or identity operators
- Membership operators
1. Arithmetic operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division etc.
Operator | Name & Meaning |
---|---|
+ | Addition Perform addition or unary plus. |
- | Subtraction Perform subtraction or unary minus. |
* | Multiplication Perform multiplication. |
/ | Division Perform division. |
% | Modulus Perform modulus to get reminder. |
// | Floor Division Perform division and get result in whole number. |
** | Exponentiation Left raised power of Right. |
Examples :
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit] Type "help", "copyright", "credits" or "license" for more information. >>> x=10 >>> y=5 >>> print(x+y) 15 >>> print(x-y) 5 >>> print(x*y) 50 >>> print(x/y) 2.0 >>> print(x//y) 2 >>> print(x**y) 100000 >>>
2. Comparison operators
Comparison operators are used to compare the values and it always return either true or false.
Operator | Name & Meaning |
---|---|
< | Less Than Return true if left oprand less than right. |
> | Greater Than Return true if left oprand greater than right. |
== | Equal Return true if both operands are equal. |
!= | Not Equal To Return true if both operands are not equal. |
<= | Less Than or Equal To Return true if left operand less than or equal to the right operand. |
>= | Greater Than or Equal To Return true if left operand greater than or equal to the right operand. |
Examples :
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit] Type "help", "copyright", "credits" or "license" for more information. >>> x=1 >>> y=2 >>> print(x< y) True >>> print(x>y) False >>> print(x>=y) False >>> print(x<=y) True >>> print(x==y) False >>> print(x!=y) True >>>
3. Logical Operators
Logical operators are used to combine conditional statements.
Operator | Name & Meaning |
---|---|
and | Logical And Return true if both statements are true. |
or | Logical Or Return true one of the statements are true. |
not | Logical Not Reverse the result that means return false if the statements results are the true. |
Examples :
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit] Type "help", "copyright", "credits" or "license()" for more information. >>> x=5 >>> y=10 >>> print(x > 4 and y < 5) False >>> print(x > 4 or y < 5) True >>> print(not(x > 4 or y < 5)) False >>>
4. Bitwise Operators
Bitwise Operators are used to compare the bits (binary) numbers.
Operator | Name & Meaning |
---|---|
& | AND Return 1 if both bits are 1. |
| | OR Return 1 if one of the two bits is 1. |
^ | XOR Return 1 if only one of two bits is 1. |
~ | Not Return inverts all the bits. |
<< | Left Shift Shift left by pushing zeros in from the right and let the leftmost bits fall off. |
>> | Right Shift Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off. |
Examples :
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit] Type "help", "copyright", "credits" or "license()" for more information. >>> x=10 >>> y=5 >>> print(x&y) 0 >>> print(x|y) 15 >>> print(x^y) 15 >>> print(x~y) SyntaxError: invalid syntax >>> print(~x) -11 >>> print(~y) -6 >>> print(x >> 3) 1 >>> print(x << 3) 80 >>>
5. Assignment Operators
Assignment operators are used to assign the values to variables.
Operator | Example Like As |
---|---|
= | x=10 x=10 |
+= | x+=10 x=x+10 |
-= | x-=10 x=x-10 |
*= | x*=10 x=x*10 |
/= | x/=10 x=x/10 |
%= | x%=10 x=x%10 |
//= | x//=10 x=x//10 |
**= | x**=10 x=x**10 |
&= | x&=10 x=x&10 |
|= | x|=10 x=x|10 |
^= | x^10 x=x^10 |
<<= | x <<= 10 x=x <<=10 |
>>= | x>>10 x=x>>10 |
6. Special or Identity Operators
Python provides special or identity operators or membership operator to check if two values (variables) are located on the same part of memory or not.
Operator | Name & Meaning |
---|---|
is | Return the true if both are refer the same object. |
is not | Return the true both are not refer the same object. |
Examples :
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit] Type "help", "copyright", "credits" or "license()" for more information. >>> x=1 >>> y=2 >>> print(x is y) False >>> print(x is not y) True >>>
7. Membership Operators
Membership operators are used to test if a sequence is presented in an object.
Operator | Name & Meaning |
---|---|
in | Returns True if a sequence with the specified value is present in the object. |
not in | Returns True if a sequence with the specified value is not present in the object. |
Examples :
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit] Type "help", "copyright", "credits" or "license()" for more information. >>> x=["dog","cat"] >>> print("dog" in x) True >>> print("deep" not in x) True >>>
No comments:
Post a Comment