NESTED IF STATEMENTS IN PYTHON
We have been using the if else statement for a while, and we are now quite familiar with the different things, in relation to if and else statements. At times, we are in situations, when we need to write some if statement, within some if block, and this is often referred to as nesting. Let’s have a look at a simple example, which demonstrates nesting.
Here as well, we would take some number as user input, and then we would check if the number is greater than zero, less than zero, or equal to zero. Let’s have a look –
Nested if statements Python
As you can see, in the above program, we are taking a number as user input, and then we are checking the conditions accordingly. Basically, first of all we are checking if the number is greater than or equal to zero. If this evaluates to True, we are again checking inside, that if the number is equal to zero, then we are printing that the number is zero, and else we are printing that the number is greater than zero. Also, we have the outer else block, which executes if the number is less than zero.
So, the basic thing here is that we are writing some if block, inside some other if block. There can be many different situations, in which we would require to nest the statements, and we can use them as and when required.
So, this was much about the conditional statements here, which are the if and else statements. In many situations, in our programs, we would make use of these conditional statements, and you can also try some programs, which require the use of conditional statements so that you can get quite more familiar with these statements.