Page 184 - Computer Science Class 11 Without Functions
P. 184
The conditional expression may not include the use of relational operators. Consider the following examples:
>>> x = 10
>>> if x:
print("Condition is True") # if block is executed
In Python, any non-zero value (other than None) is considered to be Boolean True. In the above example, as the
value of x is 10, the Boolean expression x yields True. Therefore, the conditional expression of the if statement
yields True.
>>> if None:
print("Condition is not True") # if block isn't executed
In the above example, the conditional expression None yields False. Therefore, the if block following the
condition None:
print("Condition is not True")
is ignored by the Python interpreter.
8.3.2 if-else Statement
ThtlifltSnStetnSltxtcuSttlnlttqutnctlofltSnStetnStlwhtnlShtlconditonnaltxprtttionlyitadtlTrue.lThtltSnStetnS(t)l
inlShtliflbaocklit/nrtlignortdlbylShtlPyShonlinStrprtStrlwhtnlShtlconditonnaltxprtttionlyitadtlFalse.lHowtvtr,l
toettett,lctrSninltSnStetnStlnttdlSolbtltxtcuStdlwhtnlShtlconditonnaltxprtttionlitlFalse.lInltuchlnltiSunton,l
nn else canuttlitluttd.
Syntax:
if <conditional expression>: ………htndtr
<sequence S1 of statement(s)> ………ifltuiStl/lbaock
else:
<sequence S2 of statement(s)> ………else tuiStl/lbaock
TttSl Fnatt tqutnctlofl SnStetnStl
conditon (elselbaock)
Trut
tqutnctlofl SnStetnStl
(iflbaock)
Figl8.4:lFaowldingrneloflnnlif-elseltSnStetnSl
InlShtlgivtnltynSnx,lShtrtlnrtlSwolhtndtrlcanuttt:liflnndlelse.lThtlindtnStdlttqutnctlofltSnStetnStlinlShtlif tuiStl
(natolcnaatd,lif baock)lwiaalbtltxtcuStdlwhtnlShtlconditonnaltxprtttionlyitadtlTrue.lThtlelseltuiStl(natolcnaatd,l
elselbaock)lwiaalbtltxtcuStdlwhtnlShtlconditonnaltxprtttionlyitadtlFalse.lContidtrlShtlfoaaowingltxnepat:
Program 8.1 WriStlnlprogrnelSolchtcklwhtShtrlnlnuebtrltnStrtdlbylnluttrlitltvtnlorlodd.
Solution:
01 # To check whether a number entered by a user is even or odd.
02 num = int(input("Enter a number: "))
03 if num % 2 == 0:
04 print("Number is EVEN")
05 print("Division by 2 leaves the remainder Zero")
182 Touchpad Computer Science-XI

