Python Tutorial for Beginners 9 - Python IF ELSE Statements


hello guys welcome once again till now I have been showing you a basic but very important concepts of Python now from this blog we are going to write some code in our Python file and from this blog we are going to write some useful code and this blog I will start from showing you how conditional statement works and in this blog I am going to specifically take if statement in Python so let's get started I have this test dot python file which i made if you don't know how to make this test of Python file you just need to go to file in the shell and just click new file and then just save your file at your specified location by some name ok and you will be having this type of file so my Python file name is that test dot py now the conditional statement which I am going to take let's see how if statement box so for example I want to find out the absolute value of a number how can I find out by using us if statement so for example I write a variable and which takes an input so I ask the user to enter an input and I convert it to an integer and be saved in the variable called n okay now I want to find out the absolute value of that number so what I can do is I can use if statement and how you can declare if statement just right if and then the condition ok so here comes the condition this is a pseudocode I am writing so we are going to replace this condition by our real condition and then in here so always remember when you write code in Python it's a indentation is really necessary so if you write any condition for example if if you give another code for example I want to print something what it will do is this statement sees the indentation and let me give you an example first and then I will illustrate the basic concept of indentation so I want to find out the absolute number right so in the condition I can write n if n is less than 0 so this is a condition I am checking if a user and thus the value n which is less than 0 what I am going to do is I am going to print print the value absolute value so I'm going to just write the absolute value op and then the I can just use comma to give the value of N and once again I can use comma to you know separate this string and I can write is is and let me make a little bit bigger is and then I can just separate it by one of the comma and I can just write minus NFE if the value is less than 0 right so let's save this program just ctrl s to save the program and I will run the program run module or f5 and it says invalid syntax and why it says invalid syntax here is because in here after F condition you need to give colon here okay so this is taken as a condition and after : h it indicates that this condition ends here and then the statement starts from here once again save it and run it and then it will run in this shell and it's asking the user to enter a number I will enter a negative number minus 3 and press ENTER and it gives me the absolute value of minus 3 which is 3 okay now for example I want a enter a positive number what happens then ok so I will run it once again and this time I enter last them and now nothing happens because this condition we are checking is less than or we are checking for the number less than 0 so this will print this and just remember always that always provide an indentation here ok if you don't provide this small space or tab here and just you know write your port here with no space here and save your program and run it let's see what happens so it says expected an indented block right so always provide an indentation and it it so you know show you the place where the error is coming in red you know red highlight okay so always provide just give a tab before your statement after if condition okay or after any any logic which you write so now this is considered as a statement okay and if you if you write this if you state and all like this it will consider this line as a different code and it will not come as a statement of if condition okay now for example I provide the positive number and I also want to give the absolute value of this positive number so what I can do is I can just give else condition else so your statements apart from you know with no space here but so if your condition is if and then we have the else condition so your condition start from no space but the statement inside your condition start with the indentation or space here just give a tab and it will give you this space okay so once again I want to print out the absolute value of a number now I want to check whether number is positive so if the number is not negative which is this condition if number is less than zero which means number is negative this will be printed else if the number is positive because this condition is not true that means definitely the number is positive I want to print out the absolute value of this positive number right so I will just print it like this absolute value of this should be value and is positive because the absolute value of positive number is positive right so now when I save the program and run it and it says invalid syntax once again why because I also need to give this pole in here right now once again save it and run your program it gives me this question mark with the number I give ten once again and say is absolute value of ten is ten so this is the basic syntax of if statement you're you give if as a keyword for checking the comparison or can comparison and you compare some values so right now we are comparing our value which user enters from zero and if it's less than zero we print this else anything else will be covered by this statement and always remember that your if statement or else statement it will have no space here but your actual you know code which you write in if statement or else statement will be will have to have this kind of indentation here indentation means space or tab okay and it's not necessary that you can only give one line of code inside your if condition you can just give multiple lines of code inside your condition I will just make it little bit prettier so it can have you know multiple line of statement your if condition or your else condition and this is just an example I am giving but this will be some logical code which you have to write here and in a similar way you can just give this here right here right so you can have multiple statement in else condition now one more thing to remember here is whenever you want to go outside so now what you want to do some other operation for example I want to do I want to add two numbers and or some I will declare a variable called sum sum of two for example this is my variable name and I add 50 plus 60 okay and I want to print it print and then I want just print this sum of these two numbers right so if I do it like this for example I'll do it like this what will happen let's see I save it and run my program and I give a negative number minus 9 for example I had give me these values which is which it has interviewed these values okay but I wanted to print a this number also after there are my F else condition so how I can you know show or I how I can tell Python that my else condition is now closed or my this FL condition is now over and I want to print this now so you on next code should start from once again with no space okay so this is this will be considered as a different code and it will not be included in the if condition conditional statements right and now when I save it I will save it and then I give minus 8 for example yeah I need to give minus 8 and now you see it gives me the absolute value which execute this code and then it executes my another code this code which I want to execute after my F condition okay so just if you want to end your as statement in you you know want to write some other code just start from no space so indentation is really very important in Python if you are you know the write or give this indentation incorrectly your Python code will give any error or some error so this is the if-else condition in Python and future I'm going to show you some more interesting stuff in Python so stay tuned       

Post a Comment

أحدث أقدم

Recent in Technology News