CONDITIONAL STATEMENTS IN PYTHON (1)

CONDITIONAL STATEMENTS IN PYTHON (1)

First of all, I gotta tell you: I am still in the process of starting my journey in Python and this article is a summary of what I have learned on the above topic. I love teaching and imparting knowledge and I am sure that if you have had issues with this topic in the past or still have one in the future, this article would totally undo any existing knot(s).

Let's begin.

Conditional statements in Python refer to those statements that execute Boolean operations. Boolean values evaluate to TRUE | FALSE statements in Python and a conditional statement is the machine used to drive this operation. The basic conditional statement in Python is the IF statement and its application would be seen shortly.

THE 'IF' STATEMENT

The following are the basic rules that apply to an IF statement and must be taken note of when using it:

  1. Every IF statement must be followed by:

  2. A Boolean value

  3. An expression that evaluates to a Boolean value.

  4. The code in the body of an IF statement must always be indented 4 spaces to the right or else you'll get an indentation error.

  5. An IF statement would only run in Python if the statement is actually TRUE.

Now that we know the basic rules governing the usage of an IF statement, we can now dive into writing our IF statements. Let's go!

SCENARIO 1

Let us assume that have data on 100 students in the class who either passed or failed Mathematics as follows:

Passed = 69 Failed = 31

Let us write an IF statement that prints the following result:

  1. If the number of students that passed is over 50, print ('extremely brilliant class')

passed = '69'

failed = '31'

if passed > failed:

result = ('extremely brilliant class')

print (result)

If you run this code on Pycharm, you get the result: 'extremely brilliant class'. Try it!

NINJA TIP: Did you notice the indentation on the 4th line of the code? Try to remove this indentation and see whether you get an error or not. This cautions you to always remember your indentation when writing an IF statement.

ELIF & ELSE STATEMENTS

You must be wondering what these are, right? I know I said I was only going to talk about IF statements but if you continue reading, you'll find out the meaning and relevance of these to creating our conditional statements.

After the first example, you must have one question on your mind (if you're naturally curious). There is a missing piece that needs to be added to have a full conditional statement. What if you have to add an extra condition to your statement? How do you factor it into your code without creating a tacky code?

The easiest answer is to say that you'll keep using the IF statements until you're done with all conditions. Here is an example:

SCENARIO 2:

Following the last example, let us move a level further and add to our data:

Passed = 31

Failed = 25

Repeated = 20

Advised to Withdraw = 24

Let us print a code where we compare the four information given above:

Passed = 31

Failed = 25

Repeated = 20

Advised_to_Withdraw = 24

if Passed > Failed:

result = 'extremely brilliant class'

if Failed > Advised_to_Withdraw:

result_2 = 'Review results'

if Repeated < Advised_to_Withdraw:

result_3 = 'Average Performance'

if Advised_to_Withdraw < Passed:

result_4 = 'Average Class'

print (result)

print (result_2)

print (result_3)

print (result_4)

First off, you cannot be a professional by writing codes like this. If you have to write long codes, this would be an inefficient method for you to employ. We want to write codes that save us time and energy.

This is where the ELIF and ELSE statements become relevant.

Now I would love to delve into these in this article, but one step at a time yeah? I would examine ELIF and ELSE statements extensively in the next part of this article.

Let me know what you learned and what you would like to add in the comments section.

Till then........ Thanks for reading!