#19 Python Tutorial for Beginners | If Elif Else Statement in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is Ivan 20 and let's continue this series on Python and in this video we'll talk about CPU Oh hold on this is a Python cities right why we need to know about CPU in fact everyone knows about CPU right CPU has three parts CU mu and ALU we have to work with mu to this point because mu basically means memory unit so we were using variables we were trying to save data so it was there but the then we have a unit which is ALU which is in which we have two parts one is au which is arithmetic unit and the second one is logical unit now when it comes to automatic unit it performs calculations all right so whatever question we have done till now addition subtraction division multiplication and what any conversion of course now it's time to talk about logical unit now when you say logical unit this is something which makes your computer think of course your computer will not think automatically as a programmer you do it right you let your computer think so you have to add a code right so in real world when you say you are taking a dishes what it means so you apply different conditions right so example you know in in real life we go with the flow so example will do this will do this will do this and then at certain point we decide ok so I thought we have to go this way or that way example even after completing your tenth standard where to decide which field you want to go for after completing your job after committing your graduation you will decide which company to join right so we always take decision based on certain conditions can we do that in programming so in programming as well we write statements right so we write one statement second statement or statement and then we realize okay we have two flows now I think we have to go this way or we have to go that way and that's where you have to apply conditions how do you implement that here for that we have a special keyword called as if right so we will we will be using Ethier so using if you can actually specify the flow of execution either you have to go in this way or that way I would also recommend you to learn about flowcharts of course as a programmer it's a good to know concept if you know how flow chart works it will be helpful for your programming career so let's understand how this how this F works so if you can say we have three right so after this three statement we have to flow now either we want to execute this set of statements aren't execute there another set of statements how you decide what what statement you want to execute here based on some conditions if this is true I will execute the first block if this is false and execute the second block and to implement that we use if now if you talk about the syntax this is how it looks like so we have to use if there's a keep what we have to use and then we will have you have to specify either true or false now if you say this is true they will execute this statement so if it is true only then it will execute this statement right so let's say the statement is I'm right that's it I want to print I am right so if you just true it will print I am right that's it let's run this and you can see record the answer which is Android if this is false then it will not execute the particular block so this is false let measure on this code and you can see it is not executing the block so that's how your if works if it is true then only it will execute the block but hold on what is that block and what is this : and what is this statement so thing is whenever we use if if is basically a block and input in Python we call them as a suit where you can write multiple statements but then how you munch mentioned that this statement belongs to Eve because see if if I like one more statement here we just without that if I say print and if I say bye now what do you think we are saying false right it is false so will it execute by or not if this print statement belongs to eight then it will not execute by because it is false if this statement does not belongs to if they can execute by right let's try this if I love this code you can see record by but why by which is saying false right it's because this statement does not belong to a why it's because we have to follow certain indentation how do you specify if the statement because they if we can have multiple statements right so if you under specified all these statements belongs to if we do use some certain indentation and what is indentation some specific spaces it can be 1 space 2 space 3 space 10 space it can be hundred spaces you have to use certain number of spaces so if I use for space here for this one as well I need to use for space even if I use free space you can see it will give you error okay so it says I'm the unintended does not match any other international level so you have to make sure that it belongs to you are the use either one space so you have to throughout you have to use one space if you use two space for to use two space okay but normally we always use four spaces okay example if I say even if I say three spaces for both they do work okay there is no matter so you might get a warning by saying you know you should be using for four spaces so it's better to always use four spaces in fact the tab or one tab is for space right by default so I would also prefer to use tab itself using spaces because you might make a mistake with spaces so use one tab and that's fine and if you're using an IDE just to enter by default it will it will use the intention okay I don't want to keep by inside if so I will write it outside somewhere here right so this bar doesn't belongs to a block but question arise we will not writing true and false in a code itself right this true and false will evaluate from an expression right of course it is depend upon user input is depend upon the data in the database it depend upon the data on the server it's all depend upon the value you are passing and the condition so how will you do it here maybe I will try this term this is an example let's say I want to check if the given number is even or odd maybe the input is coming from the user or I will take my oriented put an you can try it with the user input I will take the number is three okay if the number so when you say even number odd number what it means if the number gets divided by two if the remainder is zero now it's a even number right if the remainder is not zero which is one in this case it will be odd number how would I verify that so let me do is I will use a variable R and I will save the window which is either 0 or 1 so it's a X mod remember defeat the symbol which we have used more to write so in R it is either 0 or it is 1 right session of using false I want to check if my R is equal to 0 because if my R is equal to equal to 0 then it will print even right so if my R is equal to 0 that means I say you were number okay that works let's try I will run this code oh okay so that we got by you know why we got by is because X is three and it's not number let's try with eight eight is an even number right let's try this with record the answer which is even and by so yes we got even number because that's an even number and we got by because it should be printed anyway this is how you work with E so if this is true okay this is important this is true then it will print even if this is false it will not execute the block it will not agree to statement inside that if how about seven of course that is odd-numbered right I want to print even it is even I want to print all as well we were not doing that at this point okay how do I print odd because if I print odd outside the if block I will say odd the thing is it will print odd anyway doesn't matter if your value is even on order example if either on this code you can see we called odd in this case and in fact if the value is 8 then only then also it will print all I mean it is spending even then all as well we don't want all four equal numbers right so I want to execute this one only when the number is a border okay so that means I want to print this only when so we will say if if art is equal to equal to one okay so we are doing that right so we are checking if R is zero then print even if honest one drink order that works let's try oh we got even because it's an even number and I will try with 9 let's say 1 or we got odd so it is working so 8 is even it was working 9 is odd it's working but don't you think if the number is even okay everything is working as of now but there's a twist you see as a programmer we also want to improve the performance right it's not just about writing code it's not just about getting the output it's about writing the efficient code and this is not efficient you know why because when you say your number is 8 and 8 is even we know that so it will check if R is 0 yes it will print even that's right but then if the number is even why you have to check this as well because it will go in the flow right example if I if I D bug this application let me just add a breakpoint here you remember we have done that before I will say debug and let's try f8 so you can see if I say fwrite it is which is saying yes R is 0 it will print it will print a 1 and then I don't want to check for odd right because it is even so but still it will check art for one as well I don't want it to check for R is equal to 1 because it doesn't make any sense it's an even number so that means what I'm what I'm expecting here is if it is checking for this number if it is checking for even if it is true then it should not be checking this one in that case what you will do is you instead of saying R is equal to 1 you can say else you know what else needs if it's true then executes the block otherwise go for the else block if this thing is getting executed it will not be when checking the else condition let me just prove my point I will put up neck point here I will say debug right like debug and f8 or the flow you can see R is 0 yes it's going to even and now it has skipped the ice block so this is how you improve the performance oh you show-off using 2 if we can use if and else it works right so these are you walk with offense now can i only use else of course not else comes with if safety remember that so you can write if or else in show waiting to ifs or there are the conditions which are different from each other it's not dependent so of course you have to use 2 if there can I have if inside an E is it possible let's try after printing even I also want to check if the number is greater than 5 right we can do that so we can check if X is greater than 5 then I want to bring something else I want to print right oh that's it I want to print I want to print gate that's it I just want to play through it it doesn't make any sense I know printing great but then the idea is we can have if incident Eve so this if will be checked only when this is true right so when you say if this is getting if this is true it will execute even and then we have like we are reading another if and see this see the indentation so this print great belongs to the second if not the first Eve right so if this is true then it will print even though it will check for another condition and then it will print great right so this is called as nested if which is if inside the F now how we didn't how do you know that it this is inside the if because of indentation right we can have an entire tree of ifs you know if inside the if and if if inside if that works for this one also we can write else now this else is a pairing with this if not the above if okay and if I say prank I would say no it's not so great right so what do you think what is the output now of course great right so it means to run this code and you can say oh we got the error can you guess the error that's right you can see that error here the error symbol so we should be using a colon my mistake there so anyway you use if-else we have to provide a colon let's run this code and it works you can see we got even great and by if the value is not greater than 5 if it is 3 let's run this code and you can see record odd and by because this is an odd number it is not even going there and then interest printing all so that works so this is how you work with if and else let me add one more twist year the twist is it's like not the but not with this code let me just try one more code here we also have a concept of if/elif/else now what that means let's say let's take an example here if i user enters one i want to print oh and a which is one in what format if i user says to I want to print to if I use this three I want to print three if the user says for I want to play in for let's do it here I will take a random number I will take to look at doesn't matter maybe you can take this input from the user you can try that I will check if oh can I can I use round brackets and there's this yes if you want to put the condition you can put that condition on tickets as well now what happens you know if you are coming from other programming language like C C++ Java or C sharp you have this tendency of using wrong package there so in Python also you can use it it's optional you want to use it or not that is your choice what I'm talking about is this one so if I can say if X is equal to one so instead of putting that open you can also put that in a round bracket but you know open looks cool let's use that so we got X is equal to go to 1 in this case I will print 1 so that's one right and then we can check it for other other numbers as well we can say if s is equal to X is equal to 2 in this case will print so when you when your X is equal to then we can bring to and we can do that for other other statements as well so let's try that for the value so I would say so this is how we can do with for the numbers why differ on this code you can see it pretty spending too and if I change the value to let's say phone it will print it will print fold so it is working right everything is working but again the same problem right what if the value is 1 and then we know on this code don't you think it will print one of course weight was checking it will check for them every other number in that case if you don't want you to check with every other number we can use instead of using if we can also use LF now what is Elif means is if this is not true then only it will go for the next one the same thing you can apply everywhere you can say Elif and L if so L if stands for else if so if this is false then only it will check other conditions if I am this code you can see we got one and if I put a breakpoint here and if I don't this code in factor we can simply debug it and let's say f8 and you can see if this is true it will directly jump outside ok so it is not going for further instructions but if I say this is three or maybe let's go for two in this case so if I debug it let's trace it so you can see it is going for the first statement since it is false it will jump to L F right because the first one is false and now this is true because the values to it will execute to okay and it will come out yeah so this this is awesome you know okay what if the value is 5 now this is not matching with anyone right there is not which is not one is not to its nor 3 it's not four so they see the jumping now so if I debug see the jumping it's jumping to Elif which is false jumping to next Aleph which is also false jumping tell if it's also false and then it's come out so in that case we should be printing wrong input but how will I print that we can do that in LS if none of this matching we can print wrong input that's awesome right so this is how you can work with if Elif and else I mean you can try out different combinations you will find the assignment in India at the end of the video so try it out and let me know your thoughts on if a leaf and else so that's the thanks for watching everyone I hope you are enjoying my series let me know in the comment section and do clear like button if you're enjoying it thank you so much for watching everyone you
Info
Channel: Telusko
Views: 836,224
Rating: 4.9224944 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, if, else, elif, conditional, statement, true, false, syntax, debug, run, code, nested
Id: PqFKRqpHrjw
Channel Id: undefined
Length: 15min 38sec (938 seconds)
Published: Wed Jul 11 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.