Python Programming If Else Statement Tutorial with Stone River eLearning

In our last blog post we discussed about the “if statement” in Python and mentioned that the “if else” statement is a better option than writing consecutive “if” statements in Python. In this blog post as part of the series covering the Python for Beginners’ course, we are going to cover the “if else” statement and discuss the details about it.

How is “if else” executed in Python?

The execution of a ‘if else’ statement in Python is quite similar to the ‘if statement’ that we discussed in the previous blog post. In fact, the ‘if else’ statement is actually an upgrade to the ‘if statement’, with the latter giving the program more sense and structure. In layman terms, when we give a condition for a “if statement” to execute, the operation inside the ‘if’ block is executed when the condition is satisfied. But, what happens when the condition is not satisfied? This is where the ‘else’ statement comes in play.  The else statement covers the situation when the condition for the ‘if statement’ does not hold true.  I know what you are thinking, ‘Why should I use the else statement when I can give another if condition’? As we have already mentioned, using an ‘else’ condition gives more sense and structure to a Python code. However, from a more technical perspective, using consecutive ‘if statements’ means that the compiler needs to check for conditions for each one of them, thus the performance of the Python code takes a toll. The else statement gives a more performance savvy solution as it is automatically executed when the ‘if’ condition does not hold true.

Eg:

x=2

y=3

if x > y:

print(x,’is greater than’,y)

else:

print(x,’is smaller than’,y)

Output:

2 is smaller than 3.

As you can notice, the condition in the ‘if statement’ does not hold true and therefore the control jumps to the else statement and the print statement inside it is displayed on the console. Before we knew about the ‘else’ statement, we were using another ‘if statement’ to execute the same program, with the else statement, the program becomes a lot more organized and faster.

Note:

  • There are a few things that you must keep in mind while using the ‘if else’ statement in your Python code. To start with, one thing that we have regularly cautioned the beginners’ in Python about; the proper use of colon (:) in your code and especially after the statements that lead to a flow control loop.
  • The other important thing that many programmers miss, is the legitimate use of the else statement. An else statement is only valid for the condition that is directly precedes it and therefore any statement before that holds no control over the functioning of the operation inside the else statement. To explain it in a better way, let us have a look at this example.

Eg:

x=2

y=3

if x<y:

print(x,’is less than’,y)                        #1<sup>st</sup> if statement

if x &gt; y:

print(x,’is greater than’,y)               #2<sup>nd</sup> if statement

else:

print(x,’is not less than’,y)

Output:

2 is less than 3

2 is not less than 3

As you can notice in the above example, the else statement is executed for the condition in the 2nd if statement and not for the 1st if statement and therefore you will see the following output in the console. This highlights another point that the condition in the ‘if statement’ and the operation to be performed by the ‘if’ block and the ‘else’ block must be accurate and logical in sense to deliver the desired results on the console.

Online Technology Courses Stone River eLearning
GET YOUR FREE PYTHON EBOOK!

Comments