You are on page 1of 26

openSAP

Python for Beginners


Week 2 Unit 1

00:00:05 Welcome to week two of this course. The topic of this week will be Lists and Loops.
00:00:12 And unit one will deal with the question, what actually are lists in Python?
00:00:21 Consider the following situation, you would like to collect all the names
00:00:26 you would like to invite to a party. And you would like to do it with the Python program.
00:00:33 Using the input function you already know from the last week, you could, of course, get the
names.
00:00:40 However, how would you like to assign it to variables? Of course, you could name a variable
Name 1, Name 2, and so on.
00:00:49 But right at the beginning, when you implement the program, you do not know how many
people will participate.
00:00:57 So, that's a difficult question. Later on, you would like to sort these names alphabetically
00:01:03 and maybe print all these names. And actually, that's not possible
00:01:08 with the data types you are used to today. In this situation, a more complex data type would be
helpful,
00:01:17 a data type where you can enter an unlimited number of elements. And such a data type is a
list.
00:01:27 So, lists can be used to store multiple elements or multiple items. What do lists look like?
00:01:38 A list is created using square brackets, one at the beginning and one at the end of the list.
00:01:44 And all the elements that are contained in the list are separated by commas. What you can
see in the first cell here
00:01:53 is first a list that contains some strings. The second one contains some float numbers.
00:02:01 And the third one, some Boolean values, True, False, False, True.
00:02:07 And then the first list, the string list, is printed out. In the second cell, you see a list that
contains different data types in one list.
00:02:19 You see this 1, 3, 5, all integers, followed by True, a Boolean value.
00:02:25 Later on, there's a float and two strings. And again, the list is printed out.
00:02:33 It's showtime again, you remember? Now it's your turn.
00:02:39 So, if you have not done so, download the notebook, start your Jupyter server, open the
notebook,
00:02:46 and try to follow in the notebook what comes next. So again, here's the same motivation, what
lists are for.
00:03:00 But now, let's go a little bit closer into these cells, the programming and coding cells.
00:03:07 You see, we have a variable, and like we have done in the first week, we can assign a value,
in this case, a list using the equals sign.
00:03:17 And actually, here is our list. Starting with a square bracket
00:03:22 followed by a few integers, separated by commas, and at the end, the list is closed again with
a bracket.
00:03:31 And here, it's printed out, so you'll see that's the value. Okay, I haven't set back the list, let's
start again.
00:03:46 So once again, here is this motivation. Why do we need lists?
00:03:50 Because we do have lots of elements. Let's be a little bit closer looking at lists in these coding
cells.
00:04:01 Here you once again, see a variable, and you can assign, like we did in the last week using the
equals sign, a value,
00:04:10 in this case a list, to this variable. And this list is composed, as argued before,
00:04:16 by a bracket followed by all the elements of the list, which are separated by commas, and at
the end we have a bracket again.
00:04:26 And then we can print out the list. And actually, you see the list is given out
00:04:32 with brackets and commas between the elements. So no big surprise.
00:04:38 And as you can see, we can put all kind of data in here. Here, we have a list of strings,
00:04:44 a list of floats, and a list of Boolean values. And we give out only the string list.
00:04:51 However, we can have multiple data types in the list. So we can have 1, 3, 5, all integers,
00:05:00 a Boolean value, and so on, and so on. And if it printed out, no surprise,
00:05:06 that's what we are ending up with. Maybe you remember from the first week
00:05:14 that depending on the data type, you can use certain operations.
00:05:20 And that's not only true for these primitive data types, that's also true for complex data types,
00:05:28 in this case, our list. So what are typical operations that can be used?
00:05:34 We have this plus operator or plus sign, but here we call it concatenation,
00:05:41 which means two lists are simply put together to one list, one list follows each other. We have
multiplication, but be careful.
00:05:51 It's a multiplication of lists with an integer or an integer with a list, not two lists together, this
doesn't work.
00:06:00 And finally, we have the in-operator. And the in-operator gives a Boolean value back.
00:06:07 So either true or false, depending on whether the element, in this case 3, is in the list 1, 2, 3.
00:06:16 But again, let's look at a concrete example. Here, we have two lists, first lists equals to 1, 2, 3,
00:06:25 second list equals to 4, 5, 6, and we compose a new list out of this,
00:06:31 which is equal to first list plus second list, and we print it. And later on, we print three times first
list.
00:06:40 So let's see, what is the result? Yeah, as you could expect, 1, 2, 3, 4, 5, 6 is one new list.
00:06:48 So it's important to emphasize that we do not have two lists, one after each other,
00:06:55 but we have one list containing all the elements of the former first and second list.
00:07:02 And the same is true if you multiply the first list by three, we do not get three lists, we get one
list
00:07:10 containing three times all the elements. Finally, let's execute, let's check this in operator.
00:07:20 Here, we have a list of prime numbers, 2, 3, 5, 7, and so on, and now we can check
00:07:29 using the input function if a certain number is already contained in this list.
00:07:35 And you remember, we have this if else control structure. So, if my number is in prime
numbers,
00:07:46 the value of the operators is Boolean value, so we can use it in this if statement.
00:07:53 So if it is in, the first part will be printed, if it is not in, second part will be printed,
00:07:59 the else statement. I run it.
00:08:02 Let's check if the 11 is in. And no surprise, the list contains number 11, we can check, it's in
here.
00:08:10 If you run it again, and we check for the number 12... If you check for the number 12,
00:08:18 you see the list does not contain the number 12 and you see between 11 and the 13, number
12 is missing.
00:08:30 So what is learning, what are the key takeaways from this unit? If we have multiple elements,
00:08:38 if we have multiple data and we do not know how many data we have, then primitive data
types are not the correct one to handle it.
00:08:48 Lists, however, can handle multiple elements. Lists can be initialized, like primitive variables,

2
00:08:56 you can simply use this assignment operator, equal, this equal sign, to set an initial list to a
variable.
00:09:03 There are a few basic operations like these concatenations, which work on lists.
00:09:09 And using the in operator, you can check if a given element is contained in a list.

3
Week 2 Unit 2

00:00:05 Hello, welcome to week two, unit two, using an index to express the elements of a list.
00:00:12 In the previous unit, you learned what lists are, and how we can create them in Python.
00:00:20 In this unit, we will have a look at how we can access the individual elements of a list.
00:00:26 In Python, as in most of the programming languages, indices to access the elements of a list
start at 0.
00:00:35 So, the first element in a list has the index 0, the second element has the index 1, and so on.
00:00:46 Furthermore, we will see that strings are quite similar to lists.
00:00:50 Actually, you could call a string simply a list of characters.
00:00:55 And we will also see that we can use indices to access the individual characters of a string.
00:01:05 So again, it's showtime. I will switch over to the Jupyter Notebook
00:01:09 and I'll show you how to use indices for accessing lists in Python.
00:01:16 As I mentioned before, each element in a list in Python has an index.
00:01:22 For example, consider the following cell. The cell creates first a list.
00:01:28 The list contains fruit - an apple, a banana, a coconut, and so on.
00:01:34 And now we can use the index to access individual elements of this list.
00:01:41 To access a list with an index, the square brackets are used.
00:01:47 So, what you see here in line two, fruit_list[3]
00:01:54 accesses the element with the index 3 of this list, and the index 3 is the fourth element.
00:02:04 Let's execute this. You see,
00:02:09 the fourth element in this list is a pear. So there is quite a discussion...
00:02:20 In computer science, there has always been a discussion if it's correct
00:02:23 to start indexing elements with 0 or with 1, and there is a famous paper by Edsger Dijkstra,
00:02:31 which I linked to in this notebook as well, which argues that starting with a 0 is correct.
00:02:40 So it's important to get used to it. When you learn programming, after a while,
00:02:47 it'll become very natural for you that indices start at 0.
00:02:55 Actually, lists in Python have more than one index,
00:02:59 and this is what I've shown here in the small table. In this table, I again have the fruit list.
00:03:08 The fruit list contains an element. The fruit list contains an element apple,
00:03:15 an element banana, and so on. And each of these elements is indexed
00:03:22 with an index, starting on 0. So the first element, with the index 0, is an apple,
00:03:29 the second element, with the index 1, is a banana, and so on.
00:03:34 In addition to that, there's also a reverse index starting at the end of the list.
00:03:40 So the last element in a list has always the index -1,
00:03:46 the second to last element in the list has the index -2, and so on.
00:03:52 So in our case, the first element in the list has the reverse index of -5.
00:04:00 Let's have a look what this looks like in code. So again, I have here the fruit list,
00:04:05 and then I access this fruit list using different indices.
00:04:10 First, I access the element with the index 4, then the element with the index -1,
00:04:19 and then the element with the index 0. And as you see, the element with the index 4
00:04:31 is the last element in the list, the prune. Also, the element with the index -1
00:04:37 is the last element in the list, so we get, again, the result prune.
00:04:41 And the element with the index 0, as we learned before, is the apple.
00:04:48 Now that we know how we can access individual elements of a list,
00:04:54 we can also see that lists are mutable. This means that the individual elements

