Uploaded by Education Tamila

Operators&Operators precedence

advertisement
1
QPE1
RAT0RS:
1
v 01perators ,are the co1nstru1
cts whi,ch c,an
ma1
ni'p,ulat,e the valu,e 0f operani ds~
1
1
v Co nsider the express l,on 4 + 15 = 9, Here. 4,
and 5 are called operands an,dl + Is cal.led
1
1
1
1
1
,-r"-=-----·
o,,,.rator
Types of Operators,:
1
-Pytho,n !language su1ppo ns the fo UO'Wlng
type,s of operators
1
1
1
Ariit:h1metic 0 perators,
C,om pa1
riso,n {Relati10n,al) 0 pe1ra1tors,
Assignm ent 0perators
Lo gilca1I Op1er1
at:0rs
!Bitwise Operators
Membersh1ip Op,erators
Identity Op erat ors
1
1
1
1
•·
1
1
1
1
1
1
1
1
1
1
Arithmetic 012erators:
They are used to perform mathematical
operations
like
addition,
subtraction,
multiplication etc. Assume, a=l Oand
Opemor
Doscrtpclon
• Addition
Adds valun on olthor side of tho operator.
• Subtnctlon
Subtr.i<ts
opennd.
ncht
Wuhl!llln valUfl
b=S
Ex•m~
hand 1,p,,rond from h,11 hand • - b • • 10
°" elther<lde of tho operotor
••b• 200
Nut!ipln«lon
/ DMslo:n
Divides Ifft band operand by 11&111 bond operand
,. Modulus
Divides ldl band opu.,nd by f1cht hand oper.ind
-S rttums rematndff
~ ~,
Pertonns e>ponendal
11peraton
I/
floor Division · The divis ion of opminds wh<n the 5//2• 2
....,. Is the QUOCl<lnt In wt,lch the dlplS aftrr the
d«lnul point""' <ffllOVed
Examples
a=10
b=S
print("a+b=",a+b)
print("a-b=''.a-b)
prlnt("a*b=",a*b)
print("a/b=",a/ b)
pri nt("a%b=",a %b)
print("a//b=",a//b)
prlnt("a**b=",a**b)
b 'lh = 0
(po-r) akulatlon on •••b • IOIJ>tbr
pow,,r 20
Output
a+b= 15
a-b= 5
a*b= 50
a/ b= 2.0
a%b= O
a//b= 2
a**b= 100000
ComP-arison (Relatlonal)_QP-erators:
Comparison operators are us,ed to
compare values.
It either returns True or False
acco rding to the condition. Assume, a=10
and b=5
EDmple
Op,r.,lor
lloscTlpllon
=--
If the values ol rwo opennds: are Nlual, lhrn lhe condJUon (a:: b) ls
nOI true.
bt-comntrur .
I•
If ~ues or two opennds art nol equal. then condltkKI (>l'<b}
bocomes true.
u-
>
If the value of left OJ)C'.r.and Is tlJ'Ntctr 1hnn the valur of light (a > b) lJ
operand. then condition b«omn true.
noc true.
<
If IM ,at ... or ldt • .,....nd Is less 1hon lhe value or riRhl
opttand.. then m ndJtion b«omn 1rue.
u
<•
I! llll ¥11111' or Ifft openind IJ area,er 1han or rqual
va.Jue o( right ope.r.1nd. then condition bttomes true.
Is
(a < bl IJ
trut>.
to lht (a •• b) IJ
Ir the value ol litft oper.and ls ~ess than or equal to tht valur
of right operand. then tondlllon bfcomes true.
nol (Ne.
(a
a;
true.
b) ls
Output
a+b= 15
a-b= 5
a*b= 50
a/ b= 2.0
a%b= 0
a//b= 2
a**b= 100000
ComRarison {Relatlonal)_QRerators:
Comparison operators are used to
compare values.
It either returns True or False
according to the condition. Assume, a=10
and b=5
0porXM Descrlpdoo
~
Example
II the ..iues al two oprnnds an, eqwil, then the Clllldition (a
noc true.
bttomrs true.
I•
If \'31ues o( two _..,m1s
bfaJmn tnlL
an,
nol equal, lhen tondidon (al•b)
is
tr!W
111""•••
>
If Ille ••lu• ol lc,fl oper.ond is
than t.he value of right
operand, t.hen condldon b«om<S 1rue.
c
II the • alut ol ltft OPlf'lnd 11 Ins than the \'31uo of
operand. then conditlon bocomH lrut.
••
= b) is
rf«ht
I•
> b) IS
n04 truo.
I• c bl 11
true.
If the wluo of left aper.ind is great..- t~n or eqllll to tho I•>• b) is
n04 IJ\IL
..11.. or rllht operand, then condltlon btto,n•s tnlt.
If the valut of Ifft operand Is Ins tlwl or tqlW to tho Yalut
of right Ol)ONnd, then condlUon bocomH tru•.
(a•• b) 11
trut.
Example
a=10
b=S
print("a>b=>",a>b)
prlnt("a>b=>",a<b)
print("a==b=>",a==b)
print("a!=b=>",a!=b)
print("a>=b=>",a<=b)
print("a>=b=>",a>=b)
Output:
a>b=> True
a>b=> False
a==b=> False
a!=b=> True
a>=b=> False
a>=b=> True
Assignment Ogerators:.
-Assign1men1t· o:perators are us,ed in Pyt:horn to
assig1
n va1lues t0 variableS~
1
1
1
I ~mpuon
Oper;idor
I K~mp1~
AMJ1111, v11hn!t fimm ri1ht 5ide upenind$ tu Ii.it itldl1· e= .a1 +1J
~s:1.RM
operand
Villu~
ur " .,
'binto!C
II adds rdgtn: ,optirand Ila the left· aptQnd, and Qtlp r •·= lill 1.i
ieq,urvaJi!.nt
th! m.sultto len ope"nd
kn:-mc ♦ a
"'=
S u ~ It subu,u;;u rl1ht upc,n1ad hm. the 1@A Dp1-n1nd a~
AND
aH~IJ!I IN retUU ta le.ft os-nnd
a Il l
fi!CIUINll'nt
C'
- m:
ID C' ~ IC - ;a
.,_
Muit l1pl).' U mui1 ~pUf:i rnxltt opu.md wUh lh• lrD DP"r■md and
Afi D
llllr&a thr Rs.111110 left OPff."ind
/r:.
Dlvld I!!
AND
.n
dividlli!!5 left. npmmcl "frii.h lbe
~1,111 thc 1 nUlr tu Im epcn1nd
Ji&lli aprnmd !ind
1
f,
·=·
/J
u
rqulnJmt
toc :clll a.
'
Jr;; . i
~.
equlvt\'lt-!nt
ta 1: = ·C: I arr
/•
:a
hii
,qutvatent
uu: ; ; 1:f/J
modului, dill two II permd5 and .nslfln the- c il!b; a l!I
r.muh: 10 lrft opn,md
equ:Mleiat
ttu· .. c '1111 ia
Modll.lllut, It ~
,r.;:
AND
ill•
Eqtc,n.en.t
.
AND
J'/=-.
[JJ;vJ, ln n
f.loor
•IP-Jm-Ddal [pawecrJ ca],cul.ttlon
cpN111Dn and U1IKB value to the ~.:r1 uperand
Ptrtomu
D1il
p:rfenm Door dtiil=i~o~ on ~t!ln and ~
v..lW! to thr It'll: op~rand
u
c
11
~ ii
rks
eQUl~ll'nt
ttu: :ui: ,.-_,. ii
c //:. .a i1
~ 1uival@nt
ID C' = C. II I
Example
a= 211
b = 10
c·= 0
c=a+b
prin1(''Line 1 - Value 0f c is. n, c)
1
1
C+-a·
~
~ -
.
.
11
print( Line 2 - Value iof C is
1
1
",
c)
c·*= a
. 1(.nl•1ne 3·
prim.· ,... V
. ,aIue rOf C~
l·!S, ·, C·.
111
1
C
)
/= ,a
prin1(''Line 4 - Value of C is
1
1
",
c)
c·= 2 c %= a
print("Line 5 - Value of C iis, c) c·**= a
1
1
1
printf~Line 6 ·- Value of C i1s
1
1
1
1
",
1
",
c) C //= a
priintC Line 7 - v,alue of C iis c)
1
1
1
",
Output
Line 1 - Value of c Is 31
Line 2 - Value of c is 52
Line 3 - Value of c is 1092
Line 4 - Value of c Is 52.0
Line 5 - Value of c is 2
Line 6 - Value of c is 2097152
Line 7 - Value of c is 99864
,L
ogical 012erators:
-Logical operators are the and, or, not
operators.
Ol>ff1tor
~ .,,,.,,
and
Tr~
or
Tn.H! ,t e ther ot
not
Tr~
fumple
,t both me - • n d• are true
itw, ope,r ands 1,s
tr~
A
and
x or y
d - • n d os ,.,.., (ca<nplenlfflU me operand) noc A
Example
a= True
b = False
prlnt('a and b ls',a and b)
prlnt('a or b is',a or b)
prlnt('not a ls',not a)
y
Output
x and y is False
x or y is True
not x is False
Bitwise OP-erators:
A bitwise operation operates on one or
more bit patterns at the level of
individual Bits
Example:
Let x = 10 (0000 101 0 in binary) and
y = 4 {0000 0100 In binary)
Ope rator
M e aining
Exaim plo
&
B1twis,, A N O
x& y = 0 (oooe oooe)
Bitwise OR
X
Bitwise NOT
-x •
B1tw1se XOR
x
>>
B1tw1se right shift
x >> 2
<<
Bitwise le ft sh,ft
x<< 2 = 40 ( 0010 1000 )
I y =
~
14 ( 0000
• 11 ( 1111
11 10)
0101)
y = 14 ( eooo ,uo >
=
2 ( 0000 00 10)
Example
a = 60
# 6 01= oo·, ,
b := 113
# 113 = 0000 11 D1
1
, , 0101
1
c=O
c = a & b;
# 12. = 0000 1 11
Q0
1
1
prlnt "Line 1 - Vallue of c Is ", c.
1
C'
= Si I b; # 61 = 0 0'1'1 11011
1
1
print nl 1
iine 2 - v·alue 10 1f· C is
11
,
C
c = a A b; # 49 = 010111 0001
1
print "Line ,3 Value of c is
1
C = rv.a;
-
1
~ ~.
c
# -61 = 1100 0011
print ,.!Line 4.- Value of c is r\,c
1
C
= a cc: 2;
# 240
~
1111 000 0
1
print itL1lne 5 - v·a1u1
e of C i.s '', c
1
c = a >> 2;
# 15 = 1
Q000 111111
1
0
p1
rin1t 'Line 6 - v·atue of c i,s '\ c
1
Output
Line 1 - Value of c is 12
Line 2 - Value of c Is 61
Line 3
- Value of c is 49
Line 4
- Value of c is -61
Line 5
- Value of c is 240
Line 6
- Value of c Is 15
MembershlP-QP-erators:
v Evaluates to find a value or a variable is in
the specified sequence of string, list, tuple,
dictionary or not.
v Let, x=[S,3,6,4,1]. To check particular item
In 11st or not, In and not In operators are
used.
o .,...-• .tor
M•anln&
Eumple
Example:
x=[S,3,6,4, 1]
>>> 5 In X
True,
>>> 5 not In x
False
ldenti1)'_QP.erators
They are used to check if two val1Ues (or
variables) are located on the same part of
the memory.
Ot>«•IOf'
Me.nq
f,ut, If It~
oprranth ,.,~ 1denl'U tr~(«tf 10 the wn.t
OCjoct)
,. noc
True it me
C)pt',..,..,.. ar e- nioc
.wmtOqttt]
Example
x=S
y=5
x2 = 'Hello'
y2 = 'Hello'
print(x1 is not y1)
orint(x2 is v2)
idt!flflUII (OO noc rt"fttt f o a-,
" " rrue
0 utput
1
False,
True
OPERATOR PRECEDENCE:
When an expression contains more than one
operator, the order of evaluation depends on
the order of operations.
Oper-.itor
..
Ot,scnpt:lon
ExpoDl!nllatlon (raise to tht' powt,r)
Complement, unary plus and m inus (method
names (or the last two""'+@ and -0)
MulUply, divide. modulo and lloor division
♦ •
Addition and subtraction
>><<
Righi and lefl bltwllie shirt
&
Bitwise 'AND'
A
I
Bitwise exdusive 'OR' and n,gular 'OR'
Comparison oper.,tors
Equality operators
• 14.• / • //• .• • • •• •••
Assignment operators
Is Is not
Identity operators
In nolln
Memb<!rshlp operators
not or and
Logical ope.-ators
-For mathematical operators, Python follows
mathematical convention.
-The acronym PEMDAS (Parentheses,
Exponentiation,
Multiplication,
Division,
Addition, Subtraction) is a useful way to
remember the rules:
v Parentheses have the highest precedence
and can be used to force an expression to
evaluate in the order you want.
Since
expressions in parentheses are evaluated
first, 2 * (3-1)is 4, and (1 +1)**(5-2) is 8.
v You can also use parentheses to make an
expression easier to read, as in (minute
*
100) / 60, even if it doesn't change the
result.
v
Exponentiation has the next highest
precedence, so 1 + 2**3 is 9, not 27, and 2
*3**2 is 18, not 36.
v
Multiplication and Division have higher
precedence than Addition and Subtraction.
So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5.
v Operators with the same precedence are
evaluated
from
exponenti ation).
left
to
right
(except
Exampl,e:
a=9-12/3+3*2-1
a;;9-4+6-1
a=S,+6-1
a=11 ~ 1
a=10
A=2*3+4 %5-3/2,+6,
A=6+4%5-3/2,+6
A=6+4,- 3/2+6
A=6+4-1+6
A='10'-1+6
A=9+6
A,=15
find m=?
m=-43118&&0ll-2
m=-43110II-2
m=111-2
m=1
a=2 b=12 c=1
I
I
d=a<b>c
d=2<12>1
d=1>1
d=O
a=2 b=12 c=1
I
I
d=a<b>c-1
d=2<12>1-1
d=2<12>0
d=1>0
d=1
a=2*3+4%5-3//2+6
a=6+4-1+6
a=10-1+6
a=15
Download