Conditional test is the heart of if statements. In the last article you learned about conditional tests which evaluate to either True or False. These Boolean values determine the block of code to be executed.
In this article, you’ll learn to write if statements to handle various situations.
if statements
A simple if statement tests a condition and takes one action:
if conditional_test:
take an action
Consider the Dating app/website from the previous article. We want to print a message to any visitor of 18 years or above that they are eligible to sign up.
The above code prints a message to the visitor when the condition is passed (evaluates to True).
We can extend this solution to test multiple conditions as age is not the only factor for eligibility. We want to make sure that the chosen username does not exist on the website too.
The first nested condition tests for age while the second checks for the presence of the username in a list. The condition would only evaluate to True when both conditions are True.
The indented code belongs to the if block. Any un-indented code would run even if the conditional test fails. Indents in Python are indicated by four white spaces (hit the space bar four times).
Let’s put a print statement outside the if block
As you can see, the message gets printed even after a failed conditional test.
Please, note that the if condition is followed by a colon symbol (:). The colon symbol signifies the end of the conditional test. The line after this must be an indented code.
Let’s see what happens when we fail to indent the next line of code.
if-else Statement
The previous program only prints a message when the if condition is True.
if-else statements can be used to handle both True and False conditions. The else block contains the code to be executed when the if condition evaluates to False.
The if-else structure takes the following syntax:
if conditional_test:
do something
else:
do this
Let’s include an else block to print a message to visitors below the age of 18.
This structure is suitable when there are only two situations to handle.
if-elif-else Chain
This syntax can be used to handle more than one situation. As mentioned earlier, the if-else block handles only two situations – when the condition passes and when it fails.
Other situations can be included in the elif blocks. You can have more than one elif block, one per situation.
The possible situations for the registration process for our dating app/website could be:
- Age greater than or equals 18 years old and username available
- Age not greater than or equals 18 years old and username available
- Age greater than or equals 18 years old and username not available
- Age not greater than or equals 18 years old and username not available
These conditions can be handled with 1 if statement, 2 elif statements and one else statement respectively.
Test this code with several age and username values.
The else code is optional. The previous code can be implemented with 1 if statement and 3 elif statements. The else block only serves as a fallback when every condition has failed.
It would be better to handle the most important situations with if and elif statements and handle other situations with the else statement. This way, you can maintain your code easily.
Notice how only one block of code is executed so far. Once a block gets executed, Python ignores the rest blocks.
Once the if condition is passed, the code in the if block gets executed and the remaining blocks are ignored.
Multiple if Blocks
Various blocks of codes can be executed with multiple if blocks. We can test for age and username eligibility separately. This way, the two block of codes get executed when both conditions are met
This is just a simple logical representation. Building an actual app/website goes beyond testing conditions and printing messages. There are various technologies for handling user data, the app/web structure and its performance.
This logic can help you in developing an efficient and effective flow for your website or app.
Summary
- Code included in if blocks are executed when the conditional tests evaluate to True
- Indents in Python are indicated by four white spaces
- The else block contains the code to be executed when the if condition evaluates to False
- elif blocks an be used to handle multiple situations
- The else block is not compulsory in Python
No comments: