0% found this document useful (0 votes)
7 views21 pages

5 Python Operators

Uploaded by

229x1a3256
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views21 pages

5 Python Operators

Uploaded by

229x1a3256
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

PYTHON OPERATORS

Unary Operators
Binary Operators
Arithmetic Operators
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Example
• a=10
• b=2
• print('a+b=',a+b) • a+b= 12
• print('a-b=',a-b) • a-b= 8
• print('a*b=',a*b) • a*b= 20
• print('a/b=',a/b) • a/b= 5.0
• print('a//b=',a//b) • a//b= 5
• print('a%b=',a%b) • a%b= 0
• print('a**b=',a**b) • a**b= 100
Example
• a=10.5
• b=2
• print('a+b=',a+b) • a+b= 12.5
• print('a-b=',a-b) • a-b= 8.5
• print('a*b=',a*b) • a*b= 21.0
• print('a/b=',a/b) • a/b= 5.25
• print('a//b=',a//b) • a//b= 5.0
• print('a%b=',a%b) • a%b= 0.5
• print('a**b=',a**b) • a**b= 110.25
• / operator always performs floating point
arithmetic. Hence it will always returns float
value.
• But Floor division (//) can perform both floating
point and integral arithmetic.
• If arguments are int type then result is int type. If
atleast one argument is float type then result is
float type.

• 10/2==>5.0
• 10//2==>5
• 10.0/2===>5.0
• 10.0//2===>5.0
• We can use +,* operators for str type also.
• If we want to use + operator for str type then
compulsory both arguments should be str
• type only otherwise we will get error.
• >>> “GPREC“ + 10
• TypeError: must be str, not int
• >>> “GPREC“ + "10"
• ‘GPREC10‘

• If we use * operator for str type then compulsory one


argument should be int and other argument should be
str type.
• >>>2 * ”GPREC”
• ‘GPRECGPREC’
• >>>”GPREC” * 2
• ‘GPRECGPREC’
Assignment Operators
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Comparison Operators
Operato Name Example
r
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal x >= y
to
<= Less than or equal to x <= y
• a=10
• b=20
• print("a > b is ",a>b)
• a > b is False
• print("a >= b is ",a>=b)
• a >= b is False
• print("a < b is ",a<b)
• a < b is True
• print("a <= b is ",a<=b)
• a <= b is True

• a=“GPREC"
• b=“GPREC"
• print("a > b is ",a>b) • a > b is False
• print("a >= b is ",a>=b) • a >= b is True
• print("a < b is ",a<b) • a < b is False
• print("a <= b is ",a<=b) • a <= b is True
Chaining of operators
• Chaining of relational operators is possible.
• In the chaining, if all comparisons returns True
then only result is True.
• If atleast one comparison returns False then the
result is False
• 10<20 ==>True
• 10<20<30 ==>True
• 10<20<30<40 ==>True
• 10<20<30<40>50 ==>False
Equality operators (== , !=)
• We can apply these operators for any type even for incompatible
types also
>>> 10==20 False
>>> 10!= 20 True
>>> 10==True False
>>> False==False True
>>> “gprec"==“gprec“ True
>>> 10==“gprec“ False

• Chaining concept is applicable for equality operators.


• If atleast one comparison returns False then the result is
False. otherwise the result is True.
>>> 10==20==30==40 False
>>> 10==10==10==10 True
Boolean/Logical Operators

Operato Description Example


r
and Returns True if both x < 5 and x < 10
statements are true

or Returns True if one of x < 5 or x < 4


the statements is true

not Reverse the result, not(x < 5 and x <


returns False if the result 10)
is true
Bitwise Operators
Operato Name Description Exampl
r e
& AND Sets each bit to 1 if both bits are 1 x&y

| OR Sets each bit to 1 if one of two bits is 1 x|y

^ XOR Sets each bit to 1 if only one of two bits x^y


is 1
~ NOT Inverts all the bits ~x

<< Zero fill Shift left by pushing zeros in from the x << 2
left shift right and let the leftmost bits fall off

>> Signed Shift right by pushing copies of the x >> 2


right leftmost bit in from the left, and let the
shift rightmost bits fall off
Identity Operators
• We can use identity operators for address comparison.
Operato Description Example
r
is Returns True if both x is y
variables are pointing to the
same object
is not Returns True if both x is not y
variables are not pointing to
the same object
a=10 x=True
b=10 y=True
print(a is b) True print( x is y) True
Membership Operators
• We can use membership operators to check
whether the given object present in the given
collection.
• It may be String, List, Set, Tuple or Dict

in Returns True if the given object present in


the specified Collection
not in Returns True if the given object not
present in the specified Collection
Membership Operators
Operator Description Example
in Returns True if a x in y
sequence with
the specified
value is present
in the object
not in Returns True if a x not in y
sequence with
the specified
value is not
present in the
x="hello learning Python is very easy!!!"
print('h' in x) True
print('d' in x) False
print('d' not in x) True
print('Python' in x) True

list1=["sunny","bunny","chinny","pinny"]
print("sunny" in list1) True
print("tunny" in list1) False
print("tunny" not in list1) True
OPERATOR PRECEDENCE AND ASSOCIATIVITY
OPERATOR PRECEDENCE AND ASSOCIATIVITY

You might also like