Python: “Conditional Statements in Python: The If-Else Statement”

Conditional statements are a basic programming concept that is used to govern the execution of a programme. In this post, we'll look at Python's If-Else statement and how it may be used to make decisions based on specific situations.

The most fundamental and extensively used sort of conditional statement is the If-Else expression. It enables you to run a piece of code only if a specific condition is met. In Python, an If-Else statement is written as shown in the output below:

The term "condition" refers to an expression that can be “True or False”. If the “condition” is “True”, the code in the “if” block is executed; otherwise, the code in the “else” block is executed.

Let's look at this example

Consider a simple use case in which we want to determine whether a given number is positive or negative. Here's how we can do it with an If-Else statement:

In the example below, we ask the user for a number, which is then saved in the number variable.

After that, we use an If-Else statement to see if the number >= 0. If the condition is True, the programme displays the message "The number is positive." If the condition is False, the programme displays the message "The number is negative."

When we enter the number 4, the programme displays the message "The number is positive, as shown below."

Let's provide another number to check the results. Observed how The "else" condition is printed out, which displays the message "The number is negative" because -1 is less than 0.

Note

It's important to note that the code in the if and else blocks must be indented because Python needs to know which code belongs to which block. It doesn't matter how many spaces are used for indentation as long as it is consistent within the same block of code. It is common practice to limit it to 4.

Determine the grade of a student based on their score.

Let’s take another real-life application of conditional statements in python. The If-Else statement can be used to determine a student's grade based on their score. Consider the following scenario: A student receives a score ranging from 0 to 100, and the grades are assigned as follows:

  • 90 or above: A

  • 80 to 89: B

  • 70 to 79: C

  • 60 to 69: D

  • Below 60: F

    Here's how we can use an If-Else statement in Python to calculate a student's grade based on their score:

In the above example, we ask the user for their score, which is saved in the score variable. Then, we use a series of If-Elif-Else statements to check the score variable's range and determine the corresponding grade. If the score is 90 or higher, the programme will display "Your grade is A." If your score is between 80 and 89, the programme will display "Your grade is B," and so on, as shown in the code snippet above.

It's worth noting that we used the “elif “keyword, which stands for "else if" in this example. This allows us to chain multiple conditions together, with the programme only executing the first one that is met. The code in the else block will be executed if none of the conditions is met.

Let’s provide a grade of 76 and check:

Observed how the programme displays the message "Your grade is C". This is due to the elif condition "elif score >= 70: which prints ("Your grade is C.") in the code snippet above.

You can try to try different numbers to check a grade based on the number entered.

Conclusion

This is how an If-Else statement can be used to make decisions based on specific conditions. This concept allows you to write code that can be customised based on user inputs and make decisions based on various scenarios.

Previous
Previous

Python: Nested Conditional Statements in Python: Understanding the If-Elif-Else Chain.