CONDITIONAL STATEMENTS IN PYTHON (2)
You know that moment when you think you've got it all figured out? That great enthusiastic feeling that comes with starting your blog?! You start with so much energy initially and rapidly go off the next second.... Well, that was exactly what happened to me. I wrote the first part of this article over a month ago but here I am delivering the second part a long time after. If you read the first and I left you hanging for so long, I am sorry! I still don't have it all figured out but I promise to do better.
Alright, let's dive right into business!
I walked you through the definition and classes of conditional statements in the last article coupled with certain scenarios (codes) which explicitly illustrated what the message I was trying to pass across. I ended the article with an introduction to the 'else' and 'elif' clauses.
In the second and final part of this article, I would be explaining broadly the aforementioned conditional statements as well as their relevance to our codes. Also, I would make explanatory illustrations to back up the notes.
When creating conditional statements, you, sometimes need to include additional exceptions and conditions to be executed in your codes. Now, you could continue to repeat the IF clause until you have created a satisfactory conditional statement but this approach becomes inefficient and tedious when you are dealing with larger codes. The clauses to be examined in this article will introduce you to highly efficient and time-saving ways we can write our conditional statements for both small and large datasets.
ELIF CLAUSES
ELIF statements are shorter and more effective forms of creating conditionals. An important benefit of using the ELIF clause is that the code used along with it only gets executed if the preceding code returns a FALSE result and the succeeding code returns a TRUE result.
Let's illustrate this with our previous example:
In a class of 100 students, their grades divided into the following segments:
Passed = 31
Failed = 25
Repeated = 20
Advised to Withdraw = 24
Requirement: Create a conditional statement using the IF and ELIF clauses.
total_students = {'Passed': ['31'], 'Failed': ['25'], 'Repeated': ['20'], 'Advised_to_Withdraw': ['24']}
for students in total_students:
if 'Passed' < 'Failed':
print('extremely dull class')
elif 'Failed' > 'Advised_to_Withdraw':
print('Review results')
elif 'Repeated' < 'Advised_to_Withdraw':
print('Average Performance')
elif 'Advised_to_Withdraw' <= 'Passed':
print('Average Class')
OUTPUT: Review results
This resulting output is printed because of the condition attached to the execution of ELIF clauses which has been previously highlighted. Only condition 2 is executed because Condition 1 and 3 provide a FALSE and TRUE result respectively; thus, Condition 2 provides the ultimate output.
The ELIF clause is commonly used where you have large iterations to run and do not want all your conditionals to continually run at every iteration the computer makes. This increases the speed and accuracy of running the conditionals.
ELSE CLAUSES
This is also a very efficient clause that can be used for executing your conditional statements. The difference between the ELSE and ELIF clauses is that the former prints ONLY where the conditional statement that precedes it produces a FALSE output while the latter prints where FALSE and TRUE conditionals precede and succeed it respectively.
ELSE clauses serve as a last resort command in cases where you want to provide an alternate result to your conditionals which have failed to meet certain criteria. This is illustrated by tweaking the previous code as shown below:
total_students = {'Passed': ['31'], 'Failed': ['25'], 'Repeated': ['20'], 'Advised_to_Withdraw': ['24']}
for students in total_students:
if 'Passed' < 'Failed':
print('extremely dull class')
elif 'Failed' < 'Advised_to_Withdraw':
print('Review results')
elif 'Repeated' < 'Advised_to_Withdraw':
print('Average Performance')
else:
print('Average Class')
OUTPUT = Average Class
According to the condition that guides the execution of the ELSE clause, our result is the print statement attached to the clause. This shows that without the ELSE clause added, the iteration would have provided no output at all. So, in cases, where you want to avoid such a situation, the ELSE clause comes in handy.
NOTE THAT both the ELSE and ELIF clauses can only be used in conditional statements when they are preceded by an IF clause. If they are used alone without the IF clause, your code(s) run into an error and you do not want that especially when you are dealing with large datasets.
Also, best practice of using these clauses should not be forgotten as they are fundamental to the successful running of your codes. A fundamental rule that must be etched in your mind is to never forget your accurate indentation whenever you are writing your conditional statements.
Why don't you try out these new clauses today and see what you can do with them?!
Let me know what you learned or is unclear to you in the comment section below and don't forget to give a THUMBS UP!
Talk to you soon and HAPPY CODING!!!