Python if else Statement with Examples


How to Use if, elif and else Statements in Python

In this tutorial, you'll learn how to use ifelif, and else statements in Python. The Python if-statement is a control flow statement that allows you to execute a block of code.

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

The if-else Statement Syntax

The following illustrates the syntax of the if-else statement:

if expression:
statement_list
else:
other_statement_list

If the expression evaluates to True, the program executes the statement_list block. Otherwise, executes the other_statement_list block.

The 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 block, otherwise checks the condition of the next elif block and so on. However, if all of the specified conditions are false, the program executes the else_statement block.

Python if-else Examples

Example 1: if statement

The following code illustrates an example of using the if-statement:

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

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

Let's take a look at another example:

The following code illustrates how to use the IF statement:

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

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

Example 2: if and break Statements

The following code illustrates an example of using the if and break statements inside the for loop statement:

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

Output:

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 the variable-i until it is equal to 3. The program breaks the loop when i is equal to 4.

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

The following code illustrates an example of how to use the if-statement and the for loop statement 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, the program compares all of the numbers, in the list, with the variable named max.And, if the number is greater than the max, assign it to the variable max.

Output:

40 is the largest value!

What is a list in Python?

Example 4: if and continue Statements

The following code is an example of how to 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)

Here, range(10) returns the sequence of numbers, which starts from 0 to 9.

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

Output:

Total is 24

How to use the range() function in Python

Example 5: if and else Statements

The following code illustrates an example of using both if and else statements:

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

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

x is less than y.

Example 6: if, elif and else Statements

Let's take a look at an example of using the three statements together:

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.")

The program compares the variable x and y. There are three 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, you've learned how to use if, elif, and else statements in Python. The if-statement is a flow control statement that allows you to execute a block of code only if it meets a specified condition.

Leave a Reply

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