Hi everyone, this is Graven for this second episode about Python language learning in the first part we had seen the basics and the different prerequisites needed to design programs and other scripts with this language. Today I propose to attack a first notion that will deal with VARIABLES, a variable is what it is simply an element that allows you to temporarily store a value, it can of the text of the numbers, character, etc. etc.
And what we can note is that these variables they are not fixed as their names indicate they can change, vary during the execution of the program Now that we've seen the theoretical part let's go to practice we'll start by commenting on the code we did in the previous video for that you simply need to add the sign # in front of each line of code. From now on they will no longer be taken into account when running the program to create a variable it will take place in three stages . .
At first you will have to choose him a name, something that will help identify it textually, because if you call any variable abcdefg you will not find it in your code, so I'm going to call it user name for example but I do not really have the right to do that in fact when we give a name to our variable we will have to respect a series of rules and conventions to which developers must formalize to evolve together in the same direction first rule you do not have the right to put spaces the name of the variable must be one and the same word for that you simply need instead of spaces to put a small underscore (_) either on your keyboard or the dash of the 8, second rule the entire variable must be written in lower case so you will write username also you do not have the right to put special characters %, @, . . they will not be taken into account and this might not make your variable work so you can not put values numerical in front of the name of a variable and the top of the top it would be to write all of your English code instead of putting username I will rather write username so these are rules that you will be able to master as you go along, but it is very important to note them first.
In the second time you will put here an equal that will not put saying that this variable will be equal to something it affects him a value for example I can put him Graven text so why I put it in quotation marks since that's what's called a string I have several characters g, r, a, v, e, n , . . which end to end form a string of characters which we will also call STRING as soon as you are going to want to write some text in your code you will have to put the text surrounded by these quotation marks so we will be able to comment already this first variable to say what it does, Creation of a variable 'username' with values, the word, Graven we follow with the creation of a second variable which this time will store an integer 50, 30, 200 so for that you will skip the lines we will create a new variable whose name is age and will be equal to what I wish for example me is 19 years you can also comment on Creating an age variable with the value 19 It is also possible to make decimal numbers for example 42.
7 12,3 etc etc for that we will create a new variable that will be called wallet which will be for example the wallet of a person and it will be equal to 125. 7 € so here it will be the wallet of a person and finally the last type of variables that I will introduce you, this is what we call the boolean it will give us two possibilities of answer is true with the value "TRUE" be wrong with the value "FALSE" We will come back in more detail in the next video for now I want to create a simple call is_happu and which will be equal to true by default, the same can be said here! You now have a small global idea types of variables that you will be able to use in your programs what i'm asking you this is going to be in keeping only two, the string of characters (username) and 19 which corresponds to the age we will already see how to display them and for that I will use the print function that I presented to you in the last video and we will start by trying to display username I run the program and it affects us well Graven so no difficulty at this level if you now want to display the age I have several possibilities either I redo a print and I put here "age", like this or so I can very well in the same PRINT, added here a comma to put a second parameters who will be here the age, in these cases if I run I would have Graven 19 well now let's see how to change the value of an already existing variable I told you earlier that the interest of the variable is precisely that it was not fixed can be changed, vary during the execution of the program that is to say that by default here is equal to 19 but we could very well give it another lower value for example here I can say that the age is now equal to 25 so what I come to do is that as the variable age and already existing it will come to overwrite this value with the news that is 25, so if I do a print of the age I run at the very beginning the age is 19 Then I change the age value with 25 and I display it, we can see that it has updated well we can comment on that level show username and age Change the value of age with 25 finally poster the new value now how to do from this variable age that is worth 25 to have as a result What are we going to do technically we should say to ourselves: +1 "and there I'll have 26 now just add age = front and it's just going crushed the old age value by the new one that corresponds to the result of age + 1 be 26, we will re-display this again and now one should have 26 at the end super know that it also works with other operators we can very well here for example makes age x age the age equal to 25 so it'll be 25 x 25 and if I run the program we'll have 625 for the history of strings it's exactly the same thing we can very well first display the value of the username change it meanwhile by another value for example here Youtube and re-display this value username And here's the result first we had Graven and then the value of exchanged by Youtube and that the card we will remove a little bit the print we have made to start on good bases and now from all the variables that we did, I propose to make a sentence so here we go to make a Print and put for example "Hi" here we will put the pseudonym of the person so we will simply added here a "+" that will allow to inject the value of the username, so it will put here Hi and there last value is Youtube so Hi Youtube we have this result, we can continue we add a +, we re-enter the game textual and there for example I will put: "you have" + age + "years!
" What we just did here is what is called concatenation we associate hard values with variables that can just change so we will test now and we have an error why he tells us we do not have the right to concaton in a string of elements which are not strings actually the age here is an integer so in fact it should just be converted into a string you're going to encompass it with a str function which will convert it into a string of characters. So I run and there is more this error Hi Youtube, you are 125 years old! so it works perfectly so now that we did that I propose to create a real program that averages three notes that have been returned beforehand by the user so we will start here not harvested a first note that for that I will create a new variable which will be called note 1 and which will be equal to the input function which will allow to recover a first value in console we will put in quotation marks a text which will make it possible to ask it, Enter the first note then we will harvest the second note same note 2 = input Enter the second note we continue note 3 = input Enter the last note and now that we have these three values we will calculate the average of the average so I'm going to create a variable results that will store the result that to have this result will add up the note 1 + the note 2 + the note 3 and the result of this calculation so I'll encompass it in parentheses will have to to be divided by three to just have an average so there I will display the result with the function print the result is + result, the problem here is that we will have two major problems first of all what returned by the console entry it's going to be a string so it's not going to work since we want integers it will therefore be necessary to convert them in integer, .
. For that you go encompass your input with an Int function that will make it possible to transform it into the same number and like this and the same for the last value the result or so can put here the average of the student is from and as here we want a string and that by default it is an integer we can do the same procedure but this time we want to convert it to string so string, I run the program Enter the first note we will put 10, Enter the second note 20, Enter the 3rd note 14. And there, the average of the student and 14,666 .
. . .
so it works perfectly for those who want to train on this first concept I gave you a first small practical work to do for next time you go there if reap a value it will be a person's wallet so be careful to give it a correct name then you will create a product worth 50 euros and I want you to post the new value of the purse of the person who bought, after his purchase you will have in descriptions the corrected of this small practical work as well as a quiz for those who wish to train on this first notion so here is a little bit what I wanted to show you today in Anyway I hope you enjoyed this video, if so do not hesitate as usual at Liker, comment and Share subscribe to the channel so you do not miss the next videos in any case it was Graven! Hi everyone and see you next time!