Page 110 - TCS ICT Book 8
P. 110
The City School 2021-2022
5.7. Conditional Statements in Python
Conditional statement is a set of rules performed if a certain condition is met.
IF Statement
if statement is a programming conditional statement that, if proved true, performs a
function or displays information. An if statement in python is written by using the if
keyword. The sample code below shows the if statement in python.
Sample code:
# This program compares two numbers using if
a = 33
b = 200
# if block
if b > a:
print(“b is greater than a”)
In this example, we use two variables, a and b, which are used as part of the if
statement to test whether b is greater than a. As a is 33, and b is 200, we know that 200
is greater than 33, and so we print to screen that “b is greater than a”. Indentation
is necessary; if we do not use the indentation as mentioned in example, python will give
an error.
Elif Statement
The elif keyword is Python’s way of saying “if the previous conditions were not true,
then try this condition”. The sample code below demonstrates Elif statement.
Sample code: Del command is used to delete a list
element whereas append() method is
# This program compares two numbers using elif used to add an item to the list
a = 33
b = 200 Checkpoint
# if block
if b > a: List is just an ordered collection of items
which can be of any data type.
print(“b is greater than a”) Del command is used to delete a list
element whereas append() method is
# elif block used to add an item to the list.
elif b == a:
print(“a and b are equal”)
110

