Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Chapter (5)

Conditional Statement
Comparison Operators

Comparison operators are used to compare two values:

Operator Name Example


== Equal x==y
!= Not Equal x! =y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x>=y
<= Less than to equal to x<=y

Logical Operators

Logical operators are used to combine conditional statement:

Operator Description Example


and Returns True if both statements are true x>=5 and x<=10

or Returns True if one of the statements is true x<40 or y<40

not Reverse the result, returns False if the result is true not(x>=5 and x<=10)

Conditional Statement

The if statement is used test a condition. If condition is true, statement of if block is executed
otherwise it is skipped.

One-way Two-way Multi-way


if(condition): if(condition): if(condition1):

statements statements statements

else: elif(condition2):

statements statements

elif(condition3):

statements

else:

statments

Python Programming Ch 5-1


KMD Computer Centre
Create the “if.py”

str="Welcome!"

if str!="" :

print ("Hello " , str)

Create the “ifElse.py”

num=int(input("Enter Number : "))

if(num%2==0):

print (num , " is Even Number")

else:

print (num , " is Odd Number")

Create the “NestedifElse.py”

num=int(input("Enter Number : "))

if(num<0):

print (num , " is Negative")

elif(num>0):

print (num , " is Positive")

else:

print (num , " is Zero Number")

Create the “andLogical.py”

staffage=int(input("Enter age : "))

if (staffage>=18 and staffage<=60):

print ("It is Ok!!")

else:

print ("Invalid Age")

Create the “orLogical.py”

Python Programming Ch 5-2


KMD Computer Centre
mark1=int(input("Enter English Mark : "))

mark2=int(input("Enter Mathematic Mark : "))

if(mark1<40 or mark2<40):

print ("Fail the Exam")

else:

print ("Pass the Exam")

Exercise

• Accept the student’s mark and check this mark. If mark is between 100 and 40, should be
display “Pass the Exam”. If mark is between 39 and 0, should be display “Fail the Exam”.
Otherwise, display “Invalid the Mark”.

• Write a python program to accept user name and password from user and to check user name
and password using two-way conditional statement and display it.

Python Programming Ch 5-3

You might also like