4
00:05:00 of a list can be changed. And this is exactly what is shown
00:05:04 in the following code example. So I have here a list of prime numbers,
00:05:08 2, 3, 4, 7, 11, and so on. And you might have noticed
00:05:14 that this list contains a number 4, and 4 is obviously not a prime number.
00:05:21 So what I can do, as lists are mutable, so the elements of a list can be changed,
00:05:27 I can access the third element in the list, which has the index 2, and change it.
00:05:35 And this is exactly what is done here. So this statement accesses the third element in the list,
00:05:53 this element has the index 2, and sets the value to 5.
00:05:59 Let's execute this program. First, we get as an result the list with the 4.
00:06:06 The 4 is obviously not a prime number, therefore we replace it.
00:06:11 And finally, we get the correct result. So that's an important thing to understand -
00:06:18 lists are mutable, so the individual elements of a list can be changed.
00:06:23 We will see later on that you can add to lists, you can remove elements from lists as well.
00:06:31 So now let's have a look at possible errors. What happens if I try to access a list element
00:06:38 or an index that doesn't exist? For example, I have here again a fruit list
00:06:43 and this fruit list has five elements. And what happens if I try to access the element
00:06:49 with the index 10? If I do this, I get an error,
00:06:55 and this error is called an index error. We see it down here, an index error.
00:07:02 And it also tells us what went wrong. So the list index is out of range.
00:07:07 This means we tried to access a list index that was not in the range of possible list indices.
00:07:17 And furthermore, there is also this little mark here,
00:07:21 which shows us exactly where in our program the error occurred.
00:07:26 So what I could do now, for example, I could fix this, change the index from 10 to 1,
00:07:31 and then our program would be free of this error. So it's important to remember when you try to
access lists,
00:07:40 you need to make sure that the index you are using is inside the list of possible indices for this
list.
00:07:53 So far, we've always created very simple lists. All our lists contained simple data types
00:07:59 like strings or Booleans, but of course
00:08:02 it's also possible to create lists of lists. And this is what I'll be showing in the following example.

00:08:14 In the following example, I have two lists, a list of records from the band Ramones,
00:08:19 So that's "Ramones", "Leave home", "Rocket to Russia", and "Road to Ruin".
00:08:23 And we have a second list here. This list contains records of the Sex Pistols,
00:08:29 "Never mind the Bollocks", "Flogging a Dead Horse" and "Anarchy in the UK".
00:08:34 And these lists are contained or are part of another list which is called records.
00:08:40 So, if I now access the first element of the records list, I will get the list of records by the
Ramones,
00:08:50 so basically this as a result. And as a result
00:08:57 from accessing the records list with an index is a list again, I can also combine one or two
indices.
00:09:05 So if I, for example, try to get the last elements in the record list of the Ramones,
00:09:11 I would try to access the first element of the records list. so the index 0,
00:09:18 and then the last element of the list, this is index -1.
00:09:24 And let's see how this works. So you see, accessing this list
00:09:31 containing list again with just one index returns a list itself.
00:09:39 And then I can go and access this list, which is a result

5
00:09:44 of my first invocation, and, for example, get the last element.
00:09:50 And in this case it would be "Road to Ruin". And as mentioned previously,
00:09:56 of course, I have to take care that the index is in the correct range.
00:10:03 Why would I need to create lists containing lists? It's quite common that when you work with
data,
00:10:11 you want to structure your data first. So not just have a flat list, but, for example,
00:10:17 like I showed here, separate elements in this list or group them together.
00:10:22 And that's exactly when you start creating, for example, lists of lists,
00:10:26 or, as we will see later, lists of dictionaries, and so on.
00:10:34 As I mentioned before, strings are quite similar to lists when we talk about indices.
00:10:40 We can access the individual elements of a string using an index as well.
00:10:47 And this is exactly what is shown in the last example. What I do in this example,
00:10:53 I first create a variable containing the string "Python Introduction".
00:10:58 And after this, I access the individual elements of this string "Python Introduction" using an
index.
00:11:08 First with the index 1, and then with the index -2. So what would be the result?
00:11:16 The index 1 we know is the second element, so that would be Y.
00:11:20 And the index -2 is the second to last element, that would be the O in this case.
00:11:27 So let's execute this part of the program. And we see, the result is the Y and the O, as
expected.
00:11:39 Finally, there's also a part I haven't been explaining so far, that's this 'if' statement at the end.
00:11:44 The "if" statement uses a new construct, the "in" keyword, and the in keyword can be used to
check
00:11:51 if a list or a string, or to be more precise,
00:11:58 if a sequence type contains an element. So we could, for example, check
00:12:04 if the string "Python" is contained in the course name.
00:12:09 In our example, that's the case. Our course name is "Python Introduction".
00:12:13 It obviously contains the string "Python", and therefore the text
00:12:20 "Happy Python Programming!" is printed. So what have you learned in this unit?
00:12:28 In this unit you have seen that you can access the individual elements of a list
00:12:33 using their index. You have also seen that this index starts at 0
00:12:38 and that trying to access an index outside the bounds of a list results in an error.
00:12:46 Finally, we have seen that strings are quite similar to lists, so that we can access the individual
elements of a list
00:12:54 using index as well. Thanks for watching and see you in the next unit.

6
Week 2 Unit 3

00:00:05 Welcome to unit three. Now we are talking about important functions
00:00:10 and methods for lists. If you have lists, of course you would like to
00:00:17 manipulate the lists. You would like to add an element or delete an element.
00:00:22 Maybe you want to sort the elements. And for these things, there are functions and there are
predefined methods.
00:00:32 There are really many things you can do with a list. You can sort a list, append an element at
the end.
00:00:39 You can check for the minimum or maximum. You can delete elements, or delete a certain
value.
00:00:46 I will show the difference later on. And once again, herefore Python offers
00:00:54 functions and methods that support this handling of lists. Showtime again.
00:01:03 Let's dive into the notebook and see how this really works. So, important functions and
methods for lists.
00:01:16 Let's first have a look at the functions. One function you can use is the function len, short for
length.
00:01:28 Functions always have a name, and have some brackets behind them,
00:01:33 and there you can then enter the list where you would like to run the function on.
00:01:40 Let's check here, we have a defined fruit list containing some strings,
00:01:44 namely some apple, banana, and so on. And here we now use this len function.
00:01:53 We put this fruit list as a parameter inside, and hand out the result.
00:02:00 Same later on for this string. And actually, we already argued,
00:02:06 strings have some flavor of a list, and so you can use this len function here as well.
00:02:15 Let's give it a try. And you see the fruit list contains five items.
00:02:21 Once again, check these print statements. First, we have the first part of the string,
00:02:26 "The fruit list contains", then the result of the function len(fruit_lists) is inserted here.
00:02:35 And at the end, again, we have a string. And in total, it simply reads like
00:02:40 "The fruit list contains five items". Let's double check.
00:02:44 Apple, banana, coconut, pear, prune. Right, it's five.
00:02:50 course_name = "Python Introduction", and basically the same type of string.
00:02:55 However, now we do not enter a list, but we enter a string in here,
00:03:00 and you see Python introduction consists of 19 characters. Let's have a look at the next
functions, min and max.
00:03:14 With min and max, you can check for the minimum or maximum. However, it only works if
there is a minimum and maximum,
00:03:25 that is, if these items in the list are comparable, if they're orderable.
00:03:33 In here, you see a list sap_stock_prices, and you see some float values.
00:03:38 And I do not want to go and check by hand which is a minimum or maximum.
00:03:43 But again, you see a print statement where we have added a min function and a max function.

