Python 9.5 : Variable number of parameters

We have seen above that we have functions with parameters and functions without parameters If I take for example the case where I make a small function which displays hello without parameters I do def ab colon I do a print Hello And I call my ab function like this, I run my code you can see that it displays normally and we said that we could pass several parameters at the function level maybe the age, the name and so on We also saw that the print function can take several values and of different nature, we could have the size that I will put 1 point 24 the age that I will put 22 And I will put a False I run my code you can see that it is displayed normally Inside the function print on a A string of characters, A float number We have an integer and we have a boolean Now how to create a function that calculates the sum of the grades of a student not knowing the number of starting subjects I will comment this and we'll do an example of a function that calculates the sum of the averages I do def sum parenthesis it takes two arguments I will first put a comma b a colon And I do a return of a plus b and at the bottom I will call my function I'll do print sum 12 comma 13, I run my code you can see that it's the sum and that gives us 25 And if now we wanted to buy another note maybe I put 15 comma I put 08 11 I launch my code there is an error we are told that on line 10 there is an error because we have entered more arguments than it was necessary for this to work we'll have to add maybe as argument c d e , f At the level of the sum function and at the level of the return we make 1 more of c, one more of d one more of and so on So we will make it simpler It's true that we could go through the collections and lists But instead we will put a star numbers with s usually it is star args for arguments But I'll take numbers I delete this and in the function I will do a for for i in numbers colon I will first do equal note 0 And inside I will make note plus equals 1 And we'll go back to the end return note I run my code you see that it gives us 59 Because it's the sum of all the notes if I delete I leave two I run it's 25 like we had in the beginning and now we can add a title Title comma like this and we'll do a print Title comma title and at the beginning we can do the sum of the scores you can see that it displays the sum of the notes then it shows the score obtained If I delete now the notes here I delete this And I restart my code you see that a note of 0 because when it wanted to loop it realized that I did not enter values and it return the 0 of the note that we had put at the beginning we had initialized it It should be noted that there is the star number or args so I'm going to make a star It works how much dictionary key equal to value for this I will put for example comma physics equal 12 comma math equals 16 comma history equals 15 comma and French equals 11 I run my code there is an error on the line 11 The value to return must be a number and not A key with a value And to do this we need to put a value point parenthesis like this, if I rerun you see that it works normally everything works normally but in our case it's the sum we wanted to do without using the dictionary so I'll delete this, delete and I run my code it works that was all for this section, we say to ourselves to the next one for a new section concerning the collections.