Page 136 - CodePilot V5.0 C7
P. 136
Syntax of the if statement:
if (condition): Start
Statement(s)
Input Age
The if statement starts with the keyword if, followed by a condition
and ends with a colon(:). The condition is essentially an expression
(comparison) that gives True or False as an answer. False
Is Age>18?
After the if condition, on the next line, write an indented block
of statements. When the condition evaluates to true, the block of True
statements given below it is executed; otherwise, it is skipped and
control flows to the statement after if. Print ‘Eligible’
Here, if the age of the user is more than or equal to 18, ‘Eligible’ will
be printed, otherwise nothing will be printed. Stop
Program 1 To demonstrate the use of the ‘if’ statement.
Program1.py
File Edit Format Run Options Window Help
age=int(input("Enter Age: "))
if(age>=18):
print("You are eligible to cast a vote")
Output
Enter Age: 20
You are eligible to cast a vote
Enter Age: 16
Program 2 To greet the user based on the time entered.
Program2.py
File Edit Format Run Options Window Help
time=int(input("Enter Time in 24-hour format: "))
if (time<12):
print("Good Morning")
134
CodePilot (V5.0)-VII