00:03:50 And if I run it, then you see the lowest price in this list was 120.8 €,
00:03:59 and the highest price was 125.46 €. Important, the euro is added within the string.
00:04:09 It's not part of the numbers in here. Let's have a look if you try to work with min
00:04:18 in a list that cannot be ordered. Let's make a short example.
00:04:23 So if we have, for example, a list composing of True, 12, and a string, abc,
00:04:33 and we try now to create this min function based on this list, then you see we get an error.

7
00:04:43 Why? Internally, we have a smaller or bigger relationship,
00:04:51 and this is not supported between instances of string and Boolean. So here you can't do an
ordering,
00:04:57 and thus you can't find a minimum or a maximum. So let's delete this directly.
00:05:12 We can use the function sorted. If you use sorted, then the items in the lists are sorted.
00:05:21 Again, this only works if the list contains orderable elements. Important, sorted does not sort
the original list.
00:05:34 However, it gives back another list, which can be assigned to a new variable.
00:05:41 In here you see we have our random numbers. You see they are unsorted in these lists.
00:05:47 And when we call the function sorted, entering the parameter random_numbers, so our
unsorted list,
00:05:54 we give back, we assign the value to a new list, sorted_numbers, and both are printed out.
00:06:02 So once again, we can see first the example in here. You see it's still the unsorted list.
00:06:10 So even if we have put it as an argument into this sorted function, it remains the same.
00:06:18 However, we have a return value, namely our sorted list, which we assign to sorted numbers,
00:06:26 and now you can see, this one really looks fine. And again, we can use the same on a string.
00:06:35 Within a string, these elements are then sorted alphabetically,
00:06:40 and you see there's a space in here, which becomes the first, the smallest element.
00:06:46 And you can see as well, capital letters, capital characters, all come before the small
characters.
00:06:56 You do not want to go to discuss why this is. Let's have a look on these sorted functions
00:07:03 if you try to sort some values that cannot really be ordered.
00:07:08 And the same problem happens in here, what happened above with the min and max function.

00:07:14 If you would like to sort some elements of a list, they must be orderable.
00:07:20 So the smaller sign must be applicable. And that's the case, that's why we do get this type
error.
00:07:30 Let's have a look on some important methods for lists. So what actually is the difference
00:07:36 between functions and methods? We do not want to go into detail here.
00:07:41 However, if you call a function, then you simply write its name
00:07:46 and put the arguments in the parentheses. For example, print is a function.
00:07:53 You simply call print, open the parentheses, put the argument in there,
00:07:58 and close the final parentheses. Functions are, in a way, independent of objects.
00:08:07 Methods, in contrast, do something with the objects, but are part of the object. In this case, the
method sort is a part
00:08:26 of the list random_numbers. And if we want to call, if we want to execute a method,
00:08:37 then we always have to first call the object, in this case, random_numbers, followed by a dot,
00:08:45 followed by the method's name. And sometimes we can still put some arguments
00:08:51 or parameters into these parentheses. Let's give it a try, let's take an example.
00:08:59 Again, we have our random_numbers, we have our list. And now we try to sort it,
random_numbers is called.
00:09:09 We add this dot, and we add this method name, and we do not forget these parentheses.
00:09:14 And then we check, and you see now random_numbers is sorted. So again, the important
difference between
00:09:25 the function sorted and the method sort is that sort really sorts the original list,
00:09:37 whereas the function sorted does not change the list's original list,
00:09:44 but gives back a new list, which we can assign to a variable. There are a few more methods.
00:09:54 Like, for example, append, pop, insert, and remove. And those I would like to show next.

8
00:10:03 So here we have a small list, numbers consisting of the numbers 1, 2, 3, 4.
00:10:10 And append simply puts the element, which is in here, the 10, simply puts this 10 at the end of
the list.
00:10:21 Let's simply give it a try. And you can see 1, 2, 3, 4, and then 10.
00:10:30 So with append, a new item can be added at the end of the list.
00:10:37 And if you would like to do the opposite, if you would like to take away the final element of a
list,
00:10:44 you can simply use this method pop. And important, this method pop has return value.
00:10:56 Basically, it gives back the element. So you see here, we have, again,
00:11:01 our list numbers, 1, 2, 3, 4. If you pop this list, we have a value that is given back,
00:11:10 the 4, which is assigned to the variable last_item. And if we then print both in here,
00:11:17 you can see the list is really reduced. The last item has disappeared,
00:11:22 and the 4 is printed out, it's now stored in the variable last_item. If you do not want to put an
element, a new element
00:11:36 at the end of the list, you can use this method insert instead.
00:11:42 However, then you have to add the index. You have to say at which position
00:11:50 the element should be inserted. So here you have two parameters.
00:11:56 The first one gives the index. The second one is the actual element
00:12:02 that is to be inserted. So let's give it a try.
00:12:06 So you can see 1, 2, 3, 4 is our original list, and the 10 is now placed with index two.
00:12:14 1 is at the index zero, 2 is at the index one, and 10 now is at the index two.
00:12:21 You can see it, that's what we really wanted. You would like to get rid of a certain element
00:12:30 but not the last one, then you can try the method remove.
00:12:35 Remove simply removes the first occurrence of an element. So if you have several objects,
several elements
00:12:47 that are the same, then only the first one is removed. So again, let's give it a try.
00:12:52 You can see 1, 2, 3. This 3 has been removed.
00:12:57 This is away now, however, the second 3 still remains in the list. These are a few methods and
functions
00:13:09 that are used to manipulate lists. So what have you taken away?
00:13:15 What should you learn from this unit? You know lists, you know we have indices to manipulate
lists,
00:13:24 but there are a few functions and methods that really simplify your life and help you to remove
data from lists,
00:13:32 insert lists, sort lists, and so on and so on.

9
Week 2 Unit 4

