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:
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | x+y | Sum of x and y. |
| - | Subtraction | x-y | Difference of x and y. |
| * | Multiplication | x*y | Product of x and y. |
| / | Division | x/y | Quotient of x and y. |
| % | Modulus | x%y | Remainder of x divided by y. |
| ** | Exponent | x**y | x**y will give x to the power y |
| // | Floor Division | x/ y | The division of operands where the result is the quotient in which the digits after the decimal point are removed. |
| Operator | Name | Example | Result |
|---|---|---|---|
| == | Equal | x==y | True if x is exactly equal to y. |
| != | Not equal | x!=y | True if x is exactly not equal to y. |
| > | Greater than | x>y | True if x (left-hand argument) is greater than y (right-hand argument). |
| < | Less than | x<y | True if x (left-hand argument) is less than y (right-hand argument). |
| >= | Greater than or equal to | x>=y | True if x (left-hand argument) is greater than or equal to y (left-hand argument). |
| <= | Less than or equal to | x<=y | True if x (left-hand argument) is less than or equal to y (right-hand argument). |
Assignment Operators:
| Operator | Shorthand | Expression | Description |
|---|---|---|---|
| += | x+=y | x = x + y | Adds 2 numbers and assigns the result to left operand. |
| -= | x-= y | x = x -y | Subtracts 2 numbers and assigns the result to left operand. |
| *= | x*= y | x = x*y | Multiplies 2 numbers and assigns the result to left operand. |
| /= | x/= y | x = x/y | Divides 2 numbers and assigns the result to left operand. |
| %= | x%= y | x = x%y | Computes the modulus of 2 numbers and assigns the result to left operand. |
| **= | x**=y | x = x**y | Performs exponential (power) calculation on operators and assign value to the equivalent to left operand. |
| //= | x//=y | x = x//y | Performs floor division on operators and assign value to the left operand |
| Operator | Example | Result |
|---|---|---|
| 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. |
| Operator | Description | Example |
| in | It returns true if it finds a variable in the sequence otherwise returns false | List = [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 in | It returns true if it does not find a variable in the sequence otherwise returns false | List = [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 |
These operators are used to compare the memory address of two objects.
| Operator | Description | Example |
| is | It returns true if both operand ‘s identity is same otherwise false | I = 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 not | It returns true if both operand ‘s identity is not same otherwise false | I = 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 |
| Operator | Shorthand | Expression | Description |
|---|---|---|---|
| & | And | x & y | Bits that are set in both x and y are set. |
| | | Or | x | y | Bits that are set in either x or y are set. |
| ^ | Xor | x ^ y | Bits that are set in x or y but not both are set. |
| ~ | Not | ~x | Bits that are set in x are not set, and vice versa. |
| << | Shift left | x <<y | Shift the bits of x, y steps to the left |
| >> | Shift right | x >>y | Shift the bits of x, y steps to the right. |
Operator Precedence:
Highest precedence at top, lowest at bottom.
| Operator | Description |
|---|---|
| ( ) | Parentheses |
| x[index],x[index1:index2],f(arg...),x.attribute | Subscription, slicing, call, attribute reference |
| ** | Exponentiation |
| +x, -x, ~x | Positive, 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 |
| or | Boolean OR |
| if - else | Conditional expression |
| lambda | Lambda expression |
No comments:
Post a Comment