hey guys welcome to the next blog in python tutorial for beginners in the last blog I've shown you how to use if statements and L statements in Python now in this blog I'm going to show you how to use L if statement with if-else statements and how to use nested if statements in Python so let's get started so what I'm going to do is I'm going to declare a variable and I'm going to ask the user to enter some name so I'm going to just write and the name here and when the user enters this name I want to compare this name with some predefined names so in the last blog we have already seen how to use if statement so I can just write name is equal to and I can compare it with some names so let's compare it with a name called max here and I will give the Kulin here and once the name matches this name I'm going to print the name so I'm going to just write name and third is and whatever the name provided by the user so I will just use this name variable here this we have already seen in the last blog now in Python you can also use a L if statement and the signature of L is if statement is you can just write L if here and then you can provide some other condition which returns boolean so I can once again here right if the name matches some other name for example Leo and once again I'm going to just print the name entered by the user and this L if condition I can use any number of time after the if let me match this name to some other different names for example Roy and the last name will be a li so if the name matches max this statement will be executed if the name matches Leo this statement will be exist if the name matches Roy this statement will be executed and if the name matches le this statement will be executed now if the name entered by the user does not matches any of these names then we can use else statements so we can just write else here and we can just print a message here so the name entered is invalid so this message will be printed when the name entered by the user does not match any of the name I am checking here so let's run the code and let's see what happens so now my code is running and I'm going to give the name of max here for example and then press Enter and it prints this message which says the name entered is max using this statement now once again when I run the program and this name matches let's say some other name le and then when I press ENTER it prints the name entered is Ali which means this statement is executed and let me run the program once again and when I give some random string here it prints the name entered is invalid now let's say the name entered by the user is max and this condition evaluates to true then this statement will be executed and the other conditions which we have given using Elif statement will not be evaluated so let's say we will write max here also so this condition and this condition is same rights checking for the same name and when I run the program and when I write max here only one statement is printed which is this one so we can see when this condition returns true only one statement is executed and none of the other conditions are even evaluated now the important thing to note here is you cannot start with L if statement you need to start with the if statement so you need to write a if statement and check the condition first and then only you will be able to write L if condition now it's possible to use multiple if condition and then this l if will become the part of this if condition and this will be the separate if condition so when I run the code once again and when I write Maxia is going to print two statements one is using this and other is using this because this if condition and this if condition are two different if conditions and once you use L if so I'm going to use LF here then this LF is the part of this if condition so this is how you can use if statement with L if statement and as statement now let's see how to use the nested if statement so I'm going to declare a variable called X and the value I will assign to X is 10 and then I'm going to write an if condition and here I'm going to just evaluate whether X is less than 0 and when X is less than 0 I am going to just print that X is negative else if the value is not less than 0 so I'm going to just write else then I'm going to just print X is positive now let's say I also want to evaluate if the x value is even number or an odd number if the x value is positive so under the else I can once again write if and then we can just evaluate this condition that if X modulo 2 is equal to 0 which means that when X modulo 2 returns us 0 that means it's our even number so we can print X is even otherwise in the condition I can just write else X is odd so in this if condition I'm checking if the value of x is less than zero or not and if this condition is not satisfied then I am checking whether X is a even number or X is an odd number so if you remember indentation here is very important so this if condition falls under the else condition right so this if condition and this else is the part of the else statement so because we have provided this force space indent here whatever code you write with this indent is a block of code and what we are essentially doing here is we are nesting one if-else condition inside the other if-else condition you can also reverse this condition checking and you can also say if X is greater than or equal to 0 then the value of X is positive otherwise the value of x is negative and this statement you can write under the if condition also and this is also a nested if-else statement and let me run this program and you can see x is a positive value because x is equal to 10 and x is a even values so once this condition is true this statement will be printed and whatever if condition is there inside the parent if statement that will also be executed and this condition is checked first of all and obviously this condition is true in this case when X is equal to 10 because when you do X modulo 2 its going to give us the remainder 0 and that's why X is even value and that's why it's printing X is even here so it's totally possible to use one FL statement inside the other FL statement and this type of FL statement are called nested if-else statements so that's it for this blog I hope you have enjoyed this blog and I will see you in the next blog
Post a Comment