00:00:05 Hello, welcome to week two, unit four, iterating using the for loop.
00:00:14 In this week, we will learn how we can access a list of elements and work with each individual
element.
00:00:22 As an example, consider real life. In real life, you often have a collection of objects.
00:00:28 For example, you have a group of students, or a pile of books.
00:00:33 There are certain operations you can't do with the whole group.
00:00:37 For example, it's impossible to read a whole pile of books. You have to read each book
individually.
00:00:46 In order to access each individual element of a group, or list, or a sequence,
00:00:52 a new control structure is required. This control structure in Python or in programming
00:00:58 is called a loop. And in Python, we have two kinds of loops.
00:01:02 There is a "for" loop and a "while" loop. In this unit, we will introduce you to the for loop.
00:01:10 So as always, it's showtime, let's head over to our Jupyter Notebooks,
00:01:15 and have a look at what the for loop looks like. So let's start with the basic syntax of the for
loop.
00:01:23 The basic syntax of the for loop is shown up here. The for loop consists of a keyword, "for",
00:01:31 followed by a sequence variable, the keyword "in", and a sequence.
00:01:41 The whole statement ends with a colon. And then similar to, for example, if else statement,
00:01:49 there is a set of statements that are executed for each element in the sequence.
00:01:57 So what does this syntax actually mean? I try to depict this using a small graphic.
00:02:08 So think of this graphic down here as the process representation
00:02:16 of what's happening with the for loop up here. So when the for loop is executed in Python,
00:02:25 the execution starts. And the first thing that's happening is it's checked
00:02:30 if the sequence contains additional elements. If this is the case, the next element in the
sequence
00:02:39 is assigned to the sequence variable. And the block of statements
00:02:45 is executed with this sequence variable. And once the block is finished,
00:02:53 we continue to here, and check again if sequence contains more elements.
00:03:00 So the loop continues to execute all the statements with the different elements of the
sequence
00:03:07 as long as there are further elements. As soon as this check here fails,
00:03:13 as soon as the sequence doesn't contain any more elements, the execution of the loop ends,
00:03:18 and the program continues with the first statement after the loop.
00:03:25 So let's have a look at an example. In the first example, we will use a string as a sequence.
00:03:32 The first example is this one here. What this for loop does, "for" the keyword,
00:03:40 every character "in" a sequence. And in this case, the sequence is a string.
00:03:48 We simply print the individual characters. Let's execute this.
00:03:54 And you see, as expected, we get "Hello Python programming", just not written in one line,
00:04:01 but each letter separately in one line. Let's try another example.
00:04:10 Instead of iterating through a string, we now iterate using the for loop through a list.
00:04:16 Here's a list, an example list. The list contains a few elements,
00:04:20 a few integers, a string, and a Boolean value. And again, we can use the for loop
00:04:27 to work with every element in this list. And how do we do this?
00:04:32 The keyword "for" followed by a sequence variable,

10
00:04:35 I called it "element". So for every element inside our list,
00:04:41 we want to do something. Again, I just print the element of the list.
00:04:46 Let's execute this as well. And again, we get one line
00:04:52 with each element of our list as a result. Let's have a look at another example.
00:04:59 So what could we do with a for loop? We could, for example, try to find the smallest
00:05:05 or the biggest element in a list. In reality, Python has a function called min,
00:05:13 which can be used to find the minimum element in a list of numbers.
00:05:17 And it also has a function called max that can be used to find the maximum element
00:05:21 in a list of numbers. Nevertheless, we try to build
00:05:24 this functionality now ourselves using the for loop we've just learned about.
00:05:30 But important, keep in mind if you ever want to do something like this in a real program,
00:05:35 you should better use the min or max function from the Python standard library.
00:05:40 But let's build this functionality ourselves using the for loop we just learned about.
00:05:45 Why are we doing that? It shows us some important patterns
00:05:50 we always have when working with loops. So let's start.
00:05:56 First, in this little program, I have again, an example list,
00:06:00 and this example list contains a few numbers. In this example list,
00:06:06 you are easily able to spot the minimum element right here because it's the only number
containing just two digits,
00:06:14 all the others are three digit numbers. Nevertheless, let's try to write a program
00:06:19 that finds the minimum element in this list using the for loop.
00:06:24 How do we do this? First, we need a helper variable.
00:06:30 The helper variable will contain the minimum element. I call this helper current_minimum,
00:06:37 and as long as we don't know anything else, I just assume the minimum element
00:06:43 in our list is the first element. So this is true unless I check the rest of the list,
00:06:50 and that's exactly what we do next. So using the for loop,
00:06:54 I now check every element of the list. And how is this done?
00:06:59 For every number in this list, I do the following. I compare the current number
00:07:07 with the current_minimum number. And if the current number,
00:07:12 so our sequence variable up here, is lower than the current_minimum.
00:07:18 We know have a number that is smaller than the number we currently have as our minimum.
00:07:24 So we assign the current_minimum to the new number. So we have the new minimum value.
00:07:32 And once we leave this for loop, we know that we checked for each and every number
00:07:40 if it's smaller than the current_minimum number, and therefore the result is clear,
00:07:47 the current_minimum, this helper variable, contains the minimum value of this list.
00:07:54 So, let's give this a try. I execute the program, and sure enough,
00:07:59 we get as a result - the minimum value in this list is 68. What you've seen with this example
00:08:10 is a common approach of defining a helper variable, then working through all the elements of a
sequence
00:08:20 using a for loop, and after the loop, do something with the helper variable.
00:08:27 Therefore we calculated the minimum using this approach. In reality, just to stress this again,
00:08:34 you should of course always use the min and max function from the Python standard library.
00:08:43 And just so that you have seen this once, I'll print simply the min using the minimum function
00:08:52 from the Python standard library as well. And as you will see, as expected,
00:08:58 we also receive the 68 as a result. So now it's your turn to practice using the for loop.
00:09:11 Therefore, we have a small exercise. Your task in this exercise

11
00:09:15 is to write the following program. Your program should ask the user for a sentence.
00:09:21 Next, your program should ask the user for a letter. And finally, your program should print the
initial sentence,
00:09:29 but without the letter the user entered. So as an example, have a look here at our exercise.
00:09:37 If I enter, for example, the sentence, "This is how it should look like",
00:09:44 and I want to remove the letter i from it, then the result should be whatever is written down
here,
00:09:51 which, obviously, I can't pronounce because it's missing the i's.
00:09:57 We have a few hints for you. You know that you can get an input
00:10:03 from the user using the input function. You again will need some kind of helper.
00:10:10 We recommend using the result as a helper variable, and initializing it with an empty string.
00:10:19 And next you need to iterate over the input of the user letter by letter,
00:10:24 and check if the letter is equal or not to the letter that needs to be deleted.
00:10:31 If the letter is equal to the letter that needs to be deleted, you do nothing.
00:10:35 If it's different from the letter that needs to be deleted,
00:10:39 you append the current letter to the result. And in the end of the for loop, you will have
00:10:46 the initial sentence, but with the letter the user entered removed.
00:10:53 So now it's your turn, pause the video as always, and try to solve this exercise yourself.
00:11:01 So welcome back. I will show you one possible solution of this exercise.
00:11:06 And as always, this is just one possible solution. If your solution looks different,
00:11:11 it doesn't mean it's incorrect. There are several approaches to solve this exercise.
00:11:17 So what do I need to do to solve this exercise? First, I need to ask the user for some input.
00:11:22 So I will get a sentence from the user. And I use the input function for this.
00:11:34 And I just copy the text from up here, which reads, "What sentence should be output?"
00:11:42 Next, I need to get the letter from the user that is to be removed.
00:11:51 I call this variable the unwanted_letter. Again, we need an input function for that,
00:12:01 which letter should be removed. Now I have the input I need,
00:12:07 and now the actual work needs to be done. And therefore we will use the for loop.
00:12:13 But before we use the for loop, we create a helper. The result is an empty string.
00:12:23 And now we work through every letter in our initial sentence,
00:12:30 for every character in the sentence we do the following. If our current character
00:12:44 is equal to the unwanted... let's better call this character
00:12:51 so that we are a little bit more in sync here. So we have a character and an
unwanted_character.
00:12:57 So if our current character is equal to the unwanted character, we do nothing.
00:13:04 And else, else, we append the current letter to the result.
00:13:18 And we use a += statement for that. What I did here, I put the three dots here
00:13:29 just to show you that there might be a possibility to optimize this program.
00:13:34 If I don't need to do something for this if statement, I can change the check here
00:13:42 to get rid of the if statement and basically to collapse the if and the else statements
00:13:47 into just one statement, and that's what I will be doing now. If our current letter is not equal to
the unwanted letter, we just append it,
00:13:59 if it's equal, we do nothing. Therefore, I use the unequal comparison
00:14:08 to now see what to do with the current letter. And now we know, after the for loop,
00:14:14 that the result just contains the input sentence, but without the unwanted letter.
00:14:21 So what we can do as a result, we just can print the result as a result of the program.
00:14:30 So now let's give this a try. "Hallo Python Programming" is the sentence.

