2 Operators PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Operators in Python

OPERATORS are the symbols that trigger the action/


operations on data.

OPERANDS are the data on which operation is being


carried out.
Arithmetic Operators
Operator Name Example
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Modulus a%b
** Exponentiation a**b
// Floor division a//b

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. These operators are used to compare the values.


2. difference between ‘=‘ and ‘==‘ , ‘=‘ is initialisation operator where as ‘==‘ is comparison
operator.
3. Relational operators return result in ‘True’ or ‘False’
4. Relational operators can also be used with String.(It will compare with the ASCII value of
the characters)
Logical Operators
Operator Description Example
and Returns True if both statements are true a<5 and a<10
or Returns True if one of the statements are true a<5 or a<10
not Reverse the result, returns False if the result is true not(a<5 and a<10)

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.

2. Suppose we use ‘or’ operator 7 or 9 it will return 7 as the first condition is


correct it will not check the other conditions. Suppose we write 0 or 5 0r 7. in this
case it will return 5 as this is the first condition which is true.
Identity operators
operator Description Example
is Returns True if both variables are the same object ie. a is b
pointing the same value.
is not Returns True if both variables are not the same object ie a is not b
pointing two different values.

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: these operators are used in string ,list, tuples etc.


Example: str=“hello world”
‘w’ in str
Now it will return True
Augmented Assignment Operators
+= a+=b a=a+b
-= a-=b a=a-b
*= a*=b a=a*b
/= a/=b a=a/b
%= a%=b a=a%b
//= a//=b a=a//b
**= a**=b a=a**b
&= a&=b a=a&b
!= a!=b a=a!=b
^= a^=b a=a^b
>>= a>>=b a=a>>=b
<<= a<<=b a=a<<=b

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