Python Tutorial for Beginners 10 - Python elif and nested if Statements


hello guys welcome once again in the last blog I have shown you how to use if-else statements in Python so I have given you a little brief introduction how to FL statement but there is more to FL statement which is LF statement and nested if-else statement so let's see how we can use those two so for example I am going to just to take a new example so I am going to delete all these stuff and for example I want to compare some names this time I want to have a variable called name and I want to take input from my user that he will give a me a name and then I will compare the name and I am going to show the result okay so what I can do is I want to compare the name right so in my if condition I will compare my name if my name and to compare equality you use double equal not this but double equal sign here okay so always remember if you want to compare the Equality of two string or two values to just use not single equal to but you use double equal to if you use single equal to this means assignment so a sign so this single equal to means that assign the value whatever user enters to the name but if you double equal 2 it means that you want to compare the two values okay so I want to compare this value to for example name called mark and I can say print the name entered is the name entered is and then comma name for example okay now I want to check some more names if user enter some other names right so for example if user enters some other name I want to check it so I can check it just I can check it by L if condition okay so there is a condition called else if this L if means else if okay and then I can check one more condition for example this name is this time John and then I will print some other value so I will just print the name of John here so the name entered is John in a same similar way I can have multiple health conditions like this and just remember to give the indentation and then for example some other name I can compare or some other name I can compare okay so if you want to compare different conditions you can use LF condition and else if the condition is false what I can do I can just print that the name entered is not valid for example not valid okay and now I will save the program and when I run this it asked me to enter a name or so for example I enter a name mark it says the name entered is mark okay once again when I run the program once again and this time I enter some other name for example Tom and press Enter it says name entered is Tom because it goes here so what's the flow of the program so we are checking or we are asking the user to enter any names when the user enters any name this name it goes in this name variable and then the program comes to this place to compare this name to the name called mark and the a if the name is mark it will print this okay otherwise it will go on to check that if the name is John and if the name is John it goes here and it print this otherwise it will check if the name is Julie and it can print this okay so not in this way your if-else statement works or else if or if else else if else if else if else so LF means else if and you can compare multiple condition using LF right now for example I want to you know have multiple if condition inside my if condition okay so it is called nested-if condition okay and I tell the user to enter any name and if this name is let's say name we assign this value this name to for example we have this name is equal to animal animal and then this I declare another variable animal name for example and in the name and this animal name is dog ok so I have a name which is animal and then I have animal name is equal to dog so first of all I want to compare if the name is animal okay so if the name is animal then I will I want to do some some other I want to check some other condition in this animal condition so I will just I can just give mark one more condition and check if this animal name is equal to so W is equal to dog okay dog and don't forget this : and then I will print here so if your if statement starts from here in the last blog I've shown you that you need to give the indentation here okay if you write so let me show you print you can just print valid anymore valid anymore okay so why this indentation here is more important is because for example I can just write printer like this without I only give the same dentition so I can say a name and third is and mo okay so see the indentation here here indentation so this statement belongs to this if condition and this print statement belongs to this condition because this comes in the line of this if statement right so whatever you write here in line of this it will be the condition of or valid condition of this and it will print this one and if you know the animal name is dog it will go here and we'll print valid anymore so what do you think what happens let's see what happens I will just run my program save it and say is valid animal name entered is anymore right so and just remember that whenever you want to give the statement or code for this if condition just make sure that this is in line with this if and whenever you want to give the code for this if condition you just give your statement in this or along this line okay so indentation just remember indentation and this is called the nested if-else condition so what we have done we have checked if this name is equal to animal and if name is equal to animal then we say that we compare one more condition that if animal name is dog and then we print this or in the case of name any this animal name is not dog we can just say else and I can just print one more condition print animal name I can just to print animal name in valid okay so this is my F else condition inside my another F condition so this is like a parent if and this is like a child FX condition okay so I just change this name some other name cat for example and I save it and I run it and invalid expression because here I forgot the colon save run it once again and now it will print animal name is invalid because it goes here and seized okay name is equal to animal so I go to this condition but animal name is not dog here it's cat so it's it goes in this condition and it says animal name is invalid and then it just print the name entered is anymore okay so in this way you can use else--if and nested else if statement in Python I hope you enjoyed this blog 

Post a Comment

Previous Post Next Post

Recent in Technology News