12
00:14:38 Which letter should be removed? Let's remove the capital P.
00:14:42 And as you see, the result now is "Hallo Python Programming", except for the capital P's.
00:14:50 Let's check our little program again. This time I'm using the input from the exercise,
00:14:56 just to be sure everything works correctly. We want to remove the letter i from this.
00:15:02 And again, you see the result we get from our program. And it seems to solve the exercise
correctly.
00:15:11 Let's jump back to our slides. What have you learned in this unit?
00:15:15 In this unit you have learned about the for loop. You have seen how you can use the for loop
00:15:19 to iterate through all the elements of a list, or through all the characters of a string.
00:15:25 Actually, you can use the for loop to iterate through the elements of a general sequence,
00:15:31 and we will learn more about sequences in the remainder of this week.
00:15:38 Thanks for watching, and hope to see you again in one of the upcoming units.

13
Week 2 Unit 5

00:00:05 Welcome to our next unit. So far, we have worked with lists
00:00:10 and we have worked with for loops during this week. However, there is one more item
00:00:16 we would like to show you, and these are ranges. So what are ranges?
00:00:21 What are they used for? In programming, you often need a sequence of numbers,
00:00:28 1, 2, 3, 4, or 10, 9, 8, 7, and so on and so on. And these types of numbers,
00:00:38 these types of sequences of numbers can be produced using a range.
00:00:45 A range can really easily create these types of sequences.
00:00:51 And, important, the for loop and the range work perfectly together.
00:00:59 It's show time again, open your notebook, and let's look at how it really works.
00:01:07 So here you can see a range. First, just as a comment,
00:01:13 you can see, we have a range. We can enter one parameter, here the 5,
00:01:19 and then a sequence of numbers is created. It's five numbers starting from zero,
00:01:26 we always start with zero, remember our indices.
00:01:29 And you get simply this range of numbers from zero to four, actually it's five numbers.
00:01:37 So what you should learn is that the last number
00:01:42 is not really part of the range. You can have two parameters.
00:01:49 If you do not want to start with zero then you can add a second parameter
00:01:54 and then the first parameter is a start value, and the second value is the ending parameter of
the sequence.
00:02:03 But important, the first parameter is really part of the sequence.
00:02:09 Whereas the 16, in this case, is not part. So it's the first number that is no longer belonging to
this sequence.
00:02:18 So again, you can see, we have 10, 11 up to 15. 10 is included, 16 is not included.
00:02:26 So you have two borders. The smaller one, the starting one
00:02:31 is part of the sequence, the second one is not.
00:02:38 And so far you've seen that we always step by one. So 0 plus 1 is 1, 1 plus1 is 2, and so on,
00:02:46 or here, 11 plus one is 12, 12 plus one is 13. And you can change this step parameter
00:02:55 by adding a third parameter in here. So having 2, 12, and 3,
00:03:02 again, we have the 2, which is the first number of the sequence,
00:03:07 the first number of the range, 12 is the first one which does not belong to it,
00:03:14 and then we have the step three. So we have 2, 2 plus 3 is 5,
00:03:20 5 plus 3 is 8, and 8 plus 3 is 11, 11 plus 3 is 14, which is bigger than our 12, bigger than our
border,
00:03:30 so it doesn't belong to the sequence anymore. And using this step factor and adding a minus,

00:03:39 you can even go down in the sequence. So you have a sequence that is really decreasing.
00:03:46 So 20, 5, -5. So again, the 20 is part of the range, 5 is not.
00:03:54 -5 is the step factor. So you can see 20, 20 minus 5 is 15,
00:03:59 15 minus 5 is 10, 10 minus 5 is 5, but 5 is actually the first number
00:04:06 that does not belong to the range. That's how range works.
00:04:12 And you can make many uses of it, you can use it in quite different ways,
00:04:18 just to create numbers. So let's have a look in these coding cells.
00:04:25 for number in range(10) print(number) You can simply see, we have, line by line,
00:04:32 the printout starting with 0 going up to 9. That's exactly what we expected.

14
00:04:39 So start, if you have just one parameter it's the ending parameter. We start with 0, that's a
default value.
00:04:45 We step by 1, and 10 is the first number that does not belong to the range.
00:04:54 For numbers in range 10 to 20, we do the same thing.
00:04:59 You can see 10 up to 19 each in one line is printed.
00:05:04 20 is the first value that does not belong to the range so it's not printed out.
00:05:11 And again, here we go down. 10, 8, 6, 4, 2.
00:05:17 And again, you can see, this is exactly starting value, which is part of the sequence.
00:05:26 0 is not part of the sequence anymore. -2 is the factor where we can step.
00:05:36 You can have a look at, let's say, certain situations. What happens if we just say 0?
00:05:46 Does it belong to this sequence? Does it not belong?
00:05:50 So give it a try, there is no output. Because zero is the first value
00:05:55 that does not belong to this range. It's not printed out anything,
00:06:01 the sequence is basically empty. What happens if we say, for example,
00:06:09 we would like to go from 0 to 10 and step down. Then again, the sequence is empty
00:06:18 because you are stepping in the wrong direction. So the sequence directly is empty.
00:06:26 So maybe you have to take a little bit of care when working with ranges,
00:06:33 think about which is the first, which is the last element,
00:06:37 which one is included, which one is not included. I change it again and repair it
00:06:43 so now we have the same result. Let's do two exercises, combining range and input first.
00:06:53 So what we actually would like to have is a range that runs through the for loop. But we would
like to hand in our parameters
00:07:04 through the input function. So what we can do is,
00:07:10 I say start = input("What is the starting value?") And remember, input always gives back a
string.
00:07:26 So what we have to do is to cast it into an integer. So what is our stop value?
00:07:37 int(input("Where to stop?")) And then we would like to get the step.
00:07:48 Again, it's input("How far should we step?") So now we have these three values
00:08:08 and we can start our range and run through the for loop.
00:08:12 for i in range(start, stop, step) and we print out our i.
00:08:29 So what is our starting value? Let's start with our 10.
00:08:34 Where to stop? 30. What would be the step?
00:08:39 Takes a 5. And you can see,
00:08:41 we have 10 plus 5 is 15, plus 5 is 20, plus 5 is 25.
00:08:47 And the next value, 30, is actually the same as we have as a stop value,
00:08:53 and this one does not belong to the range. Now, let's make it a little bit more complicated.
00:09:02 Let's do this famous FizzBuzz program. We should write a program that outputs the number
00:09:09 from 1 to 100, you can easily do with a range.
00:09:13 All numbers that are divisible by three should be replaced with the string Fizz,
00:09:19 and all numbers that are divisible by five should be replaced with Buzz.
00:09:25 And if number like 15 is divisible by both three and five, it should be FizzBuzz accordingly.
00:09:34 So let's do it step by step. And remember, the divisibility
00:09:39 we can check using this modulo operator. If a certain number, like let's say 10, is divisible by 5,

00:09:48 then 10 divided by 5 leaves no rest, so the modulo would be zero.


00:09:56 Let's do it step by step. for i in range(101)
00:10:04 Why 101? Because if you would like to go up to 100,

