Professional Documents
Culture Documents
THE If STATEMENTS OF PYTHON
THE If STATEMENTS OF PYTHON
OF PYTHON
The if statements are conditional statements
in Python and these implement selection
constructs.
An if statement tests a particular condition;if
the condition evaluates to True, a set of
statements is executed. Otherwise,the set of
statements is ignored.
THE IF STATEMENT
The simple if statement tests a condition and
if the condition evaluates to True,it carries
out some instructions and does nothing in
case condition evaluates to False.
Syntax:
if <condition>:
Statement(s)
where Statement(s) may consists of a single
statement, a compound statement or just
the pass statement.
Example:
ch=‘ ’
if ch==‘ ‘:
space=1
chars=1
Example:
ch=input(“Enter a single character”)
if ch>='0' and ch<='9':
print("you entered a digit")
if ch>='A'and ch<='Z':
print("upper case")
if ch>='a' and ch<='z':
print("lower case")
if ch==' ':
print("space")
else:
print(“spl character”)
IF ELSE STATEMENT
The form of if statement tests a condition
and if the condition evaluates to true, it
carries out statement indented below if
In case condition evaluates to false ,it
carries out statements indented below else.
Syntax:
if <condition>:
Statement(s)
else:
Statement(s)
Example:
a=10
if a>=0:
print(“a is zero or positive”)
else:
print(“a is negative”)
a=int(input(“enter a number”))
if a%2==0:
print(a, “is even”)
else:
print(a, “ is odd”)
PROGRAM TO ACCEPT 3 NUMBERS AND
PRINT THE LARGEST OF THE THREE
x=y=z=0
x=int(input(“enter the I st number”)) #20
y=int(input(“enter the second number”)) #100
z=int(input(“enter the third number”))#500
max=x # max=500
if y>max: #100>20
max=y
if z>max: #500>100
max=z
print(“The largest number is”,max)
THE IF –ELIF STATEMENT
Sometimes if we want to check a condition
when control reaches the else part
ie.,condition test in the form of else if.
Syntax:
if<condition>:
statement(s)
elif <condition>:
statement(s)
........
else:
statement(s)
Example:
ch=input(“enter a character(‘+’,’-’.*’,/’)”)
a,b=10,5
if ch==‘+’:
print(“the sum of a and b is”,a+b)
elif ch==‘-’:
print(“the difference of a and b is “,a-b)
elif ch==‘*’:
print(“the product of a and b is”,a*b)
elif ch==‘/’:
print(“The quotient of a and b is”, a/b)
else:
print(“invalid”)
print(”thank u”)
NESTED IF
A nested if is an if that has another if in it’s if’s body
or in elif’s body or in its else body.
FORMAT OF NESTED IF(1):
if<condition>:
if<condition>:
statement(s)
else:
statement(s)
elif <condition>:
statement(s)
else:
statement(s)
FORMAT OF NESTED IF(2):
if<condition>:
statement(s)
elif <condition>:
if <condition>:
statement(s)
else:
statement(s)
else:
statement(s):
FORMAT OF NESTED IF(3):
if<condition>:
statement
elif<condition>:
statement
else:
if<condition>:
statement
else:
statement
FORMAT OF NESTED IF(4):
if <condition>:
if <condition>:
statement(s)
else:
statement
elif<condition>:
if <condition>:
statement(s)
else:
statement(s)
else:
if<condition>:
statement(s)
else:
statement(s)
PROGRAM
a=int(input(“enter a number”)100
b=int(input(“enter a number”)500
c=int(input(“enter a number”)4000
if a>b: 100>500
if a>c:
g=a
else:
g=c
else:
if b>c: 500>4000
g=b
else:
g=cg=4000
print(“greater=“,g)
OUPUT (1)
if(4+5==10):
print(“true”)
else:
print(“false”)
print(“true”)
OUTPUT(2)
x=1
if x>3:
if x>4:
print("A",end=" ")
else:
print("B",end=" ")
elif x<2:
if x!=0:
print("C",end=“ ")
print("D")