2 Operators PDF
2 Operators PDF
2 Operators PDF
1.Difference between ‘/’ and ‘//’ is that ‘/’ will give quotient in
decimal while ‘//’ gives quotient in integer.
2. ‘%’ returns remainder .( If 3%13 is written i.e first no. is smaller
than the second no. Then it will return first no. as answer)
3. Associativity of all the operators is left to right except ‘**’ , its
associativity is from right to left (eg. 2**3**2 this will first solve 3**2 =9 then
2**9=512.
Relational Operators
Operator Name Example
== Equal a==b
!= Not equal a!=b
> Greater than a>b
< Less than a<b
>= Greater than or equal a<=b
to
<= Less than or equal to a>=b
1.With logical operators we can write digits also(All the non zero numbers will
be considered as True.) Eg. 7 and 0 it will return zero . Suppose we write 7 and 9
It will return the second value ie. 9 since both the condition is True.
If ‘is’ operator is returning True then ‘==’ operator will always return true.
There are few cases where python creates two different objects that both
store the same value.
• Input of string from the console.(because if we take value from the user
it will always be stored in different location.)
• Very big integer
• Writing floating point and complex numbers. (floating point and
complex no. Are stored in different locations)
• Eg. 0.1+o.1+0.1==0.3 will return False, because floating point
values(fractional values) returnupto 14 or 15 decimal values.
Membership operator
Operator Description Example
in Returns True if a sequence with a specified value is a in b
present in the object.
not in Returns True if a sequence with a specified value is a not in b
not present in the object.
Note: if we have same variable on both side of ‘=‘ then we can write it in short hand.
Bitwise Operators
Operator Name Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits are 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill left shift Shift left by putting zeros in from the right and let
the left most bit fall off
>> Signed right shift Shift right by pushing copies of the left most bit in
from the left
& operator returns 1 if both compared bit is 1, otherwise 0, then change it to decimal value.
Eg. a=10(1010), b=12(1100), now a&b it will give 8 (1000).
A=15 #1111
A<<3 #1111000 26 25 24 23 22 21 20
64 32 16 8 4 2 1
1 1 1 1 0 0 0
120
Operator Precedence
operator Description
() Parentheses
** Exponentiation
~x Bitwise nor
+x , -x Positive , Negative (unary + , -)
* , / , // . % Multiplication, division, floor division, remainder
+,- Addition , subtraction
& Bitwise and
^ Bitwise XOR
| Bitwise OR
<, >, <=, >=, <>, !=, == Relational operator , identity operator
is , is not
not x Boolean NOT
and Boolean AND
or Boolean OR