15
00:10:11 then the 100 would not be included. So we have add one more number
00:10:16 and we should start with a one, not with a zero. So let's put one in here.
00:10:26 Now we have to check the divisibility. So let's do it with if.
00:10:33 if i % 3 == 0 we say print("Fizz").
00:10:57 And let's make it easy for the moment. Let's say print i if it is not divisible.
00:11:06 Okay, let's give it a try. And you'll see 1, 2, Fizz,
00:11:11 4, 5, Fizz, and so on and so on. And we can go all the way.
00:11:17 And yes, it works. However, it's not complete yet.
00:11:22 So we have to check once more. So, elif i % 5 == 0
00:11:43 print("Buzz"). And again, you see, seems to work fine.
00:11:53 However, if you have a closer look, then you see here at the 15,
00:11:58 we do not have a BuzzFizz or FizzBuzz, we simply have a Fizz.
00:12:03 Why is that? If you come to the number 15,
00:12:11 then we have this print("Fizz"), we find i is divisible by three.
00:12:19 So we go in this case and we do not check the divisibility for five anymore.
00:12:28 So what we do have to do is we have to enter a second if in here.
00:12:35 So we have to first check if it is divisible by five as well.
00:12:41 So we check this, if i % 5 == 0 and then we say print("FizzBuzz").
00:13:06 And in the else case, that means it is divisible by three,
00:13:14 otherwise it would not come into the situation, but it's not divisible by five,
00:13:22 then we go in here, let's check again. And you see the 15 becomes a FizzBuzz,
00:13:30 the 30 becomes a FizzBuzz, fine. Maybe you remember one of our first exercises.
00:13:42 Maybe we could simplify it a little bit more. Now we have nested ifs,
00:13:48 which is sometimes a little bit nasty. What we could do as well
00:13:53 would be that we change the code a little bit and that we first check if i is divisible by both three
and five,
00:14:07 i % 5 == 0 In this case, we say print("FizzBuzz").
00:14:23 And here we say, okay, if not both, if i is not divisible by both,
00:14:29 we simply check now if it is divided by either three or by five.
00:14:55 So what you can see here is we have no nesting anymore.
00:14:59 Instead we have now four options. Either i is divisible by both three and five
00:15:08 or it's only divisible by three or it's only divisible by five
00:15:13 or it's not divisible at all. And then we have those four cases.
00:15:19 And again, you can see it in the result. Let's first give it a try.
00:15:26 And you can see we have these four cases, numbers, Fizzes, Buzzes, and FizzBuzzes.
00:15:33 So, that's one thing you can do with ranges. And here it's combined with a for loop
00:15:40 and with an if statement and with a modulo. So it brings together some of those things
00:15:44 we have learned so far. So what have you learned in this unit?
00:15:52 Using a range, we can create sequences of numbers. And these sequences of numbers are
used
00:16:00 again and again in programming. And as we have sequences,
00:16:04 and for loops really are based on sequences, you can use ranges and for loops perfectly
together.
00:16:14 Make use of it.

16
Week 2 Unit 6

00:00:05 Hello, welcome to week two, unit six. In this unit, we will be talking about sequences.
00:00:13 So, you have already learned a little bit about sequence types. Just a little recap.
00:00:21 There are several different sequence types in Python. For example lists, ranges, tuples, or
also strings.
00:00:31 Tuples we will be introducing next week, but they share some common characteristics,
00:00:35 therefore, I already mention them here. What's interesting, all these sequence types
00:00:41 share common characteristics. So each of the sequence types contain several elements.
00:00:47 The elements in these sequence types are ordered. So there is a first element and there is a
last element.
00:00:55 We can access all the elements in the sequences using an index. So we can access the first
element of a list
00:01:03 or the first element of a string. Similarly, we can access the last element of a string
00:01:10 and the last element of a list. And as the sequences contain several elements, they have
lengths.
00:01:19 Each list has a certain length. Each string has a certain length.
00:01:23 And besides that, there are common functions that we can use on all of the sequence types.
00:01:30 But not all of these functions work with all of the sequence types.
00:01:33 And that's exactly what I will be showing you in this unit. So again, it's showtime.
00:01:42 Let's switch over to our Jupyter Notebooks and see what functions we can use with the
sequence types.
00:01:50 So, here we are in our notebook. And just to repeat,
00:01:56 there are different sequence types in Python, there are lists, tuples, and ranges.
00:02:01 You already know about lists and ranges. And strings are also a sequence type.
00:02:08 They're a special sequence type called text sequences. I also added a link to the Python
documentation down here
00:02:18 if you want to read a little bit more about the difference between text sequence and standard
sequence types.
00:02:24 So, as mentioned in the introduction, there are common operations, which you can use on
most of these sequence types.
00:02:35 For example, there is a built-in function len. And this built-in function len
00:02:41 returns the lengths of a sequence. This function can be used
00:02:45 to get the length of a string, of a list, or of a range. Other functions that are quite useful
00:02:55 are the functions min and max. And these can be used to get the smallest
00:03:01 or the largest element in a sequence. In addition to that,
00:03:07 some of the well-known mathematical operators also work on sequences.
00:03:11 For example, you can use the plus to concatenate two sequences.
00:03:16 So to concatenate a sequence S and a sequence T into a new sequence containing the
elements
00:03:23 of both of these sequences. Also the multiplication, so the star operation,
00:03:31 can be used with sequences. What this does if you, for example,
00:03:35 execute i times sequence, it creates a new sequence with all the elements from s but repeated
i times.
00:03:48 Of course, we already mentioned in the introduction, you can access a sequence using an
index.
00:03:54 And there's also special keyword, in, which you can use to check if an element
00:04:00 is contained in a sequence. But not all of these operations work on all sequences.

17
00:04:07 For example you cannot multiply a range with an integer. So this operation doesn't work for
ranges.
00:04:20 So basically that's all the theory I want to give you as a background. So let's try this out.
00:04:30 For the next exercise I provided a few examples. You should now go on and play around a
little bit
00:04:36 with a string, with a list, and with a range that we provided here and try out different operations

00:04:42 and see what the result is. So as you already know, I would now suggest you pause the video,

00:04:49 play around with this a little bit yourself, and we come back later and have a look
00:04:55 at some examples together. So welcome back.
00:05:02 Let's have a look at this exercise together. So I deleted all the example code I provided to you
for this exercise.
00:05:12 So let's check different operations. For example let's check the length operation.
00:05:18 First we want to print the length of our list. Then we want to print the length of our string.
00:05:31 And finally, we want to print the length of the range I created.
00:05:40 Then as you'll see, we get some results back. So the length function works on all the sequence
types
00:05:47 I have here at hand, on lists, on strings, on ranges.
00:05:54 Another interesting function, in my opinion, is the min and max.
00:05:57 So let's try out the max. Let's get the max value of our list.
00:06:11 And we see is the next value of the list is a six. And of course this doesn't only work on sorted
lists.
00:06:17 So I create another list. Let's call this an unsorted list.
00:06:23 And this unsorted list contains different numbers. I just made up some of the numbers.
00:06:34 And let's format this a little bit more nicely. And of course I can also use the max function
00:06:46 on the unsorted list. And here I get the largest number as a result as well.
00:06:57 What happens if I use a string as an input to the max function? So let's use our string.
00:07:04 And so we get the letter t. Why is it's letter t?
00:07:09 That's again the Unicode representation of the characters in a string.
00:07:14 And in this representation t has the largest value, that's the reason why we get as a result the
t.
00:07:21 And max also works for a range, of course. So our range up to 100 in steps of two.
00:07:30 What is the largest number? It's 98, because 100 is not part of the range anymore.
00:07:41 So let's try one final thing out. Let's try to use the concatenation.
00:07:48 What happens, for example, if we create a result, which is the sorted list we have at hand and
the unsorted list.
00:08:04 And if we print this result we get a concatenation of these two lists.
00:08:14 Let's try this also for a string, so string_s
00:08:19 and I concatenate this with " – Python is fun!" And we get the list.
00:08:33 And as a result, we get the string concatenated of the first string we had in hand and the
"Python is fun!".
00:08:40 Does this also work for a range? Let's give it a try.
00:08:44 range_100 And we try to combine this, for example, with a new range
00:08:55 containing the numbers from zero to nine. And this doesn't work.
00:08:59 So concatenation for range isn't supported. And that's exactly the error message we get down
here.
00:09:07 Unsupported operation of type plus for range and range.

18
00:09:14 So the concatenation doesn't work for ranges. So much for this small exercise for you to play
around
00:09:22 with the different functions that are available for the sequence types. Let's switch back to our
slides again.
00:09:33 So, what have you learned in this unit? In this unit we visited the basic sequence types again.

00:09:39 You have seen that there are common operations available on the sequence types.
00:09:43 But we have also seen that not all the operations work on all of the different sequence types.
00:09:49 For example, a concatenation doesn't work for range. Thanks for watching, and see you again
in one of the upcoming units.

19
Week 2 Unit 7

00:00:05 Welcome to unit seven, which is about slicing. So far, you have always accessed either a
complete list
00:00:15 or individual elements of a list by using an index within this list.
00:00:22 Now, what is slicing? By the means of slicing,
00:00:26 you are able to access a part of a list. You can create sublists.
00:00:31 So for example, the sublist containing only the first three elements of a list.
00:00:38 How is this done? Actually, you specify the first
00:00:46 and the last index of your sublist. And similar to the range,
00:00:52 the first index which you give is part of the sublist, whereas the second is not.
00:01:00 Actually, you can include a third parameter, which defines a step.
00:01:06 It's actually the same way as you have seen in ranges. The single parameters are separated
by colons.
00:01:20 Let's dive into our notebook. It's showtime - open the notebook,
00:01:24 and we'll continue there. Let's first have a look at the syntax.
00:01:31 So you can see, we have our list, and we have a start, and we have a stop value, which are
separated by a colon.
00:01:41 And again, the start value is an index, the stop value is an index.
00:01:46 And the start value is the first which is part of the sublist,
00:01:52 whereas the stop value is the first which is not part of the list at the end.
00:01:59 So the sublist goes to index start to stop-1.
00:02:08 If you would like to include the very last element, you simply leave away the second
parameter,
00:02:15 and you can do so with the first parameter as well. So if you have some slicing like colon and
stop value only,
00:02:22 then it always starts with a first element. And if you do not have a start and stop value at all,
00:02:29 you simply include these colons, then actually you have the complete list.
00:02:37 Let's have a look. Let's check an example.
00:02:40 We have our list consisting of these even numbers, 0 to 10. And if we now say print even
numbers, 2 to 4,
00:02:50 very important, it's the index 2, not the value 2, then 0, 1, 2.
00:02:57 It starts with a 4, 3, 4,
00:03:01 and it ends with an 8, where the 4 is part of the sublist
00:03:06 and the 8 is not part of the sublist. Let's execute this cell.
00:03:12 And you can see 4, 6 are included. 8, the element with the index 4,
00:03:18 is the first which is not. 0 to 3.
00:03:23 You can see 0, 1, 2, 3. It's the first three elements, 0, 2, 4.
00:03:29 And similarly, 3 to the end will include all elements 6, 8, 10.
00:03:36 And again, as stated, the brackets where start and stop value are deleted are left away,
00:03:46 and we only have the colon - this is actually the complete list, as you can see here.
00:03:54 We already pointed out that strings have list-like attributes, and actually you can use slicing
00:04:05 on strings as well. And if you do it, we have this greeting "Hello world!"
00:04:11 and we print only the slice of this string starting with index 3 and going to index 8,
00:04:19 not including 8, then you see this "lo Wo",
00:04:23 which is actually this part of the string we have sliced. Of course you can use negative indices
as well.

20
00:04:35 As you have learned, the index -1 is actually the last element of a list,
00:04:43 2 is the second last element, and so on. So if we have our list in our example, 1, 2, 3,
00:04:51 and we execute it, then you see here, we do not slice at all,
00:04:56 we simply go to our last element. If we create a slice from -2 to the end,
00:05:04 it actually includes the second last elements... It's a list,
00:05:09 it's a sublist consisting of 7 and 8, the two last elements.
00:05:14 And if we go from the beginning to -3, then we have 1, 2, 3, 4, 5.
00:05:22 So the third last element, the 6, is the first one which is not included.
00:05:30 We can include a third parameter, so to speak, a third value in our slicing.
00:05:36 Yeah, so far we have had start and stop. If we have a second colon,
00:05:42 then we can include a step value as well. And actually it works like in a range.
00:05:49 Again, we create first a list. Now we use a range from 1 to 11,
00:05:56 which means we have the values 1 to 10. And we use this casting operator, which transfers,
00:06:04 which converts this range into a list, and actually we have this list of numbers in here.
00:06:12 Let's simply execute it. You see our list.
00:06:16 Now we slice this list. We say every second number is a list,
00:06:21 starting with the element with the index number 1, going to the end because we leave this
second number away,
00:06:31 and we use every second element. So as we go, you see here, we have index 0.
00:06:39 It starts with index 1 - this one is included, and then it takes every second element,
00:06:44 so the 4, the 6, and so on, and you see we get this list of even numbers.
00:06:52 And of course, you can now play around with negative values, step values, and so on, and so
on.
00:06:59 Here we have created some examples. I recommend to go through it carefully, have a look,
00:07:08 play around with it, change these values in the last cell in here, and just try to find out what
happens.
00:07:18 So again we create a list with the numbers from 1 to 15. 16 is not included.
00:07:26 If we simply leave away the start and stop parameter, but include the -1 as a step,
00:07:33 then you see the reverse list is simply a list starting with 15 and ending with 1.
00:07:42 And if we have a list starting at the very beginning, going to -4, and we go backwards,
00:07:53 then you can see it's the list with 15, 14, and 13. Again, I recommend playing around with
these options you have.
00:08:05 This slicing can look weird, especially if you have negative indices or are trying to go
backwards,
00:08:12 but simply give it a try. Let's come to our exercise -
00:08:18 create a list containing all the elements from 1 to 20. Actually we can take the code from here.

00:08:27 Now, of course, we have then to change this value, we can include this one as well.
00:08:39 We run it, and you see we have a list from 1 to 20. Now we should use a slicing operator to
output elements
00:08:48 with index 0 to 4, 4 to 8, and 8 to 10. And of course, let's do it in the next cell:
00:08:57 print(number) We have to slice from 0 to 4.
00:09:07 So 4 should be included, so we have to take the next one,
00:09:14 print... from 4 to 8... We have to...
00:09:23 Oh, it should be with an "s" - "numbers"... from 4 to 9.
00:09:32 And 8 to 10, the indices: print(numbers)...
00:09:41 with the slice from 8 to 11. So if you execute it, you see all elements are included,

21
00:09:53 starting with 1 to 5, 0 to 4 are five elements. And just check the rest.
00:10:04 So what have you learned? You can create sublists of lists,
00:10:10 this is called slicing. And actually this works not only for lists,
00:10:15 this works for all types of sequences.

22
Week 2 Unit 8

00:00:05 Hello, welcome to week two, unit eight, List Comprehension.


00:00:11 What is a list comprehension? According to the Python documentation,
00:00:15 a list comprehension is a way to process all or part of the elements in the sequence
00:00:21 and return a list containing its results. We will see in this unit that we can use list
comprehension to build new lists.
00:00:30 And this can be done using a very compact comprehensive syntax. And also list
comprehensions are quite useful
00:00:38 to combine multiple lists or filter lists. So, again, it's showtime,
00:00:45 let's jump over to our Jupyter Notebooks and I'll show you how list comprehensions are
performed.
00:00:52 So here we are on the Jupyter Notebook, and as mentioned in introduction,
00:00:57 list comprehensions are a way to process all or part of the elements in a sequence.
00:01:04 So let's do this with an example. Assume that we have a list of numbers from 1 to 20,
00:01:12 and we want to create a new list that contains the square of these numbers.
00:01:19 With the approaches we have taken so far, you would probably write the following program.
00:01:25 First, we create a list of the numbers from 1 to 20. Afterwards, we create an empty list
squares.
00:01:33 And then we use a for loop to iterate through each number in our numbers from 1 to 20
00:01:42 and simply append the square of this number to our result list.
00:01:49 And finally, we could, for example, print the result. Let's execute this little program.
00:01:57 And you see, the results are a squares from the numbers from 1 to 20, 1, 4, 9, 16, and so on.

