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

Core Python 6 PM

Uploaded by

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

Core Python 6 PM

Uploaded by

kumargpc7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Operators:

==========
[Link] operators:
+,-,*,/,%,//,**

[Link] operatords:
<,<=,>,>=

[Link] operators:
==, !=

[Link] operators:
and, or , not

For non-boolean types behaviours:


----------------------------------------------------
-->0 means False
-->non-zero means True
-->Empty string is always False

x and y:
------------
If x evaluates to False return 'x' otherwise return 'y'

Ex:
10 and 20 #20
0 and 20 #0

-->If the first argument is zero then result is zero otherwise result is y.

x or y:
----------
If x evaluates True then result is x otherwise result is y.
Ex:
10 or 20 #10
0 or 20 #20

not x:
---------
If x evaluates to False then result is True otherwise False.
Ex:
not 10 #False
not '' #True

Ex:
'mahesh' and 'sunny' #sunny
'' and 'sunny' #''
'radhika' or 'lilly' #radhika
'' or 'lilly' #lilly
not 'radhika' #False
10 or 10/0 #10
0 or 10/0 #ZeroDivisionError

[Link] operators:
-----------------------------
-->We can apply these operators bitwise.
-->These operators are applicable only for int and boolean types.
&, |, ^, ~, <<, >>
&==>If both bits are 1 then only result is 1 otherwise result is 0
|==>If atleast one bit is 1 then the result is 1 otherwise result is 0
^==>x-or==>If bits are different then only result is 1 otherwise result is 0
~==>bitwise complement operator
1===>0 & 0===>1

0 & 0==>0 0 | 0==>0 0 ^ 0==>0


0 & 1==>0 0 | 1==>1 0 ^ 1==>1
1 & 0==>0 1 | 0==>1 1 ^ 0==>1
1 & 1==>1 1 | 1==>1 1 ^ 1==>0

Ex:
4 & 5 #4
4 | 5 #5
4 ^ 5 #1
~4 #-5

<< left shift operator:


10 << 2 #40

>> right shift operator:


10 >> 2 #

Ex:
True & False #False
True | False #True
True ^ False #True
~True #-2
True << 2 #4
True >> 2 #0

Q. Consider the following code:


-----------------------------------------------
v1 = 1
v2 = 0
v1 = v1 ^ v2
v2 = v1 ^ v2
v1 = v1 ^ v2
print(v1)

What is the result?

A) 0
B) 1
C) 2
D) 3

Answer:A

6).Assignment operators:
-------------------------------------
-->We can use assignment operator to assign a value to the variable.
Ex:
x = 10

-->We can combine assignment operator with some other operator to form compound
assignment operator.
Ex: x += 10===>x = x + 10
The possible compound assignment operators in python are:
+=, -=, *=, /=, %=, **=.............

No increment opoerators in python:


x++(Invalid)
x--(Invalid)
++x(Valid)
--x(Valid)

7).Ternary operator:
------------------------------
In java we can use:
x = (condition)?first value:second value
Ex:
x = (10<20)?30:40
SyntaxError: invalid syntax

In python we can use ternary operator:


x = first value if condition else second value
Ex:
x = 30 if 10 < 20 else 40
x #30
x = 30 if 10 > 20 else 40
x #40

pow(2,3,5)#3

-->Firstvalue if condition-1 else Secondvalue if condition-2 else Thirdvalue


Ex:
x = 10 if 20<30 else 40 if 50<60 else 70
x #10
x = 10 if 20>30 else 40 if 50<60 else 70
x #40
x = 10 if 20>30 else 40 if 50>60 else 70
x #70

Q.
a = 2
b = 3
a**b*a**b

Q.
x,y = 4,3
z = (x--x) + (y--y)
print(z)

Q.
>>> a = 1_2
>>> b = a*2
>>>b

You might also like