In this article, we will learn how to alter the flow of code based on the conditional expression using the if…else statement.
While developing applications, for a programmer, it is essential to know how to control the flow of code using conditional expressions. In the previous article, we discussed how we could use if-statement to conditionally execute a suite ( a group of statements) in the if-block. Like, if the condition provided in the if-statement evaluates to True, then run the code in if block, otherwise skip the code in if block i.e.

But what for the scenarios where the condition in if-statement evaluates to False?
We don’t want to skip to the code after the block. Like we want to run some statements if and if the only condition in the if-statement evaluates to False. Here comes the else keyword in the picture.
Flow chart of if..else in Python
After the if-block ends, we can define an else block with some statements (a suite). The code in the else block will only execute if the condition in if-statement evaluates to False.
Syntax of if..else statement,
if condition: statement 1 statement 2 statement 3 else: statement 4 statement 5 statement 6
As soon as the interpreter encounters the if statement, it evaluates the condition in the if-statement, and if that condition evaluates to True, then it executes the suite in if-block, i.e., statements in the if-block and skips the statements in else part.
Whereas, if the condition in if-statement evaluates to False, then it executes the suite in else block directly, i.e., statements in the else block and skips the statements in if block.
Let’s look at some example of if..else
Examples of if…else statement in python
Example 1:
x = 15 print('Change the flow') if x > 10: print('x :: ', x) x = x + 1 print('x :: ', x) else: print('x is fine') print('No need to change the x') print('Last statement')
Output:
Change the flow x :: 15 x :: 16 Last statement
Here the first two statements were executed in a sequence. But after that, the interpreter encountered an if-statement and then evaluated the condition in that if-statement. As x is 15, which is greater than 10, so the condition evaluated to True. Therefore, the interpreter executed the statements in the if-block and skipped the lines of code in the else-block.
After running the lines in the if-block, it directly jumped to the statements after the else block. The condition in the if-statement evaluated to True, so it executed the code in if-block only and skipped the else-block.
Let’s look at another example of if..else statement
Example 2:
x = 7 print('Change the flow') if x > 10: print('x :: ', x) x = x + 1 print('x :: ', x) else: print('x is fine') print('No need to change the x') print('Last statement')
Output:
Change the flow x is fine No need to change the x Last statement
Here the first two statements were executed in a sequence. After that, the interpreter encountered an if-statement and then evaluated the condition in it. As x is seven, which is smaller than 10, so the condition is evaluated to False. Therefore, the interpreter skipped the statements in the if block. Instead, it directly jumped to the else block to ran the statements in it. After that, it executed the last statement, which is after the if-else.
If the condition in if-statement evaluates to False, then the code in the else block gets executed, and code in if-block gets skipped.
Conclusion:
So, this is how we can use the if…else statement in python to write a code in which the flow of execution can be controlled and directed to any one of the two directions, based on the condition results.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.