Hi everyone, this is Graven for this third episode about python language learning in the second part we had seen how to create variables that allowed us to temporarily store values that could subsequently change, vary when running the program today I propose to attack a brand new concept in which we will deal with conditions in programming as in everyday life we are regularly made to make choices that will be the result of conditions I give you a very simple example to illustrate me If the weather is nice then I go out otherwise I stay at home.
This, this then and this we will find them in our code and this no matter which programming language you choose now that we saw the theory let's get to practice Before I get to the heart of the matter, I'd like to quickly go back to the structure established for the first two episodes we had created a Main function that was called when the program lit up some of you found it was very complex to start with that first and then some people a little bit more experienced in python less trained than it was not necessarily
useful to do that Why? Because in our case we are simply going execute a series of instructions so no need to do a main function we'll use that later when we create modules so all you can do is start from now you can write your code directly at that level for example we can put here print hello world and if I run the program we always have a positive result and it makes it even easier for you to do that let's start right away development the first things to do that but to create for example
two variables the first it will be a person's wallet so we will for example call wallet and we will assign it as a value for example 5000 euros we can go to the line and we will create a second which will be the price of a product for example a computer so I will call it computer underscore price if you have followed the last video you will know why we have an underscore at this level and we can here for example assign it as a value 900 euros well now that we have all that we
will be able to do our first verification so I skipped two lines and for example I'll check that the price of the computer is less than 1000 euros for example so how to do that well that's what we'll see immediately the price computer corresponds to the variable computer price we created just there then I wish to check the price is less than 1000 euros we must use as in mathematics signs strictly superior to or strictly inferior or even two equal to check a perfect equality or a whole lot of other signs so we'll start by
checking if it's sadly less than 1000 euros so what I just wrote there is what is called a boolean it will return to me either true "true" or false "false" and at this point I'm doing a print of this you will see that this is true and that's all the very meaning of condition we can do that if this condition is true we carry out instructions otherwise we will do something else it is not a problem so for that we will recover the verification that we want to do and we will be able to start
our condition first we need the keyword if that will start the condition to do the famous if something is true for example so I'll put here if little space at that level we will paste the verification that we want so in our case it's always the same and to start the block conditional you're going to put here two points and if now i'm going to line I could here also perform a series of instructions if this condition is true then how do i put here for example a print being the price of the computer is
good less than 1000 euros so if I run the program we can see that I have this message that is displayed now what will happen if I change the price of the computer by putting 1200 euros it's ok with me here the condition will no longer be good therefore one would have to create just below one else which simply means otherwise you open as usual the conditional block and we can do something else for example a print by putting No, the price is higher at 1000 euros now I run the program we have here this
result so what we must remember is that there we check if the price of the computer is less than 1000 if it is the case we carry out instructions otherwise we will do something else we can put another message what is interesting in the verification just now is this famous sign that we had there I could do that it is strictly inferior or superior for example or so check a perfect equality but we can do other things for example we can check if the price of the computer is different by putting! = 1000 euros I
could display as a message the price is not 2000 euros and we can put yes the price is 1000 euros I run the program and we have the price is not 1000 euros and if now I put as 1000 value the program will try to make this condition it will see that it is not true therefore it will take refuge in the otherwise execute this perfect statement so in our case I used the variable computer price but also I can use the variable wallet in my condition if the price the computer is 6000 euros you
agree with me that we can not afford it with only 5000 euros in our wallet therefore we could very well do this kind of program here it will just check that the price of the computer is well below or equal since if the person pile 1000 euros they must still buy it will have zero at the end but it can buy it anyway and here we put wallet, so at this level we can put the purchase is possible so there for example I can very well recover the value of wallet and remove the price of
the computer and reassign that to the variable Wallet for those who wish to do this even more quickly you have another writing you can outright to do less equal therefore he will know that it takes the value of wallet subtract the price of the computer now if this condition is false we will take refuge in the else and put here the purchase is impossible you only have here I will inject the value of the wallet of the person so we could very well do it by adding here + wallet and converting that to a string
for it to pass or it is also possible to put here in the text downright open and then closed braces we will be able to replace the value that we want so I will add here a small sign the € at the end we'll get quotation marks and add here the format function that will replace this value for example the wallet of the person and what is interesting is that we do not even need to choose the type it will automatically do it for us and finally finally I propose to post here the new wallet
of the person after the purchase I launch the program and we have here the purchase that is possible since we had a wallet of 5000 euros and the product cost 1000 euros which is interesting and we can note that it is possible to make several checks for the same condition there we checked if the wallet can afford to buy a computer at this price but we could check in more of that something else for that I will use the small keyword AND which will make it possible to check on top of that if the price
of the computer is more than 1000 euros so here what will happen is that he will do this first check first then he will move on to the second and if both are true he will be able to execute the different instructions at that level it is possible to simplify the chain by doing alt + enter and look at that level we have an even simpler code you had other keywords for example to make an OR by putting the place of the AND, OR so there he will check that or that if one of the
two is true therefore he will be able to perform instructions you have another way of writing conditions even simpler than what I have presented to you so-called ternary conditions imagine that I want to check that the price of the computer is always less than or equal to 1000 euros this condition we will be able enclose it in square brackets put in parentheses that we want to do it was then true for example we can put the purchase is possible then we will put here a comma and put something else if the condition is wrong for
example the purchase is impossible now I can put that in a variable will be called text and it will be equal to all that so if I print here the text what will happen is that we will make this condition if it is true it will fetch the first element and assign it to this text variable otherwise it will give the second value is what we call the ternary conditions here we have a two and three elements the result of if the result of otherwise is here the condition I run the program and there the
purchase is possible well now that we control a little better this universe that we will be able to start with a second example so i'm going to delete here i code it will be a verification system of password I jump a line and at first we will create a variable that will be called password and who will allow thanks to the input function to ask the user to enter the console his password so I will put here "Enter your password" and we will create a second variable that will be called password length, which corresponds to
the size of the password in English so it will be equal to what to the len function which will allow to count the number of characters of this variable password so if I display password_length and that I launch the program I will enter as password for example azerty123 and there I would have 9 characters so now that we know that this variable works we go here check and password is less than 8 characters for this as we will begin our conditions with the keyword if and the check to be done is that password_length is less
than or equal to 8 characters I want to put two points to go back to the conditional block and we will put password too short otherwise, I will put the little ELSE we will put password valid I run the program again enter your password we will put again azerty123 there it puts me valid passwords since we have nine character so it is much greater than 8 and if now I put a very simple password "ABC" we have here passwords too short so it works perfectly but there you will tell me "Ok Graven it's fine all
that but how can we make a condition intermediary to check that if the first condition is not good instead of going directly into the other one we make another "so we will use the" else if " if not so if my condition is not good then here we will elif we're going to put here what we want to check so we know that password_length is greater than 8 and in addition to that that password_length is less than 12 they offer us at this level of simplify this chain so I'm going to do here alt enter
and then we'll check that the password size will be between 8 and 12 characters attention here it is strictly greater than 12 if you want to include it will have to put an equal here to take it into account so here is I skip a line I could do an instruction by putting password means then there, if the conditions here is not good we will go to the second if it is not even good well in these cases it will still go in the otherwise if I put passwords perfect I run the program we will
put azerty123 so there password means as it lies between this interval and if now I put a relatively long password here we have perfect password because we have 26 character and that's it you now have a global idea of how conditions are created as at each end of video I gave you a little practical work to do for next time so it's going to be a system of movie tickets to go first to harvest the age of the person who buys the tickets so you ask him in console what is your age if the person
is a minor then she will have to pay € 7, if the person is of age so she will pay 12 euros so this is the simplest version now I asked you in addition to ask him do you want popcorn so here only if she answers yes then you will add 5 euros more in the total price to pay and at the end bah naturally you will display this total price to pay you will of course in descriptions the correct tp and a small quiz to train you on this new concept so here is a
little bit what I wanted to show you today in any case I hope you enjoyed this video next time we'll see how to create lists do not hesitate to subscribe to not miss the next episodes of this series in any case it was Graven ciao to all and to the next Hey Hey Subtitling: MAKSSOUW (Mod Channel)