#22 Python Tutorial for Beginners | Break Continue Pass in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens find myself in 20 and let's go to do with the series on Python till this find you have talked about loops right we have talked about for loop and while loop in this video we'll talk about three keywords break continue and pass again why do we need that let's see in the code itself so let's design a vending machine of course not a physical machine here let's write a simple code just to mimic vending machine here so what we'll do is if I user says hey I want five candies you have to give 5 candies not a physically candies but then we'll will write only candies so print candies ok so if I say one can dates one time so if I use as they say I want five counties you will print candies five times so that's what you want to do so that's right here so first of all we love variable and we'll say X and the value for this variable will be coming from user input so we'll say int input and in this bracket you would mention how many candies you want okay once you got this statement I what all do is I want to play candies right so if I you say say I want 5 candies will print candy five times if you want to do something multiple times you will simply use a loop so we'll use a while loop here again you can use for loop that's your choice okay but I love for a while loop here so I will take a variable which is increment variable I will say I is equal to 1 and then we'll use a while loop here and we'll say if I less than equal to X because depend upon the number of value our users entering will print will go for so many candies and every time this loop runs it will print candy that's what we want to do here right and then after every statement we need to also increment it in fact we can so if you can write I equal to I plus 1 or you can use shortcut here which is I plus equal to 1 because we have seen that right so instead of saying I equal to I plus 1 we can also use I plus equal to 1 shortcut for the same thing now this should work right so this should work let's try so after this code you can see it is asking for the number of candies I would say four candies enter you call it candy four times tecna's you're getting candies from the machine and I will just run this code once again now at this time I will go for let's say eight and you can see we caught candy eight times which is working now here is a twist the twist is what game are you says hey I want 100 candies okay and then in your machine in the vending machine we don't have hundred candies you have let's say fifty candies or maybe 80 candles so what you will do now you have two choice you can stop the transaction you can say hey user we don't have so many candies all what you can do is you can give 80 candies and you can say here would be a we are out of stock right can you do it here so I want to do that here so the available candies I would say house is a variable called as a V which is available candies and let's say in my machine I have only ten candies okay if I user says 5 candies no issue you can take 5 candies example if I on this code now and if I user say say I want 10 candies that's completely fine we have 10 candies but if I you say hey I want of 20 candies we don't have that much right so what you will do here so in that case before printing candies what we can do is so here we can check if the number of if the value of I because see if you have 10 candies and if I you says we want 20 candies what you can do is you can give 10 candies right so the value of I is important here so if the value of I is greater than your number of available candies which is a B in that case you will stop the execution or we forgot one code in there so we need to stop the execution but which execution not the entire code execution because maybe at the end of your code outside your while loop I want to print by so I don't want to stop the execution because you can do that by saying exit or something I don't want to end the execution of the code I just want to come out of while loop and the way you can do that is by using a statement called as break the moment you say break it means hey just could just jump out of the loop right so let me just run this code and you can see it is saying how many candies you want I would say I want five candies no issue you've got five candies and you got by as well it should be get lost right okay so if I say 11 candies now and you can see we caught how many candies here so we got one in fact let me should reduce the number because it is difficult to count we don't have much time so I would say I want six candies and see how many candies you caught you got one two three four five so total number of candies available above is five but then a user says I want six candies what you will get is all these six candies right so this thing is important so this is how you can stop you can just jump out of the loop so there are so many operation of this but extract and you can use it in your in your applications but then this is how it works so if you want to just jump out of the block you can simply use break in fact if you wanted to print a message by saying hey we are tough now we can do that here so you can say print out above stock and now I feel on this code let's say I want I want eight candies you can see after printing five candidates saying out of stock you know this is what happens when you buy something buy a new phone online you know if we have this open sale so some people can buy it and then it says out of stock anyway so this is how your break statement works the next statement we have is continue so the question is so in the last video I have given you the assignment which is you need to print numbers from one to hundred or one to twenty but then don't print the values which are divisible by three or five Oh case you have to skip those values so basically what I want is I want to print one two or three I want to print one two I don't want to print three then I because it's divisible by three I want to print 4 I don't want to print five because it use either by five I don't want to print sex because it is divisible by three I want to print seven eight then I will not bring nine because it is divisible by three okay so I want to skip those numbers how can we do that here of course we will shorten the code will not be focusing on five Kanzi focus on three okay so I want to print all the values from one 200 but then I don't want to I want to skip those values which are divisible by three and to do that what I will do is have that a cool and this time will go forward okay we can go for wild low before though your choice I will say for tie in and then with the use of the inch here and I will three I will go to 1 zero 1 because that's what I want to go for I don't want to print 0 so just let me start with one ok so that's my for loop and then I just want to print the value of I now what will happen it will print all the values it will print the values from one 200 let's check it out so let's say 1 and you can see we got all the values but I don't want to print all the values right I want to I don't want to print the value which is divisible by 3 in that case what it will do is I will say if if my I value which is I mod 3 is equal to equal to zero ok let's not use on package there if this is equal to 0 in that case I do I just want to skip the remaining statements okay when I say the meaning statement I don't to skip the statement outside the loop example if I say at the end and it's a good idea to print by at the end because at least you will know what is happening so what will happen now is when you say for I in range 1 200 every time you say if it is 3 in that case I don't want to print I I just want it to continue with the loop let me just run this code for you oh we got an error what's wrong oh we are missing one thing right now how would you how will you continue so you have to say a statement which is continue so continue we'll skip the remaining stuff so you find on this code and you can see we got all the values and look at the output now we got one two we are there's no three because it is divisible by three we don't have sex again we don't have nine we don't have 12 so this is how you work with continuous so what continually do it will skip this for the execution if you say continue it will not jump out of the loop it will only skip the remaining statements in fact in the assignment what we have done is so in the assignment question it was 3 or 5 right so we can do we can say odd I mean if you have to use or symbol here so we have to say odd I mod 5 is equal to 0 right so now it will skip all the values which are divisible by 3 or 5 so he can see we don't have 3 we don't have 5 we don't have 9 we don't have 10 right so this is how it works in fact we can also do it for and so or just keep those values which are divisible by 3 and 5 example 15 right so we have 20 so all these values so I find on this code once again and you can see it will skip only those values which are divisible by both in this case it is 15 you can see we don't have 15 so that's how you can practice and also we have continued here it works right so the next one we have is pass now when you will use pass so just imagine we want to print one 200 ok so I want to print all the values but then I don't want to print those values which are let's say odd numbers okay I can be a 25 a simple example I don't want to print values which are odd numbers so my condition here is don't print odd numbers see I'm not thinking about even I'm thinking on the odd numbers so my logic here is on order how do I check odd so we'll say I won't print the value of I but it should not be an odd number so in that case my condition is only on order so I want to do so I want to print in else addition so I will say in else you have to print this so if I mod 2 is not equal to 0 then don't do it but only if it is in in the else condition I want to print it but that's what I am trying to do here now I know you'll be saying why don't what if you simply use even if you check for even and print it but imagine if you have a scenario where you are checking for the old values they're not focusing on you when you're focusing on odd in that case I want to print it in else part but then you can see it will not work you can see it is giving you an error you cannot simply put an F as an empty block right so in that case you will say hey I don't have anything here and I'm typing pass so pass simply means a there is no code ignore it right so just pass the if block in fact in other languages like C C++ we have got a package so we can't fully freed up but in unfortunately we don't use current package here so we have to use pass so in this code you can say we caught all the values all even numbers so if we go to all the values which are not out okay so just the difference in the midpoint so that's how we use break continued and passed so in the next video we'll talk about some more patterns since we have talked about this thing now this so the next video will be very exciting where we talk about different patterns so I hope you enjoyed this series let me know in the comment section and if you have any questions let me know that as well so thanks everyone bye you
Info
Channel: Telusko
Views: 596,278
Rating: 4.9196968 out of 5
Keywords: telusko, navin, reddy, tutorial, python, for loop break, loop, break, continue, pass, jump, block
Id: yCZBnjF4_tU
Channel Id: undefined
Length: 10min 19sec (619 seconds)
Published: Sat Jul 14 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.