APPRENDRE LE PYTHON #4 ? LES LISTES

662.23k views2587 WordsCopy TextShare
Graven - Développement
Aujourd'hui , on se retrouve pour le 4ème épisode sur l’apprentissage du langage python avec la noti...
Video Transcript:
Hey well hi to all this is Graven for this fourth episode on python language learning in the third part we had seen how to create conditions that allowed us to check several elements with each other today I propose to attack a brand new concept in which we will deal with list programming as in life every day we are all led to list some elements as a very simple example to illustrate me when you go shopping you mostly create a list with the name of each of the products it makes your life easier since you can go through this list add beforehand elements in delete afterwards etc etc and know that in programming it will be exactly the same thing now that we've seen the theory let's go to practice we'll start by first removing the code from the last video to start on a new basis at this level there what am I going to do I will first start by creating a list to store nicks to simulate an online game we will have for example a list that will store as pseudo Graven, Anana and Cleymax etc etc to create a list we will start by creating here a new variable whose name will for example online_players corresponds to the players online I will put here the = of the assignment and that we will open here hooks it's inside these hooks that everything will happen so i'm going to put my different values ​​have strings so as usual we open the quotation marks and we will put a first value Graven for example we add a comma to add a second element the list is here I will put in Anana third element we continue Cleymax and we could very well continue so on so now I'm doing a print of this variable online players and launch the program and you will see that I have a list that contains these different elements but there you will tell me okay graven it's all well and good but how to recover a specific item in this list if I want to recover for example the first element how am I going to do well if you have to know that each item on my list has a number what's called a clue it's going to be just his position in this list if I want to get back first it'll be the number 0 the second 1 the third 2 etc etc but it's very important to understand that we start from scratch so I'll try I'm going to make a little print here, then I put the name of the list that I want to target I'm going to open the square brackets to say that I'm going to get an element so this first element graven is numbered 0 I run the program and there is good graven that has been posted so it works perfectly now if i ask you to retrieve the last item from the list how are you going to go about it then you have several possibilities either here you make a print of online player 2 since it's the third element, so here we have 0 1 2 if I run the program we have the last value or you can very well here use the len function we already had used to count the number of items in this list so there it goes you back that there are three elements except that if you try to recover the third bah there is none if I run the program on hold and it puts me an error he's trying to grab an item that's off the list so what should be done from here simply subtract one to this value so he will recover the second as we subtract 1, I run the program and it's good we have here that the max will add to this level there a value that you can that it works correctly for example bob and look at it we have a good bob which is the last perfect value now that you know how to display values ​​we will see how to modify them imagine that during the game I made a specific command that allows me to rename and so from Graven a little longer my pseudonym called Gravenilvec how am I going to do so I started by grabbing the first item from my list by doing here hook 0 and here i'm going to use the assignment as for the variables it will allow the first value of the list is changed with a new for example here we will put Gravenilvec so if now I make a print of my list before the change and after that and I run the program you can see at that level that the first value has been updated if now I want to inject a value between Anana and Cleymax I have a function that allows to do it you will do here online_player. insert which will inject a precise index berne 01 so this will be the position here 2, we will put as value bernardGameur123 and I launch the program we can see that this new value has been injected between the elements without modify them and imagine that now I want to change several values ​​at once for example from from Cleymax to the end of the list I'm going update the values ​​by for example paul and jacques we'll like just now retrieve this list open the square brackets and start from the third value which then carries the index 2 as we start from scratch then I'll put here a: and stop at the end the lists so And I recovered these elements from somewhere else if I tried to display them and run the program. you will see that it recovers them so to update them we will add another = and here we will reopen the hooks put the value of the first element so I put paul, and here put jacques I execute we can see that the last two values ​​have been updated so it works perfectly so now that we have seen how to modify existing values ​​in our list and how to inject from a specific index I will show you how to add others we imagine that a player he wears the nickname Gameur123 connects how are we going to add it to the end of the list so we'll just do online players here .
append this will just allow to get to the end of the list and inject new values ​​so I'm going to put here Gameur123 or that's the first possibility launch the program and we have at the end the new value but know that it is possible to extend the list and to put several for that you will do in onlineplayers. extend then you will open the brackets and put the values ​​you want eg Gogume1er first in first value and a second here gigi2k I'm doing that I run the program you will see that at this level my two values ​​have been added so now that we have all that we imagine that the player in Anana disconnects therefore it will be necessary to transfer it from the list for that you have two possibilities is to transfer it to you by its position so we know it's the second element so the index 1 for that I'm going to do here be a little del onlineplayers [1] else onlineplayers. pop (1) of the two elements allow you to do pretty much the same thing so if I run the program we have here the value to nana who has disappeared other possibility you can very well use the remove function which will allow you to transfer it by the name for example I would say there so the last value that is bob I run the program again and we can see that this value is gone and finally last way to delete elements we imagine that the game ended for example and want to clean refurbish your list either you do here a del of onlineplayer by putting the complete list and we will not hide it and a little redundant instead we could very well onlineplayer .
clear of course so if I run the program we can see that my list is now brand new it has been emptied of its contents so now you have understood the stakes of the lists I propose you to do here a new example where will play the mistress so there you will tell me but no we do not do that but if we will do a system of student scoring so what I'm going to do at first is going to be to create a new list that will store the notes of a student so I'm going to assign it to a new list to issue integers this time And if I post his notes and run the program we will have this result only when you are going to have many items in your list to be able to easily operate you will rather tend to make line breaks and for example to display 3 on the first three on the second and close at the end the dab hook is like that a better visibility also to add here a small comma in case we want to put other elements after to facilitate the task all we have seen in advance apply for this list ie we can very well to display the first value like this but I would like to go a little bit further with you on it in python you have the possibility as in other languages ​​to bring what are called modules they will contain a certain amount of functions and elements that you can use in your code for example we have a module called statistics i'm going to use this first module to use the mean function which will allow to average all of these notes so how to import this module to go to the beginning of your script and use the keyword import then you will put the name of this module so there i will start to write statistic and automatically he is going to propose it to me and there he tells me that for now I have not use what is quite normal so if now I create for example a result variable will appeal to the statistical modules - you will see that I have a lot of possibilities and the first one and it's the one that will interest me is the mean function that will allow us to average from our list of notes it saves you from recovering the first the second the third the fourth and the fifth the sixth note and divide by six if I ctrl and I click on this function it will lead me to the famous modules or we will have full full full of possibilities and every time it explains to you how it works I will display in the console the average student is from and at that level open braces we will be able to replace them by our value so I'm going out here quotation marks and use the function format and put here my result it will happen is that when I run the program right away it will replace the braces by the value and automatically it will do the conversion to string so that's a pretty cool way to do that now back to our dedicated statistics module we can do statistics. to display the functions that you want but know that it is possible directly from using the mean function to calculate the mean so how will our code know what this function is and what module it is in and quite simply instead of doing import statistics we will put here from to say that it comes from the statistics module and we are going to import our mean function and here I am going to add two notes to show you for example 14 and roy violence the program and we have good here the average of the student and ten Go, I will show you another module really practical that will allow us to do random actions it's the module random for example here will use it to do actions like mix our list of notes so what I'm going to do here is do an import since the module called random is imported the element called shuffle for now do not use it yet at this level, what I'm going to do is first display the list of notes then I used the shuffle function to mix it and then I'll show it again I run the program and look at first we have this list it is mixed later and now the elements are no longer in the same order so we'll delete that and I'll show you one last way to create lists I am going to create a new variable that will be called text and which will be equal to the input function which will allow to recover in console a text value enter a string of the form email-pseudo-password and what I want to do is turn this string into a list to retrieve each element in a unitary way so for that I will use the split function which will allow to cut this chain from what is called a delimiter intentionally between each element I put a little shot and that will allow to separate the elements so here I put in quotation marks the limiters dice which is a dash and if I do a text print launches the program game but for example here gravenilvec [at] gmail [dot] com -Graven- azerty123 I run the program and therefore look at it and it has exploded each item or insert them into a new list so if for example I want to make a print by putting hi and the name of the person I'm going to format the second element that carries the index 1 and we'll put here text 1 I add a comma your email here I put braces and add a second value so it will be text 0 and from here the first value and finally a comma your password and here I reinject them another value I run the program enter a string so gravenilvec @ gmail. com - Graven - azerty123 and if I run the program hi Graven your mails gravenilvec@gmail.
Copyright © 2025. Made with ♥ in London by YTScribe.com