Introduction
The real world and the programming world are quite similar in many cases, in real life you often make decisions based on certain factors, and these decisions will tell you what you need to do next, the same situations occur in programming where the program needs to decide which block of code want to execute next based on a condition given.
The Decision-making statements help us to make a decision and perform accordingly. In python, we use the if...else and elif statements for decision making.
Conditional tests
Before going to the concept of if...else, we need to understand the conditional tests, The if...else statement works based on an expression that is being evaluated as True or False known as conditional tests.
In conditional tests, we determine whether a given statement is True or False, an example is given below:
language = "Python" # Assigning valuelanguage == "Python" # Conditional TestTrue
First, we're assigning the value "Python" to the variable language using the "=" symbol, and then in the next line, we are checking whether the language is equal to "Python" using the "==" symbol.
This will return True since the value of the variable language is "Python". If the value assigned to the variable is different when checking conditions it will return False.
language = "Java"language == "Python"False
Python if statement
The if statement is one of the primary conditional statements used to make decisions in programming. Well, you often use if statements in your real life as well, "if there is rain, we need an umbrella". When comes to programming, the code written inside an if block is only executed when the condition become True.
if statement syntax
if conditional_test:statements_to_execute
The body of the if statement in Python is really simple to understand, first, we use the keyword if to inform the Python interpreter that we are going to perform a conditional operation, then we provide the test expression(conditional test) followed by a colon. The statements inside if block need to be properly intended with 2 or 4 spaces since python use indentation to declare a block instead of curly braces like in C, C++, Java, etc. Anyway, in most of the test editors and Python IDEs, the indentation will be put automatically after giving a colon.
Note that the statements that are written inside the if block will only be executed when the condition specified is true. else it will skip into the next block of code.
consider this code below,
if False:print("it's False")if True:print("it's True")output:it's True
This code illustrates that when a statement becomes False the code written inside the if will not be executed and if it becomes True, it will be executed.
Flow chart of python if statement
For better catching, let's draw the pictorial representation of the if statement.
![]() |
| Flowchart of if statement |
Example for If statement
Let's go through an example to get the point,
num = 3if num < 0:print("The number is negative")
You'll see nothing when you run this code since the number assigned to variable num is positive, our condition is to check whether the num is negative, So here the condition becomes False and you will not see the output.
But if you give a negative number for variable num you'll see the output printed to the console,
num = -3if num < 0:print("The number is negative")output:The number is negative
If ...else statements in python
The if statement only executes a particular block of code when the condition is True, but what if you want to do something in case of the condition became False. There comes the significance of else statements. Together with if statements, the else statement helps to execute a block of code when the condition is False.
if...else statement syntax
if conditional_test:# execute this block if statement is trueelse:# execute this block if statement is false
Flowchart of if...else statement
![]() |
| Flowchart of if...else statement |
Example 1: if...else statement
Let's go through the same example as we discussed early,
num = 3if num < 0:print("The number is negative")else:print("The number is positive")output:The number is positive
Suppose you want to check whether a number is negative or not you can use a simple if statement, but if you want to check whether the number is negative or positive you can use the if...else statement as shown in the above code.
Example 2:
Now let's do one more example to check whether a number is even or odd, the smallest even number is 2, so we can check if the given number is divisible by 2, if so it is an even else it is an odd number.
num = 10if num%2 == 0:print("The number is even")else:print("The number is odd")output:The number is even
The if-elif-else chain
In some cases, you often what to test more than two conditions, in those cases you can use the python if-elif-else chain to pass one condition among multiple conditions. In the if-elif-else chain, python only executes one block of code which is True for a certain condition. Python stops the execution of the if-elif-else chain after it finds the condition True.
if-elif-else syntax
if conditional_test:statement# If true execute this block# If false go futherelif conditional_test:statement# If true execute this block# If false execute else partelse:statement
Flowchart of if-elif-else chain
![]() |
| Flowchart of if-elif-else chain |
Example for if-elif-else chain
num = 0if num < 0:print("The number is Negative")elif num == 0:print("The number is Zero")else:print("The number is Positive")output:The number is Zero
You can use as many elif blocks on your code as needed. Here we added one more condition from the previous example to check whether a number is zero, if the number is zero the elif block will be executed.
Testing multiple conditions
So an if-elif-else chain helps to test multiple conditions and return an output when the condition become True, python skips the rest of the code in the if-elif-else chain if one of the tests is passed, but sometimes you need to check all the conditions and return all statements that are True. For instance, if you want to check whether a given integer is even, positive, and less than 10, you can use a series of if statements instead of an if-elif-else chain just like this:
num = 4if num > 0:print("The number is Positive")if num%2 == 0:print("The number is Even")if num < 10:print("The number is less than 10")output:The number is PositiveThe number is EvenThe number is less than 10
Here we are checking if the number 4 is positive, even, and less than 10, it's true in both of the three cases. But if you use the if-elif-else chain here, it will only print the first statement since whenever the condition become True, python skips the rest.
Nested if statements
An if statement inside another if statement is known as a Nested if statement. We can include as many nested if statements but it is good to avoid too many since our code become bulky.
Nested if statement syntax
if conditional_test_1:if conditional_test_2:statement..# End of inside if# End of outside if
Flowchart of nested if statement
![]() |
| Flowchart of nested if statement |
Example for a nested if statement
num1 = 3if num1 > 1: # Main conditionif num1 < 5: # Nested if blockprint("The number is between 1 and 5")else: # Nested if else blockprint("The number is greater than 5")else: # Else block of main conditionprint("The number is less than 1")output:The number is between 1 and 5
Ok, let's understand what's happening in the above case, an integer is assigned to the variable num, The main if statement will check whether the num is greater than 1, if not it prints the number is less than 1. If the number is greater than 1 the condition becomes True and the execution will go to the next if statement inside the main if statement(Nested if statement). If num is greater than 5 the execution will go to the else part of the nested if statement and print the number is greater than 5.
One-line if...else statements
So far you have seen how to code an if-else statement in python, these statements took 2 to 3 lines, but wait! python is more popular in shorthand techniques, so we can shrink the if...else statement for simple programs to one line just like this:
num = 11print("Less than 10") if num < 10 else print("greater than 10")output:greater than 10
Topics you may like:




