This video belongs to the openHPI course Programmieren lernen mit Python. Do you want to see more?
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.
Scroll to current position
- 00:00Welcome back.
- 00:01Also in this video we want to deal more intensively with lists.
- 00:05For this we have in the last two videos already displays lists with simple data types, that we've used throughout the list.
- 00:14So like here for example the list closed with the three string elements, or the list code with the six integer values.
- 00:21Now Simon and Leonie are on a scavenger hunt and found another clue, which, unfortunately, is in a box with these same three locks.
- 00:29But through the two lists they have an idea, what numbers they can try to open the three locks.
- 00:35And to make the assignment easier, you now want to create a new list, in which the two numbers are stored for the lock red in addition to the colour, with which the lock can be opened.
- 00:47To do this, you create a new list with mixed data types, i.e. the list closed_with_code, which contains the element red as a string, and the two numbers 1 and 8, each stored as an integer.
- 01:00We can use this new list of mixed datatypes in Python as usual, which means we can output them and get the same list back.
- 01:09Or we can use indexing to access a single element and just spend this one too.
- 01:15But if we're going to make it clear, that the 1 and the 8 in our list belong together, we can therefore make additional square brackets.
- 01:24So we are making an inner list, in the pre-existing outer big list, if you will.
- 01:31So in our inner list we have elements 1 and 8.
- 01:35We can also output the various elements in our list.
- 01:40So if we now access our element with index 0 again first and write an index behind it again,
- 01:48in this case we output the characters from the string "red the first character, which is the r in the case.
- 01:55But if we now specify index 1 as the very first thing, we do not access a specific element, but the element itself, which is actually our list.
- 02:04So we access the list, which has index 1, and if we write a second "A" again,
- 02:11we access the element with index 1 again here in the inner list, which in this case is the number 8.
- 02:20So we can see that the right issue is coming out here too.
- 02:23Simon and Leonie made it, You have finally found the assignment for the locks and the matching codes and code them into a new list.
- 02:32But now they want to spend it, so Stella can find them and open the box accordingly.
- 02:38To do this, they output the assignment individually, For the colour red, for example, the numbers 1 and 8, as you've just done.
- 02:48We can use what is known as slicing, that is, this list, which is coherent, and extract individual elements.
- 02:58We write the list again and then in square brackets 2 indices, namely, from where to where we want to cut.
- 03:06Remember, our list starts at 0, as always and by specifying an index of 2 we cut in front of the element with the index number 2 and before the element with index number 4.
- 03:18We use the colon to indicate the two digits separately.
- 03:23Now, if we run this line, then we get a new list back with two elements,
- 03:30namely the element "green" and another element of type list, which in turn contains two elements, namely the numbers 1 and 5.
- 03:39Now, if we only want to cut once, we can leave out the second number.
- 03:44So in line 3 we only want to cut once before line 4.
- 03:49So we want to cut in front of the 4 and really get all the elements to the end.
- 03:53That is, if we write 4: virtually nothing, we get the elements with index 4 and 5.
- 03:59So we get the output "yellow" and the inner list 2 and 3.
- 04:03So much for cutting up lists.
- 04:07But we can also do more.
- 04:09With strings and lists we can create a string and thus convert a string into a list.
- 04:16The split method is available for this purpose.
- 04:18We can call this on a string to pass the spaces Split the set into individual elements within a list, as in case 2.
- 04:28If we now output the result, i.e. the split sentence again, as in line 3, then we get a list consisting of six elements, in which each word appears as an individual element of type string within the list.
- 04:41We have thus achieved our goal.
- 04:43But we can also turn the whole thing around.
- 04:45We just made a list from a string, but we can also make a string from a list.
- 04:50Python provides us with a method for this as well, it's called a join.
- 04:55So here we have again given a list with different elements.
- 04:58What we want to do is put these elements together, into a coherent string, which then separates the individual elements with spaces.
- 05:06Join calls a bit strangely, because we call it on our hyphen,
- 05:12in this case, the space character, what we want in the string between our words at the end.
- 05:17By the dot notation you then call join as normal and in the parentheses, hand over our list.
- 05:24At the end we get our string with the elements from our list, which are then separated by spaces.
- 05:30Let me make this clear, so we can also specify a hashtag as a space, for example, and then the elements in our resulting string would be separated with hashtags.
- 05:41But as long as we haven't reassembled the list into a string, we can do more with it.
- 05:47Namely, we can use any element from the list perform a certain action.
- 05:53For this we use the for loop, that we showed you earlier this week.
- 05:58The for loop iterates over each element.
- 06:01So we write for, then again a variable name, which we use temporarily to assign a single element to this list, in, and then the name on the list.
- 06:11separated by a colon and then indented in the new line the different actions.
- 06:15So in this example we just give all the words, that is, all elements of type String within this list in a single line, because that's how print worked.
- 06:26But what an odd list this is, that you gave?
- 06:30It's called clues, and not without reason.
- 06:33There are elements in it, to lead the children on their scavenger hunt to the final treasure.
- 06:39How to do it is as follows:
- 06:41The task is that the children should extract the words from the list, which have an e in the second place.
- 06:48It should be noted that the elements to which this applies must be output in the order in which they appear in the list.
- 06:55So Simon gets the idea, to use a for loop again for this.
- 07:00So we write the same line again as just before.
- 07:03Now something should happen in the for loop.
- 07:06For we want to inquire whether the second element of the word, what we are looking at at any given time coincides with the e.
- 07:14For this we can use a branching.
- 07:16For example, we have already got to know the if-branch here, and we want to use them here as well.
- 07:21So if we now ask if our word what we are looking at at the index position 1, which is the second character, corresponds to an e, it's exactly what we want to check.
- 07:35In doing so, we carry out the comparison with the two double actual-equal signs.
- 07:39And then we can give the string as usual, with which we want to compare.
- 07:43At the end follows a colon, and now we can write that, what is to take place in this if branch.
- 07:50So we always execute the print command then, if the condition in our if branch is true, that is, if it applies.
- 07:58So we get the issue: "The right way".
- 08:02And so the children know, how they can get to their treasure.
- 08:07And you know how we can do more with lists.
- 08:12We have shown you that lists can contain different data types and even further nested lists can appear in it again, so we can keep the codes assigned accordingly.
- 08:23And for the edition it was practical, that we have cut the list, that is, divided it up with the slicing ...and you've just extracted individual elements out of it.
- 08:34List and string have some similarities, and we can convert them into each other with join and split.
- 08:42Or we can iterate over lists using the for loop and thus perform an action on each individual element.
- 08:48Right. And with that, we'd like to say goodbye for this week and look forward to seeing you in week three,
- 08:54where we will take a closer look at functions and random numbers.
To enable the transcript, please select a language in the video player settings menu.