Python IF ELSE Statement with Examples


How to Use the Python IF ELIF ELSE Statement

In this tutorial, we'll learn the Python IF ELIF ELSE statement. Python IF is a control flow statement, used to execute a block of code if a certain condition is met.

Python IF Statement Syntax

The following illustrates the syntax of the IF statement:

if expression:
statement_list

Here, the program evaluates the expression and executes the statement_list only if the expression is True. If the expression evaluates to False, the statement_list is not executed.

IF ELSE Statement Syntax

The following illustrates the syntax of the IF ELSE statement:

if expression:
statement_list
else:
other_statement_list

Here, if the expression evaluates to True, the program executes the statement_list. Otherwise the other_statement_list is executed.

IF ELIF ELSE Statement Syntax

The following illustrates the syntax of the IF ELIF ELSE statement:

if if_expression:
if_statement_list
elif elif_expression:
elif_statement_list

else:
else_statement_list

If the if_expression evaluates to True, the program executes the if_statement_list, otherwise checks the condition of next elif block and so on. However, if all the conditions are False, the else_statement is executed.

Python IF ELSE Examples

Example 1: IF statement

The following code illustrates how to use the IF statement:

x = 200; y = 100;
if x > y:
print ("200 is greater than 100")

Result:

Since x > y, the expression x > y evaluates to True, the program executes and prints the message "200 is greater than 100".

Example 2: IF statement

The following code illustrates how to use the IF statement:

x = 100; y = 200;
if x > y:
print ("100 is greater than 200")

Result:

If the expression x > y is evaluates to True, the program prints the message "100 is greater than 200". Since x is less than y, none of code is executed.

Example 3: IF and Break

The following illustrates how to use the IF statement inside a For loop:

for i in range(10):
if i == 4:
break;
print (i)

Result:

0
1
2
3

Note that the range(10) returns a sequence of numbers, starting from 0 to 9. The program prints the value of i until i is equal to 3. When i is equal to 4, the loop breaks.

Example 4: Find the Largest Number in a List of Numbers

The following code illustrates how to use the IF statement inside a For loop to find the maximum value in a list of numbers:

numbers = [10, 20, 30, 20, 40]
max = 0;
for number in numbers:
if number > max:
max = number
print (max, " is the largest value!")

Here, we compare each number in the list with the variable max, and if the number is greater than max, assign that number to max.

Result:

40 is the largest value!

Example 5: IF and Continue

The following code sums a sequence of numbers, starting from 7 to 9:

sum = 0;
for i in range(10):
if i <= 6 or i > 9:
continue;
else:
sum += i;
print ("Total is ", sum)

Note that the range(10) returns a sequence of numbers, starting from 0 to 9.

As you can see, a block of code in the else statement is executed only if i is greater than or equal to 7 and less than or equal to 9.

Result:

Total is 24

Example 6: IF and ELSE

The following code illustrates how to use the IF ELSE statement:

x = 100; y = 200;
if x > y:
print ("x is greater than y.")
else:
print ("x is less than y.")

Result:

This program compares 2 variables, x and y. Since x is less than y, the program prints a message as follows:

x is less than y.

Example 7: IF, ELIF and ELSE

The following code illustrates how to use the IF ELIF ELSE statement:

x = 200; y = 200;
if x > y:
print ("x is greater than y.")
elif x < y:
print ("x is less than y.")
else:
print ("x is equal to y.")

Result:

This program compares the variable x and y. There are 3 conditional statements in this program. However, since x and y are equal, the else statement is executed, and the program prints a message as follows:

x is equal to y.

In this tutorial, we've learned the Python IF ELSE statement. IF is a flow control statement that allows us to execute a block of code only if a specified condition is met.

Leave a Reply

Your email address will not be published. Required fields are marked *