Este vídeo pertenece al curso Programmieren lernen mit Python de openHPI. ¿Quiere ver más?
An error occurred while loading the video player, or it takes a long time to initialize. You can try clearing your browser cache. Please try again later and contact the helpdesk if the problem persists.
- 00:00Welcome back.
- 00:01In the last video we have already looked at, how we create functions without parameters, so for example you can draw a tree.
- 00:08But we have always used some with parameters, such as the print function.
- 00:15Let's take a closer look at how it works.
- 00:17Exactly, so here we see the function definition, which looks very familiar to you from the last video.
- 00:25But what has been added now is a list of parameters, which we can give in parenthesis.
- 00:31These can also be several parameters, which we then separate with a comma.
- 00:37Parameters are therefore variables, that we can use in the function definition.
- 00:43We then use arguments when calling the function, which are actual values for these parameters.
- 00:50The example that Sebastian has just mentioned, print, we have used it several times before.
- 00:56For example, we have given "Hello" as an argument here.
- 00:59But we didn't just give you "hello." but many different arguments.
- 01:03And depending on the arguments we give we also get different results from the function.
- 01:11Let us first look at this in an example, again with the tree, on.
- 01:14You already know the example, we want to draw a tree, but this time anywhere.
- 01:19Therefore we write two additional parameters for the function definition in line 2, so we can pass the values for x and y.
- 01:29We then use this to run to the corresponding position, pick up our pencils, go there and put our pen back down.
- 01:38At the beginning these values are then correspondingly initialized by the x and y values as we pass them.
- 01:47We must then specify them when using them, that means we draw a tree here at the position 0, 0 and another one just up on the right in 70 and 70.
- 01:58If we run the program now, we see the tree first starting in the center and then just executed in the upper right-hand corner.
- 02:05We can also make this function call within a loop.
- 02:11For example, if we want to call the Tree function four times, we can do this on a loop.
- 02:18To do this, we pass a list of values back to the for loop, so these four values you see in line 2, and this then takes the function tree as i-value.
- 02:31So the function tree is called four times here, the y-value remains the same, i.e. always 0, so we always draw the tree at the same height.
- 02:41However, the x value changes, so for example at the beginning is the -120.
- 02:48So we must be careful, that we always pass the same number of arguments, as defined in the function definition.
- 02:57And also the order remains the same, so the first value here is the x-value and the second is always the y-value.
- 03:03And so we now see the four trees, which are drawn on one level.
- 03:08Fancy. But now we move on and actually want to go to London, so we finally get there.
- 03:15For this purpose, everyone should create a list of the attractions, which have a certain initial letter.
- 03:22Simon receives the initial letter B and I'm supposed to pick out all the attractions from the list.
- 03:28That is, next to Tower Bridge, for example, he looks mainly to Big Ben, since Big Ben starts with a B.
- 03:35To solve this problem, we can now create more subtasks.
- 03:42The first subtask would be to determine all the attractions, that begin with the letter B.
- 03:50It's a bit like that, as we explained in one of the list videos, where we wanted to check all the words to see if they had an E in the second position.
- 04:00So with the help of the square brackets. access the first letter, above the 0, because we are 0-indexed.
- 04:09And then we can check using this double equal sign, whether the first letter is a B.
- 04:17And in the comparisons we get, as we have already learned, always returns a truth value, so either True or False.
- 04:25In the code it now looks like this: So we define a function starts_with_b and hand over an attraction.
- 04:34So first we check, whether the first letter is the same as the B.
- 04:40This truth value that we receive, is then stored in the variable result and returned.
- 04:46So far we have not used this function, but only defined.
- 04:52This is similar to functions without parameters, so it makes no difference.
- 04:56If we go through with this, then it looks like this:
- 05:00So we use the function starts_with_b and indicate a value to be checked in the round brackets, so the string "Big Ben."
- 05:08We then store this value in the variable big_ben_with_b and try to spend it.
- 05:13We get True back, because Big Ben is known to start with the letter B.
- 05:18We can of course do this with another attraction, such as the London Eye.
- 05:25Here we use a nested call.
- 05:28These are always processed from the inside, that is, first we pass the string "London Eye" to the function start_with_b
- 05:36and then we hand over the result obtained there, which we get by return in line 3, to the function print and spend it accordingly.
- 05:47So we get false.
- 05:49So we have already solved our first subtask.
- 05:53The second subtask is now, to make a list of all the attractions that begin with B.
- 05:59For this we first create a new empty list result with just two square brackets that are empty.
- 06:07And to add a new element to this empty list, we know the append function.
- 06:14That means we can on our score, call the append function on our empty list and then add the new value in round brackets.
- 06:23So we are now defining ourselves a new definition, attractions_with_b and pass it a list, for example, as you can see here on the right side a short list of three attractions.
- 06:37Then we create ourselves within this function, therefore also indented, an empty list result.
- 06:44This is followed by a for loop, in which we go through all the attractions on this list one by one.
- 06:53And for each of these attractions we call the function, which we have already defined on the previous slides, starts_with_b at and so they give this one attraction.
- 07:07Before that, as you can see, there is an if, that is, we receive a truth value, so true or false.
- 07:14So if this execution is true and we get a True back, then the value, i.e. this attraction, should be added to the list, because we know that the attraction starts with B.
- 07:26And so we call here result .append with our attraction.
- 07:31Since we are doing this with the for loop, we now add all the attractions, that appear in the previous list, that begin with a B
- 07:41and can therefore with the return our result, so return our new list, which contains only attractions with B.
- 07:49Let's combine this with the actual list of attractions, that they received on the trip to London.
- 07:56The attractions are relatively many, so we've grouped these together in small print, but they're all on line 1.
- 08:03Now we add the function start_with_b again, which we already know and which just checks whether a word begins with B
- 08:11and the just introduced function attractions_with_b, which, as is well known, calls the function from above, starts_with_b again.
- 08:21So now we just have to call the function attractions_with_b with our list of attractions and then output the result.
- 08:30To do this, we call both functions again nested.
- 08:34That is, we pass the list of attractions to the function attractions_with_b and the result we get there, which is a list, we pass this to the print function to output it.
- 08:47And this long list suddenly becomes a rather short list, which then includes Big Ben, Buckingham Palace and British Library.
- 08:56Yeah, that's pretty cool, but what about all the other contestants?
- 09:00They are supposed to create lists with different initial letters.
- 09:04Surely we can reuse it.
- 09:06Right. But then we don't check if we have a B here, but simply whether we have a letter, where all attractions start with this letter.
- 09:18Of course we have to include this in the function definition.
- 09:23So we add the letter here and at the same time change the name of the function to start_with,
- 09:30because we still don't know which letter is being passed and how. and thus cannot call the function starts_with_b.
- 09:38Here below in line 10 the function is called again, so we have to give the letter again.
- 09:45And since the function is called from within another function, we have to pass the list also up here at attractions_with_b, where up to now we only pass the list,
- 09:57also pass the letter with and change the name here as well.
- 10:00We call this function at the bottom of the print, so it needs to be adapted here as well.
- 10:08And in addition, we are now also showing off here, with which letter all our attractions should actually begin.
- 10:15And thus we receive the "London Eye" issue as here.
- 10:18We can now easily adapt this and, for example, an N here and thus receive a new output with a new list of attractions that start with N.
- 10:31So much for functions with parameters.
- 10:35That's how we conceived you, how you can create your own functions.
- 10:39In the function definition, you specify the following within the parameters to specify a list of parameters that you want to use accordingly.
- 10:48If these are called by a function call, you then pass the corresponding values to her,
- 10:55which you then use as variables, called arguments, are available within the function call and can be used similarly to variables.
To enable the transcript, please select a language in the video player settings menu.
Sobre este vídeo
Hinweis: Variablen, die außerhalb von Funktionen angelegt werden, und Import-Anweisungen (wie bspw. mit from turtle import *
oder from daten import alter
) sind auch innerhalb von Funktionen verwendbar. Argumente und Parameter können genutzt werden, um veränderbare Informationen (wie den Startpunkt des Baums) an die Funktion zu übergeben, sodass diese entsprechend unterschiedliches Verhalten abbilden kann.