Arithmetic Operators
Comparison (Relational) Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
Arithmetic Operators( Mathematical)
Symbol | Description | Example 1 | Example 2 |
+ | Addition | >>>55+45 | >>> “Good” + “Morning” |
- | Subtraction | >>>55-45 | >>>30-80 |
* | Multiplication | >>>55*45 | >>> “Good” * 3 |
/ | Division | >>>17/5 | >>> 17.0/5 |
% | Remainder/ | >>>17%5 | >>> 23%2 |
** | Exponentiation | >>>2**3 | >>>2**8 |
// | Integer | >>>7.0//2 | >>>3/ / 2 |
Symbol | Description | Example 1 | Example 2 |
< | Less than | >>>7<10 | >>>'Goodbye' < 'Hello' |
> | Greater than | >>>7>5 | >>>'Goodbye' > 'Hello' |
<= | less than equal to | >>> 2<=5 | >>>"Hello" <= “Goodbye” |
>= | greater than equal | >>>10>=10 | >>>"Hello" >= “Goodbye” |
! = | not equal to | >>>10!=11 | >>>‟Hello”!= “HELLO" |
== | equal to | >>>10==10 | >>>"Hello" == “Hello” |
Assignment Operator combines the effect of arithmetic and assignment operator
Symbol | Description | Example | Explanation |
= | Assigned values from right side | >>>x=12 | x=12 ( we will assume x=12 for all examples) |
+= | added and assign back the result | >>>x+=2 | x=x+2 |
-= | subtracted and assign back the | x-=2 | x will become 10 |
*= | multiplied and assign back the | x*=2 | x will become 24 |
/= | divided and assign back the result to left operand | x/=2 | x will become 6 |
%= | taken modulus using two | x%=2 | x will become 0 |
**= | performed exponential (power) | x**=2 | x will become 144 |
//= | performed floor division on | x / /= 2 | x will become |
Symbol | Description | Example |
or | If any one of the operand is true, then the condition becomes true. | A or B is True |
and | If both the operands are true, then the condition becomes true. | A and B is False |
not | Reverses the state of operand/condition. | not A is False |
Python Bitwise Operators
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60; and b = 13; Now in binary format they will be as follows −
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
Python's built-in function bin() can be used to obtain binary representation of an integer number.
The following Bitwise operators are supported by Python language
Operator | Description | Example |
& Binary AND | Operator copies a bit, to the result, if it exists in both operands | (a & b) (means 0000 1100) |
| Binary OR | It copies a bit, if it exists in either operand. | (a | b) = 61 (means 0011 1101) |
^ Binary XOR | It copies the bit, if it is set in one operand but not both. | (a ^ b) = 49 (means 0011 0001) |
~ Binary Ones Complement | It is unary and has the effect of 'flipping' bits. | (~a ) = -61 (means 1100 0011 in 2's complement form due to a signed binary number. |
<< Binary Left Shift | The left operand's value is moved left by the number of bits specified by the right operand. | a << 2 = 240 (means 1111 0000) |
>> Binary Right Shift | The left operand's value is moved right by the number of bits specified by the right operand. | a >> 2 = 15 (means 0000 1111) |
Membership Operators
Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below
Let S=”Python” c=’o’
Operator | Description | Example |
in | Evaluates to true if it finds a variable in the specified sequence and false otherwise. | c in S will result True |
not in | Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. | c not in S will result False |
Identity Operators
Identity operators compare the memory locations of two objects. There are two Identity operators as explained below
Let x=10 and y=x
id(x) and id(y) are same in this case
Operator | Description | Example |
is | Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. | x is y will return True |
is not | Evaluates to false if the variables on either side of the operator point to the same object and true otherwise. | x is not y will return False |
The following table lists all operators from highest precedence to the lowest.
Sr.No. | Operator & Description |
1 | ** Exponentiation (raise to the power) |
2 | ~ + - Complement, unary plus and minus |
3 | * / % // Multiply, divide, modulo and floor division |
4 | + - Addition and subtraction |
5 | >> << Right and left bitwise shift |
6 | & Bitwise 'AND' |
7 | ^ | Bitwise exclusive `OR' and regular `OR' |
8 | <= < > >= Comparison operators |
Arithmetic Expressions and Evaluation
11
here 3**2 will be evaluated first because it is having high precedence. So 2+9=11 will be the result.
>>>2**3**2
512
Here 3**2 will be evaluated first because the associativity is from right to left. So 2**9=512 will be the result.>>>-4**2 # here unary operator applied later
-16
>>4%0
This will produce division by zero exception
>>>2+3/2*4**2
18
Comments
Post a Comment