You are on page 1of 10

REVIEW PYTHON

Conditional Statements

▪ Sometimes the program needs to do different things for different conditions. In that
case the following statements are used:
▪ if statement
▪ if…else
▪ if…elif…else
if Statement

• In Python, if Statement is used for decision making. It will run the


body of code only when IF statement is true.
• When you want to justify one condition while the other condition is
not true, then you use "if statement".
Syntax:
If<condition>:
Statement
Example:
a = 33
b=
200
if b >
a:
a
Program displaying use of If condition

a=int(input(“Enter a number:”))
if a<100:
print(“The number is less than 100”)
if …else statement
▪ Syntax:
If:
[Statements to be executed if condition 1 is true]
else:
[Statements to be executed if condition 1 is false]

Example:
M=int(input(“Enter the marks:”))
if M >= 35:
print(“Congrats! You have passed the test!”)
else:
print(“Sorry! You have not passed the test””)

Explanation:
If the user enters 40, the message Congrats! You have passed the test! will appear on the screen.
If the user enters 30, the message Sorry! You have not passed the test will appear on the screen.
Program to find the Eligibility for voting

# Voting Eligibility
age=int(input("Enter your age")) //Simple Statement
pass //Empty Statement
if age>17

print(’Eligible to vote’)
else Compound
Statement
print(’Not Eligible to
vote’)
What happen when ”if condition” does not meet

Example:
a = 33
b = 200
if b > a:
print("b is greater than
a")
else:
print(“a is greater than
b")
WAP to check whether the given year is
leap year or not.

year=int(input("Enter year:")) if (year % 4) == 0:


if (year % 100) == 0:
if (year % 400) == 0: print(year, "is a leap year")
else:
print(year, "is not a leap year") else:
print(year, "is not a leap year") else:
print(year, "is not a leap year")
IF – ELIF-ELSE statement
▪ It is used when you need additional conditions.
▪ Syntax:
if <condition1>:
[Statements to be executed if condition 1 is true]
elif <condition 2>:
[Statements to be executed if condition 1 is false and condition 2 is true]
elif <condition 3>:
[Statements to be executed if condition 1,2 is false and condition 3 is true]
elif <condition 4>:
[Statements to be executed if condition 1,2,3 is false and condition 4 is true]
else:
[Statement (s) to be executed]
How to use "elif " condition?

▪ By using "elif" condition, you are telling the program to print out the third condition or
possibility when the other condition goes wrong or incorrect.
▪ Demonstrate:
x=int(input("Enter num1:"))
y=int(input("Enter num2:"))
if (x<y):
print("x is less than y")
elif (x == y):
print("x is same as y")
else:
print("x is greater than y")

You might also like