Python (Chapter 2.2) : Nested IF
For any type of consultation, query or doubt. You may contact to the following: (+91) 9804 436 193 debabrataguha20@gmail.com
and join the group https://www.facebook.com/groups/331231161093613/
Hope the basic understanding of conditional statement is clear now. But if you are starting your python learning from this chapter only, I will request you to go through 1st 2 chapters and solve the exercises. Then only come to this chapter.
So let’s go to a program which we discussed earlier in the previous chapter ( Starting IF ). It will be like, user will give the month-number as input and program will return the number of days as an output.
Input output why?
1 31 1 means January & there are 31 days in that month
2 28/29 2 means February and we can have 28/29 days in Feb
3 31 3 means March, that’s why output is 31days
4 30 4 means April and April has 30 days
So on …
So, if the month number is m , then we need to start writing the code as per below:
if m == 1 :
print ( ‘ 31 days ‘
)
else
:
print (‘ all
other months will come here except January ’ )
Means, all other months except January will fall under the “Else :” block. And now we start extending this program a bit..
if m == 1 :
print ( ‘ 31 days ‘
)
elif
m == 2 :
print ( ‘ 28 or
29 days ‘ )
else
:print ( ‘ all other months except Jan & Feb ‘ )
We are placing another IF-ELSE : unit inside Parent ELSE : block. In short ELSE : IF block is called ELIF in python. It means, at first the program is checking if the month is January or not , if yes, then printing “31 days” , otherwise it is again checking if the month is February or not, if “yes” this time, then printing “28/29 days” and if “no” then going to the child Else : block.
So now inserting some of grand-child IF-ELSE : blocks ..
if m == 1 :
print ( ‘ 31 days ‘ )
elif
m == 2 :
print ( ‘ 28 or 29
days ‘ )
elif
m == 3 :
print ( ‘ 31 days ‘
)
elif
m == 4 :
print ( ‘ 30 days ‘
)
else
:
print ( ‘ Months except Jan,Feb,March,April ‘ )
So this is the way to form Nested IF-ELSE : program. Some times we call it as IF-ELSE : ladder. In the above program , we put the child IF-ELSE : block inside the Parent ELSE : block. We can nesting the same inside the parent IF block as well. Let us write the above program again in a different way, but this time we will be nesting it inside parent IF block.
if m <=12 :
if m >= 1 :
if m>= 2 :
if m >= 3 :
if m >= 4 :
if m >= 5 :
## So on .. till month no. 12
else :
print ( ‘ 30 days ‘ )
else :
print ( ‘ 31 days ‘ )
else :
print ( ‘ 28/29 days ‘ )
else :
print ( ‘ 31 days ‘ )
else :
print ( ‘ Invalid month ‘
)else :
print ( ‘ Invalid month ‘ )
Hope you got the logic.. I am not explaining much on the above program.. My intention was to show that we can nest anywhere. Even if we can write nested IF-ELSE : block inside of both parent IF block and parent ELSE : block.
Let’s take another example of conditional statement of a range value. User will input the percentage of attendance and will get the remarks as per the below table:
Percentage of attendance | Remarks/Comments |
91-100 | “Superb !!” |
75-90 | “Good.. Try to be more frequent” |
50-74 | “Considering for the last time” |
25-49 | “ Guardian Call “ |
<=24 | “ No need to give Exam “ |
att = int ( input ( ‘ Enter percentage of attendance ‘ )
if att > 100 :
print ( ‘ Invalid
attendance ‘ )
elif
att >= 91 :
print ( ‘ Superb !!
‘ )
elif
att >= 75 :
print ( ‘ Good try
to be more frequent ‘ )
elif
att >= 50 :
print ( ‘ Considering
last time ‘ )
elif
att >= 25 :
print ( ‘ Guardian
Call ‘ )
else
:
print ( ‘ No need to
give exam ‘ )
This is how we can build a strong conditional logic using Nested IF-ELSE : block, but let us now analyze what we have written above. We have written the second IF-ELSE : block as below:
“ if att >= 91 : “
You do not need to write this line like “ if att in between 91 and 100 “ , because the syntax would be wrong. Another reason is that there is no need to mention the full range. If the control is coming to the parent ELSE : block of “ if att >= 100 : “, that means automatically the value of variable “ att “ is lesser than 100.
Now if the value of “ att “ is greater than 91 ( as per the second IF statement) , means the value of “att “ is in between 91 and 100. By that reason we have not mentioned any range in the program, but still it is giving the desired output
The last part of this chapter is to show that we can also create this IF-ELSE : ladder simultaneously inside of both IF and ELSE : block. Let us take the same programming mentioned above.
if att > 100 :
print( ‘ Invalid
attendance ‘ )
elif
att >=50 :if att >= 91 :
print(
‘ Superb !! ‘ )
elif att >= 75 :print( ‘ Good try to be more frequent ‘ )
else :
print( ‘ Considering last time ‘ )
else :
if att >= 25 :
print( ‘ Guardian Call ‘ )
else :
print(
‘ No need to give exam ‘ )
Hope you are now able to go and solve the exercise mentioned in the Facebook Group Album (mentioned at the starting of this chapter) for Basic Programming, “chapter 2.2 Nested IF”. But keep in mind do not use && or || or anything else : which we have not learned yet. You can contact me over my email if you have any concern or doubt. You can also comment here.
Please leave your comments here in the comment box. Give your feedbacks with the good parts of this material or suggest where exactly you are still facing difficulties.
Comments
Post a Comment