00:02:08 Using list comprehension, we can write this little program much more concisely.
00:02:12 And how does list comprehension work? Let's use this example.
00:02:18 What we first do is we create the list of the numbers from one to 20 again.
00:02:25 And after that, we can basically transform this whole for loop
00:02:30 and create a new list into one expression. And that's the expression here.
00:02:35 A list comprehension contains the square brackets marking it as a list.
00:02:43 And in that the square brackets, we describe how we want to build this new list from the old
list.
00:02:50 And we want to build this new list by calculating the square of x.
00:02:57 And where does this x come from? For every x in our list of numbers.
00:03:04 So that's quite a complex operation that's happening here, but in a very concise syntax,
00:03:12 so we are calculating a square for every element x in our list of numbers.
00:03:24 And the result will be a list, and this is marked using the square bracket.
00:03:29 Let's execute this program. And you see, the result is our list of squares from 1 to 400.
00:03:37 And it's the same result that we achieved before using the more traditional approach we used
before.
00:03:46 So basically, a list comprehension is a very concise syntax to create lists. In this first example
of a list comprehension,
00:03:58 I created a list from a range. Actually that's not even necessary
00:04:03 because a range is a sequence type and we can use sequence types directly
00:04:08 in list comprehensions and that's what we've seen here. Here, I get rid of the numbers variable
instead.
00:04:17 I create a list and this list contains all x squared elements

23
00:04:25 from a range of numbers, here. So the list will contain all the squares of x
00:04:36 for every x in a range of numbers. Now let's execute this.
00:04:43 And again, we see it achieves the same result as our first approach. Then if you now compare
this one line here,
00:04:56 creating our squares list, to the program that we have written initially,
00:05:04 namely this six lines of codes. You see why list comprehensions are so nice,
00:05:13 really powerful construct to perform complex operations. And the syntax keeps the program
still readable.
00:05:24 Once you're used to the syntax, you will immediately see that a list is created here
00:05:29 and how this list is created. Let's continue with a second example.
00:05:37 It's also possible to use multiple lists in a list comprehension. Assume that we have a list of
letters, A, B, and C,
00:05:46 and a list of numbers 1, 2, 3, and we want to create a list containing all the combinations of
these two lists.
00:05:54 So I want to have a list that contains A1, A2, A3, followed by B1, B2, and B3, and so on.
00:06:05 How would I do this traditionally? I would write two for loops.
00:06:09 The first for loop that goes through all the letters in this list, a second for loop that goes through
all the numbers in this list.
00:06:20 Then I would concatenate both together and append them to my results list.
00:06:26 Using a list comprehension, I can do this much more easily. I can write a list comprehension
00:06:32 working through the tool lists in just one go. So, the combinations I want to calculate
00:06:39 is a list comprehensions, so we start with the square brackets.
00:06:43 What we are going to do is to concatenate a letter and a number converted to a string.
00:06:52 And where do the letter and the number come from? The letter comes from, for every l in our
letters
00:07:00 and the number n comes from for every n inside our numbers list.
00:07:07 So this is a list comprehension, which transforms two nested for loops
00:07:13 into one easily readable line. Let's execute the program.
00:07:18 And we see indeed the result is the expected combination of all the letters and all the numbers
in our initial lists.
00:07:28 So a really powerful construct to work with lists, or even with multiple lists,
00:07:35 or to be more precise with sequences or multiple sequences. So maybe we should do this as
well,
00:07:44 so that you have seen it once. So instead of using a list, I could also use a tuple here
00:07:52 and it would still work. We will be learning about tuples later on in this course,
00:08:00 therefore, that's just to show you that everything I've talked about so far
00:08:08 works with all of the different sequence types. One thing you can do with a list comprehension
is
00:08:16 create a new list from all the elements of an existing list. That's what we've done so far.
00:08:23 But list comprehensions are also very powerful to filter lists. As mentioned in the introduction,
00:08:31 list comprehensions are also very useful to filter existing lists or sequences.
00:08:37 So let's do this again, using an example. The example I've created here,
00:08:43 assume that we have a list of songs. So I have here a list of songs,
00:08:49 and this list of songs contains small lists again. And each of these lists contains a song title
and its play count.
00:09:04 So for example, we have here the first list element contains the song Ace of Spades,
00:09:09 and Ace of Spades has a play count of 99. Similarly, the last element in our list is the song
Paranoid

24
00:09:18 which has a play count of 33. And what I now want to do is
00:09:23 I want to filter this list of songs and get all the songs that have been played at least 30 times.
00:09:30 How would I do this without list comprehension? I would create a new list, I called it
favorite_songs here.
00:09:38 Then I would loop through my songs, so for every song
00:09:44 in the list of songs, I will check if the play count, that's the value at index one,
00:09:54 is equal to or larger than 30. And if this is the case,
00:09:58 I want to append this song to my list of favorite songs. So let's execute the program
00:10:07 and we see that in our initial list, there are four songs
00:10:10 that have been played at least 30 times that's Ace of Spades, Anarchy in the UK, Blue Train,
and Paranoid.
00:10:18 Well, that's a traditional approach to filtering lists. Using a string comprehension,
00:10:24 this can be expressed much more concisely again. So we have here again our list of songs.
00:10:34 And now we filter this list using a list comprehension. So our favorite songs are created
00:10:41 using a list comprehension, which starts with square brackets
00:10:45 and ends with square brackets. And how is this new list created?
00:10:57 So this list contains every s, and the s originates from our song list,
00:11:06 for every s in songs. But, and now that's the condition, we only add it
00:11:14 if the value at index one, so that's a play count, is larger than 30. So this list comprehension
states that
00:11:27 s is added to the new list if the play count, so that's the index one of our song, is larger than
30.
00:11:48 So basically we have the same information here as we did in the previous example
00:11:53 although there we iterate through our songs and then we check each individual song.
00:11:59 Using a list comprehension, this can be expressed very concisely
00:12:03 by just writing one line containing the loop for s in songs and the condition.
00:12:11 And we only add elements to our result list that fulfill the condition,
00:12:16 and the element is just the element s. So, let's execute this and we see we again receive the
same results.
00:12:25 So instead of filtering our songs using the traditional approach, which is up here,
00:12:36 there's an initial list and appending to the list manually, we can simply use a list
comprehension.
00:12:44 And finally, it's of course possible to combine what we've seen so far.
00:12:48 So we can manipulate the elements and filter them and I'll show you this right here.
00:12:54 Assume that we don't want to have the play count in our list of favorite songs,
00:12:58 but we just want to have the song title. So, of course, we can also manipulate the element
00:13:04 that's added to our list. And as a result, we now receive a list
00:13:09 that just contains the titles of the songs that have a play count of at least 30.
00:13:17 Let's jump back to our slides. What have you learned in this unit?
00:13:21 You have learned how to edit lists or manipulate lists in a more readable way.
00:13:26 And you have seen that you can use list comprehensions to even replace nested loops
00:13:32 so to work with multiple lists as well. This was the last unit of week two.
00:13:39 Thanks for watching. Good luck with the weekly assignment,
00:13:43 and I hope to see you again in week three.

25
www.sap.com/contactsap

© 2022 SAP SE or an SAP affiliate company. All rights reserved.


No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company.

The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its distributors contain proprietary software components of other software vendors.
National product specifications may vary.

These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP or its a ffiliated companies shall not be liable
for errors or omissions with respect to the materials. The only warranties for SAP or SAP affiliate company products and services are those that are set forth in the express warranty statements
accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release any functionality
mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products, and/or platform directions and functionality are
all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The information in this document is not a commitment, promise, or legal obligation
to deliver any material, code, or functionality. All forward-looking statements are subject to various risks and uncertainties that could cause actual results to differ materially fro m expectations. Readers are
cautioned not to place undue reliance on these forward-looking statements, and they should not be relied upon in making purchasing decisions.

SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company) in Germany and other
countries. All other product and service names mentioned are the trademarks of their respective companies. See www.sap.com/trademark for additional trademark information and notices.

You might also like