In this article, you will learn how to write Conditional Statements In Python. Before we get started, if you want to Data Type in Python, please go through the following article: Data Types in Python.

In programming, sometimes we need to execute an individual block of code only, and we might need to skip over some lines too. Conditional Statements give us greater control over our program. With conditional statements, we can make a block of code run or skip over depending upon the condition we provide. Conditional expressions, involving keywords such as if, elif, and else, provide Python programs the ability to perform different actions depending on a boolean condition: True or False.

If statement in Python

An if statement starts with if keyword which should always be written in lowercase followed by an expression. If the expression returns a True value then the statement below the if statement will get executed and if it returns False then the interpreter will skip over this part. Let’s look at a simple number comparison example;

The syntax is very specific therefore you must pay close attention to the layout. The if keyword must end with a colon(:) and the statements below the if condition should always be indented. Python relies on indentation to know which lines or block of code is contained under the if condition.

Always indent the statement by adding 4 spaces to the right of the statement. If you don’t indent your code, you will get an error ;

One line if statements in Python

We can also limit the if statement to one line like this,

We can add multiple statements by separating them with a semicolon (;)

Although it works as expected, it is advisable to spread the program in multiple lines to increase reliability.

Else Statement In Python

It is likely that we will like our program to do something else even when if statement evaluates to false. The else keyword lets us do that. By combining else and if statements we are constructing a two-part conditional statement, which tells the Python interpreter to run the block of codes depending upon the conditions.

In the above example, if the statement evaluates to false therefore else statement got executed. Similar to if, else keyword should always end with a colon(:) and the statement inside it should be indented.

Else if statement In Python

There might be a possibility of having more than 2 outputs in our program, and we want to perform condition operations for each output. This can be achieved by else if statement in Python.

The elif keyword is similar to the if statement which will evaluate another condition. Let’s again go back to our number comparison example where we might encounter a situation where both numbers are equal, using an elif statement can evaluate this condition.

Now we have conditions for all possible outcomes of this program.

Nested If Statements in Python

Once you have a good grasp of the basic conditional statements, You can create more advance conditional statements. When would you want to use nested if statements?

We should use nested if statements when we want to check a second condition if the first condition evaluates to True. Now let’s go back to our number comparison example again, this time we will add a nested if statement to check if the value of a is greater than 2 or not,

How To Write Conditional Statements In Python

The article was published on September 26, 2020 @ 2:30 PM

Leave a Comment