hey guys welcome to the next blog on Python tutorial for beginners in this blog we will learn how to use if-else statements in Python so first of all what is a if statement so a if statement is used to execute a statement or a block of code if and only if a condition is fulfilled so first of all let's see the syntax of if statement so let's say I declare a variable called X whose value is 100 and then I'm going to check whether the value in X is equal to 100 or not so I can use this keyword if and then if you have seen the last blog in which I have shown you how to use comparison operators and logical operators then you will know that for equality we use a double equal symbol and then I'm going to just check it against hundred now after the condition you need to give this colon in order to tell Python that this condition is finished and you need to press Enter and you will see that there is an indent given to our code and you can see this cursor is here which is four spaces away from the start or one tab away from the start and here I can just for example print the value of x so i can just say x is equal to and then after the comma x and let me save this code and run this code and you can see on the run window here x is equal to 100 is printed because this condition which we are checking using the if statement is true which means whatever code or statement or block of code you will write under if it will be printed let's say this condition is not fulfilled so I'm going to just say that X is not 200 which is false condition and when I run the code now it doesn't print anything because this condition is not fulfilled and that's why this statement is not executed now let's talk about the indentation little bit so indentation in Python is the way of marking a block of code so you can see I have given 4 spaces indentation here so 1 2 3 4 or if you are using PyCharm when you press ENTER after an if condition you will see that automatically this cursor is pointing here which is 4 spaces away from the starting point so this means that whatever you write after these 4 spaces or a tab is the part of this if statement now if you are familiar with C or C++ this indent acts like a curly bracket in C or C++ so in Python you don't use the curly bracket to define a block of code you use indentation to define a block of code so I can write this statement like this also so I'm going to just say X is equal to and in the next line I can just give one more print function and I will print X from the next line and you can see here also I have given indentation after this starting point and this means that these two lines are the part of this if statement so let me change the value of x is equal to 99 and when I run the code it will print X is equal to and in the next line it prints 99 using this line of code so you may ask what happens when I will write this print function here and let's say we want to print finish here okay and let me run the code and let's see what happens so it prints finish now let me just change this condition and I'm going to just say hundred here and let's see what happens now so now only Finnish is printed because this line of code is not the part of if condition only statement which you write after four spaces will be the part of the if condition like this line and this line of code but if you don't give any indentation here that means that this line of code is not the part of the if statement now let's take another scenario and this time I want to check whether the value of x is a positive value or a negative value so I will start from here and I will give one more if statement here and I will just check whether X is greater than zero or not and then give this schoolin to indicate python that i have finished this condition and then if it's greater than zero then I'm going to print that X is positive now if X is not positive and if it has a negative value I need to somehow print that X is a negative value and for this you use our else statement so here you can write else and once again you need to give the colon here and then under this else statement once again you provide four spaces or a tab indent and you can once again write print and this time we can write X is negative this means whenever this condition is false and if and only if this condition is false then only the else part will be executed so whatever code or block of code you write under else that will be executed if and only if the condition here is false so seeing the value of x which is 100 which is obviously greater than zero it is going to print that X is a positive value now when I change the value of x is equal to minus hundred here and once I I execute the code first of all it prints that X is minus 100 here because first of all this condition is checked and obviously minus 100 is not equal to 100 so this condition is true so these two lines of code are executed because this condition is true and here because this condition is false that's why this statement is not printed and the execution of code goes to the else condition and then this line of code will be executed which is X is negative and you can see here and this line of code is independent of both these if statements and that's why it will be printed always now as we have seen from our last blog that we can provide multiple conditions using a logical operators so we can here also write that if X is not equal to hundred or if the value of x is less than 0 let's write here X is less than 0 then only we will want to print this statement so once again when we run the code in this case both the conditions are true that's why this is printed if we write here that X should be greater than 0 then also this will be executed because one of the condition is true and because in the case of or we only need to satisfy one condition out of multiple conditions in the case of end when I write end here and run the code now you can see this is not executed because one condition is true here and other is false and in the case of end we need to satisfy both the conditions and then only these two statements will be executed or this block of code will be executed so this is how you can use if-else statements in Python I hope you have enjoyed this blog I will see you in the next blog
Post a Comment