Unit 2 Notes Python Operators and Control Flow Statements Part 1
Unit 2 Notes Python Operators and Control Flow Statements Part 1
Unit- J I
Python OperatorS an d
Control Flow StatementS.
-10 MarkS
2-1 Basic Op erator S
- An operator is Sinqle Or Set_af specia Sumbols
that pertormS pre-defined eperations on apeiators
Puthon hasa
arich Set o f operatorS which
aie divided into fallowing Seven
gaupsi
1,Arithmetic operataS
R Comparison/ Relationql aperatorS
3ASsignment Qperatarg
4 Loqical 0peratorS
5 Bitwise OperatarS
6, Membershu p Operators
7 Tdentity Operqtors
ArithmetiC Operat or
Arith metic OperatarS aie to per for m
arithmetic operationS on two operands.
Operator Name Deseri ption
addition performS addition of two operands
Subtra ction Perfor ms Subtrcaction cf opG two
Operator Eaample
+ +y
*
Output
Additi on is:19
Subtraction is 11
Multiplication is 60
float Division is: 3.75
Floor Di vision is 3
Remaander is:3
false
3Assignment perator
-The agsignment operator IS to assiqn
of 1ight operand o
the value /1eS ul t
exp1ession to the vari able /1efe1enee on
left The assignment opesator is
single
equals-to- Bign
Des cription Ezarnple
perator Name
value / 10
assiqn ment AsignS
Resut of right OR
operand nariable z=10+20
Cpresson to the
b a=a>>b
Eaponent AND Calcul ote ezpon ent
Naue uSing operá'ndsS
and assi qn value to
teft oper and
Ezample
la b,c,d,e,f.g,hi,i k,A,m,na,2,3,45,7
e= f
9/ h
m**h
print(at :b resuts a.s "a)
print CC - d esultS a s ' c)
print ( e * f reswtS as:", e)
print(q/=h 1esul ts aS", 9)
pint i = j 1esults a9:",i)
print (" K%: eswts as: R
print ( m ** =n resultS aS ", m)
Outpu
atb 1ests as3
C- d vesultSs aS -
e*tTesults as 30
g / - h YesultS as o.875
II 1eSultS a s 0
1esult9 as I
m * =n resulls as : 3937376385 61928
4Logical Operators
Lagica operotorS re to test multiple
b ll1o001|
print a b esultS as a &b)
print(alb1eSultS a s , alb)
print("anb YesultS aS " a ^b)
Output
a 8 b regultS as 8913 250
albe eSultS as12 297871
a Ab_nesultS a9: 3374621
a a esultS ag -lojoLLL
resultS aS 404 O 4 44 6
ai<
a>z2 YeSultS aS; 2775002
6Membership operato1S: -
PIDt(presence of 25 in inlist 1)
listl is , 25
is: 1a intuple 1)
pTint ( presence of 10 in tuple
25 in tuplel1
print("presence of 25 in tuple 1 is:
is ", P in stuing1)
prnt" presence of P in Strinq1
0 ut put
Presence of l o in l i s t 1 is True
PreSence of 25 in LiSt 1 iS: folse
1 is false
Presence of LO in tuple
Presence of 2S in tuple t is : True
p1esen Ce Ot P in String 1 is: True
7 Tdentity aperators:
Python dentity aperatorS a e to compaie
b 5
locations 2 True
Does a andC points to Same memOTy
ocations ? fodse
Does a and b not pointS to Same memoi
locations ? Pcd s e
Does a and C notpointS to Same memany
locaticnS 2 True
a J i s t (1anqe Cio)
print a )
will ies ult a9
specifiedStep(inciement_/decement)
having
having two paiamete1S, the Stepis by-
dlefalt t l
for e1ample-
alist( Tanqe C,20,+2))
priOtCa)
wi_1esult_ aS_
[ , 3 ,5, 7,9, 13, , 15,17, 19
yoA Can even try following 9tCte PmeTIt
a- list yanqe (205 2
print a)
will eSult a s
[20, 18, 16, 14,12, 108, 6]
Statemnent-- n
The mos t impo1tant thing 1s
Python's
Ony
Ony contral flow 9tatement Containsg a colon
and then inden tation to
Specitythe bods.
Here, if the speci Pie d condition is rue
then anly block of statement ezet utes
a»ly
atherwise gets Skipped.
The Pollo winq python prOqTam illustiates
the only ane if condition
print Before if Statement")
if a> 10
print ("a is greater than 10 )
print( After if Statement)
O/P
Befo re it statement
a is gieater than 10
After f Statem ent
if-StatemmentSare independent
following pOq1am ContainS One prom ptolso
W e can
me.9saqe in inpuut ) Stotement
use logical operators to chec multiple condiio
ng.
The fallowinq Pythonprogram 1Sezample
-it Ctatements to find targest
of
of multiple
thiee valueS
Py thon Proqram:
print ("Enter three numberS
n=intCinput O)
haint(input )
n3int(inputO)
if n n 2 and n n3
print( n1, "is larqes t ")
if ngn and ng Da
print (n2, "is larqeSt "
itn3 n and ngn2:
pxint Cngis lasqest")
O/p
Enter thiee numberS
20
30
30 is larqest
S t a t em e n t - n
ese
Statement - 1
Statement - 2
Statement-n
Python Proqram
print ( Enter a number: ")
num int(input )
f num %2:<o:
print Cnum, " is even")
else
print (num TS Odd
/p
Enter a numbder
20 is odd
0/p
List 1 contouns 10
List1does not contoin 2a
Statement:
Guntaz:
if condition-1
Statement-1
Statement-2
Statement-n_
elif condition-2:
Statement-1
Statement -2
Statement -n
elif con dition-n:
Stctem ent- 1
Stoutement-2
SEatement-n
else
Stotement-1
9tatement-2
Statem ent -n
Specia sumbol.
-Python Proqram
va input("Enter any character")
ch ord (val)
if ch2 65 and ch<-9o:
print"It is an upper case Jetter")
elif ch2= 97 and ch<= 122
C/p
Enter a n eharacter
N
etter
Tt is an uppercase
4 nested-if Statement :
The nested-if Statements are algo test
nested Conditiong.
Suntax :
if condition-1:
if condition -2:
if condition-n:
Statene nt - 1
Stctement -2
Statem ent - n