5.Operators
Arithmetic Operators
Comparison (Relational) Operators
Assignment Operators
Logical Operators
Bit wise 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/
Modulo |
17%5 |
23%2 |
|
** |
Exponentiation |
2**3 |
2**8 |
|
// |
Integer/ floor
Division |
7.0//2 |
-5/ / 2 |
| Symbol | Description | Example 1 | Example 2 |
| < | Less than | 7<10 True |
Goodbye' < 'Hello' True |
| > | Greater than | 7>5 True |
'Goodbye' > 'Hello' False |
|
<= |
less than equal to | 2<=5 True |
"Hello" <= “Goodbye” False |
|
>= |
greater than equal to |
10>=10 True |
"Hello" >= “Goodbye” True |
|
! = |
not equal to | 10!=11 True |
‟Hello”!= “HELLO" True |
|
== |
equal to | 10==10 True |
"Hello" == “Hello” True |
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
|
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 |
Note: Important Operators
Summary

Comments
Post a Comment