
So you can reverse it and you can get multiple ranges. We call that slicing. So you get a string slice, that means you get a particular— I would rather have a cake slice. time, I hope you remember, so finally go now. We’ve been trying to look into, like, why does it make sense and how easy it is to get started with doing something with programming. And now, the idea is to start with the basics and then really get something going. So I have prepared a couple of slides and want to go over them together with you. In order to do this, let’s jump right back. I think that’s better like this. So the picture that you can see here on the screen, that snake, this is actually the symbol of a Python programming language. And Python has a specific philosophy. Basically, you can import this, and then you see the philosophy. Instead of reading it here on the slide deck, I think it’s much more interesting to explore this directly hands on. OK? So let’s move back where we started last tim We were exploring the tool PyCafe as an easy place to get started writing some code. Can you take over the control and you basically— drive what I’m telling you to do. So let’s click the Streamloot app as last time. And instead of basically starting the Streamloot app here, let’s simply actually delete everything and simply type print hello. Now if we rerun this, we should very soon see the output. In fact, should show it to us. Exactl Here in the corner, we see the result. So simply typing print hello will basically allow us to do something. So you mean on the left side we have our comments, and on the right side we see result Yes. OK. So instead of typing here print hello, can you import this like we have seen here? Import this? You mean what? Can you type this import this? Import this here instead of hello? No, instead of everything. So delete everything her And now type import this. And import this. you import this special module, this you basically can get in touch with this then of Python. So this because Python is this? Therefore, we get like the— It’s a joke, but it makes a lot of sense. And basically, you can see these items would make Python special. And then basically, beautiful is better than ugly. And explicit is better than implicit. And simple is better than complex. items that make Python very, very useful, in particular, to newcomers like you are. So the idea is actually to have something that is readable, simple, and gets the job done. That allows you to solve your problem. And the idea is that even special cases should not be especially enough to break these rule And that it should basically be better to solve a problem now than never. So the idea is that the human is more important than basically the computer. So the idea is that there is no need for hand optimizing the low level code. But basically, it is good enough to write the high level, easy to understand Python code. The rest should work just fine at least as long as you don’t try to do something crazy. But for the start, that’s not what we’re up to. In different programming languages, for example, in Java, you may need a lot of boilerplate code. What about code? Boilerplate code, so stuff that you’re actually not really interested in order to type print in Java. You have to write all of additional text here as wel But in Python, as you have seen before, you can simply type print and the text, and you’re basically finished. So it’s much more concise. So it requires less boilerplate code. Or in other programming languages like C++ here, it also takes a lot of additional text. However, in Python, as we have seen before, it’s simply exactly what you want to say as a human being. So there’s no need for this boilerplate to make the computer understand what you want to do. It’s basically better to always include everything simple. mean, the same thing. We could also print, let’s say, print 1 plus And the output that we get— Yeah, would it be 2 or 1 plus 1? OK, OK. So in terms of words, it will give the specific word we type in, and in terms of a calculation or function, it gives the result, yeah? Yes. you can use it to write a comment. So then we basically add this here. Computer will not execute this line. So we can write, this line is not executed. And we can add additional context, but another human that should read the line needs to understand. Maybe you have made certain assumptions. And then you can state them here so that another human who is working with this can easily follow them. So it won’t be visible in the result part. It will be visible only in the comment part. Yes, it’s only visible in the raw source code. It is not visible in, basically, the output. So there are different types of comments that you should be aware of. You have these single line comments with a hash or pound symbol. And then you have multi-line comments. Multi-line comments are convenient if you want to document something longer. But why is there a difference? So why? Because basically, the computer’s amaha is to understand that the comment is longer than a single line. And you have to tell the computer, ignore whatever comes between these particular symbols here and just treat it as a comment. OK, so with hashtag only one line comments? Yes. OK. We have briefly touched upon variables last time with the input and the slider. But what is a variable? Basically, it’s a thing that has a name. Not so hard. So we can say the thing is named x, and the value is 2, for example. Let’s do this in Exploit Hands-On. You want to add this here? What exactl A variable? Or what exactly? Because there were different types of them? So we can say x equals 2. So one question before that. So always between those characters, we need to put a spac No, it’s just nicer to rea OK. So you can also type it like this. But it’s not as readable. In fact, later on, we will touch on tools that help us automatically format the cod They clean it up according to specific coding standards that some people agree on, that it looks nice. OK, but space, it doesn’t matter. It doesn’t matter. Maybe in some— What you want to do, but in general, it doesn’t matter so much. OK. And here we can, for example, interact with it and multiply it. And then we will eventually get some outcome, but only if we print it. Otherwise— So to get the out, yeah? Otherwise we don’t see it. OK. So what about other actions or calculations? Every time we want to see this result, we need to put print befor So print is just to define that it will come into output, section or not? Basically, yes. In the future, we’ll see a couple of other options to do it as well. But for the start, this is the most straightforward approach to see something. OK. So you just had print and then the thing that you want to look at and then you will see the output. And we can do these operations, like adding something, assigning something with numbers, with text, with pretty much anything that you can think about. We can even do this with code itself, so something like a function. Whereas computing something, we can even assign that one to a variable and then do something with it. A whole function, OK. And some programming languages explicitly require you to set some type in Python. This is not necessary, as we have seen, right? What did it mean with my typ So some programming language requires you to specify that x is a number. But here, as you can see, we don’t need to do this. So this, again, makes the code very readabl Imagine pen and paper and some math code that you want to crac Python is pretty similar to that. So there’s not necessarily so much additional fluff around it. That can be needed for simple things in particular. However, types are not gone. Types are still her So if we ask a computer what is the type of something, we will actually get it back. So if we go here and type— you want to look at it, right? Print type of, let’s say, x. And we will see the output is a class. And the computer understands on its own that this particular class is an integer or numbe Integer is basically discrete numbers, like one, two, three natural counting numbers. And then the computer inferred this automatically. There are means in Python to explicitly assign a typ And that can be useful in certain scenarios. But for the star we will omit them for the sake of simplicity. But when you build larger amounts of software, it can make a lot of sense to add them in. Because basically, types are like a kind of contract. And when you and I have a contract, and we have a handshake, and say, we agree on something, then it’s easy if you maybe write it down and if it’s not only verball And the same goes for the types. It’s easy to check later what the contract was about if you write this down with the types. And with that, let’s dive into a little bit of numbers. Wel we can do basic calculator things with numbers by simply assigning the basic calculator symbols and then that just works. Let’s not take too much time for this and rather explore it later hands-on. Because I think that’s quite clear for you. What is maybe less clear are things where the normal calculator doesn or here the model or operator or some exponentiation. We might have different, let’s say layouts, these types of functions on your normal calculator. But the basics ones are probably clear. Boolean values, are values that either true or false. And we can have some algebra around them as well and then have calculation Or we can check certain values of them. Like for example, here we can multiply them and see that the result is not true. Can see that some other result might be true. This allows us to check conditions. So in the past, we were talking about this example, right? Where you have specific data sets and maybe an energy plant is reporting their kilowatt hours. One of the preconditions might be a check that this is actually kilowatt hours, not kilowatt minutes or watt minutes or something else. But a specific type is matching or the semantically matching your expectations. And you could have some kind of automated check similar to what we see here. It is doing a quality assurance on the input data that you’re processing in that axle. And if you have a check, obviously the program of the axle that you create will be more reliable later on because it will automatically point out any inconsistencies or problems with the input. Well, and there’s obviously text. Like a lot of data is text. And by default, the text is more or less the dummy default type for almost anything. And text can get quite tricky. So it has characters that you might not know from the standard Net-N-Off, but it can be invisible characters even that sometimes hackers use to trick people or characters that look like an A but are acrylic A and are similar for domain and type of squatting topics. So it can be a bit tricky with characters, but in general, maybe it look like you would expect them to look like and operate in the same way like numbers work. You can add and even multiply tex So what do I mean with multiply text? Well, if we say instead of X is true is hello, for exampl we can now still multiply X. One question, if we want to have our variable to be text and we need to put it in this, how do you say emphasis? With quotes, yes, with the single quotes. Yeah, it’s necessary. So he understands this is our variable or? Yes, so basically text could be a space and we could say, hello D, n And then in particular for this reason, it’s important that it knows like the whole text should be this variable of X. And now as I told you, we can repeat it. What does it mean? Just multiply the text and then we concatenate the text here. Seven times, huh? Yes, seven times. I mean, people should know your name now. Maybe seven times watching, huh? Yes, MDNet is a really brave student and I hope over time you will learn something from me and eventually maybe be able to apply pieces of this to automate things that you’re annoyed with. So this is text basically. But text is a culmination of character So for example, if we have a text like alph we start by zero and then we have these particular characters and they have all the index. But what do you mean we start by zero? That’s, so if we go here and say print X in the place of zero. With in the place of zero? Yes. In the place of zero. But so with this, you mean that everywhere there’s a zero you put this variable or what? No, what I mean is like these characters here, they are sort of a list of characters and together they form the string, right? Okay. And the first one, we give it the letter zero or the digit zero, the next on one and so on. So we start with zero, but otherwise we just count them and we can look at them individually. But these, wait, wait, wait, these two commands, print X is zero and X instead of one. These two define that the first character of the string is zero and the last, what do you mean? No, but I want to find. What do we have down here? So basically we have defined that X is a specific, let me delete this so it’s more clean. We have defined that X is this particular variable. Let’s say your name, Dina. And now the first letter would be D and the second letter would be I. So if we want to manipulate the text and replace them or do something with that text, need to access the individual characters, right? But you just said that the first character, you said is D. However, now you have written print X instead of zero. So why this zero? So basically the characters are at least. Yeah. So what do you mean? Yeah, this is the first one, second is I and so on. Yes, and they’re basically released. If you ever want to access the element in the lis you have to use this bracket notation to we call this indexing, select that particular element from the list. So A is the zero element and not the first. So here D is the zero element or the first element? The first element is a zero element. So we start by counting with zero and we don’t start by counting with one. Okay. And we can then take the last element, A is at position three, so fourth element, because we start by counting with zero. But then if you go one further, let’s say four, we should see an error and it tells us, oh, this doesn’t work. We are too long now. This can actually go both ways and we can reverse this indexing as well. And we can even select a list and subset here. There is more than one item. I’m curious because Arthur has actually, oh, two, four, yeah, it has six digits, very, no, but if it has six elements, so to say, then it should be only from zero to five, what is six? This is why we wanted to demonstrate the error case that we had here. Okay. Cut out. No, it’s okay, I guess, to keep that. Because everyone should learn, right? And these questions are not bad by any means. It’s really important to ask these questions. So you can reverse it and you can get multiple ranges. We call that slicing. So you get a string slice, that means you get a particular— I would rather have a cake slic Right now we just ate. Too much chocolate, huh? I didn’t make it any, okay, so cut. No, but string slices are like cake slices, actually, that’s a good analogy, Tina. And then basically, you get a piece of a string. However, unlike cake slices, where ideally are all equal in size and equally good in taste, it doesn’t matter, not necessarily, but ideally, it doesn’t matter which one you take here with the string slice, the position totally matters. It’s really importan Because as you can also see here, depending on the position, you get a different resul Sometimes for non-ideal, normal, real cakes, it’s the same. If you don’t specify an end or start condition for these sliced cake you basically get a bigger piece of it. So never specify, hu It can be a convenient thing to do. And why there are two equality marks here? Because the idea is to show that this is the output. This is the output. So you actually, if you would type it in, you would need to type too, yeah? No, you don’t need to do it. In fact, let’s just try it and show it. So here, if you remove it like that, instead of I-N of Tina, we would say, “Din.” No, I’m confused because I thought the D is the zero. Yes. So the first character, this is the first and this is the second, but we asked to give the result until the third element. So to my understanding, it should be the whole element. But it’s zero, one and two. So D is zero, I is one and N is two. Exactly, and where’s the third one? Until it’s not including, it’s excluding the third one. No, it’s including the third one, but it’s excluding the fourth one, like thre We’re starting to count from zero, so three would be referencing four, right? Again, we start to count from the zero, therefore. So D is zero, right? Yeah. I is one and N is two. Exactly. And A would be three. But here, we’re going until three, that means we exclude three, this means we exclude A, the fourth elemen We exclude it. And zero, one, two, that is D, I, N are included. And do you also have it like in math, that if you use this other bracket, then it’s either including or excluding in the math, it was like that? That is possible and we will later see how to do this in more, let’s say, complex scenarios. But here, the default option is sort of excluding it. even get a bit more complex and pass in a step siz but that’s something I would say you skip for now. And you can convert between the types of values. So you can make a number to behave as if it was a string or you can convert a string back into a number. That is actually really important. So again, let’s come back to this example energy data, maybe one power plant is sending you a bit up data, where they have mistyped some formatting. And then maybe send you data like this where the numbers are in the shape of the text value. And then you need to clean this u I don’t know how often you had to do stuff like this by hand in Excel, but I imagine even doing it once can be super daunting. And here you can easily automate thi but you have to be advised if they put something here that is not a number, let’s say, and some other text value that cannot be passed to a number, then you will get an error message from the program. Mm-hmm. Yeah, this is it with the basic scalar value That being said, do you have any questions before we move on to collections? We can move on, yeah. Okay, then let’s look over into lists. And yeah, we will deep dive on them very soon. So we have already seen one type of list, it was the string, where it sort of, the string was implicitly a list of characters, but we can also construct our own lists. As you see here, one, two, three, and put them into this grab brace This is the array of lists, basically. And at least for the sake of Python, it’s sort of the same thing, but underlying it is an array list that is often object that is automatically adapted on the size. So there are other types of arrays that are more optimized, perhaps, but they’re more complex and not the ones we will be looking at for the start. So for your purposes, you can always think scrap braces and items inside, but it’s a list of all these cake pieces, cake slices. So some lists might be empty, others might have numbers, and even more might have letters. And in Python, it is possible to even mix the type So you can mix, for example, numbers and text in one list. That can be quite tricky in more typed or more strongly typed programming languages, because they tell you this list is only supporting text and not numbers, or only supporting one of the types and not a combinatio However, for someone using or consuming this list, it can be a bit simpler, because we can trust that this list has a well-defined type, right? So here, if you expect to get numbers and one element is a text, you might run into issues. And it’s possible to add and remove those elements for the list, for example, here, adding elements at the end, simply calling out to append. So it’s here at the bottom of the screen. Is this clear or are there any? What do you mean with add? We can add the elements and then— Okay, let’s try this in order. Do I need to understand this dot append? Let’s do it hands-on. Let’s say my list is Nina Georg. So now if you want to append somethin you can simply, you can simply add it. And now the question is, Nina, what should we add here? So append means we want to add something to the list here? Yes. I’m not English native, therefore for me, it’s good to explain this. So what should we add here? A child maybe. S as you can see in the output, we start with the list, where this basic only contains you and m and then we add a child. (Nina Laughs) Okay, so dot append is basically adding. As we have done before, we can also get elements on this list, a particular index or cake slice. This is understandable for you, so we can apply the same concept that we had before, or it will use this again. What do you mean with this? So we can again— Just like basically to say, okay, what was in this position? Which element was in this position? So we can take for something the zero position, and then we will get your back. Mm-hmm, yeah. Or we can rearrange and get both of us. Yeah, yeah, yeah. So here, you will see both of us. Yeah, you write two times actually. So this particular shell is printing twice, because it’s, first of all, keeping all the output that we had before, so you see all the old things, when we wrote, and twice, that will be printed here and here. So here we have it, both of us. We add the child, and then we take subset. So if we print three times, you will see us together with a child, and then alone again. because can you go back before you did, no, no, no, before you did, before you added these things, before you added another time, where we just had two times, mean delete, but now it’s still visible? Yes, this is the log, basically it contains all the things that we have done in the past. So it’s not clear, so when we rerun it, it doesn’t itself delete the log. Ah, if we delete some, okay. I think you might be able to delete it somewhere. Okay, okay, okay. But very soon, it will move away from PyCafe to something a bit more flexible, and there you will also be able to delete the log. It can sometimes be very convenient if you want to find a bug. Okay, because here we saw, okay, okay. Yes, but can totally be confusing if it keeps the log. Well, we covered the basic lists and in similar fashion you can have more complex lists that are nested with zeros in a minut As said before, elements can be indexed. So for the nested list we can have lists of lists. So we can, as you can see here for example, we can have sub lists, elements of a lis Is this understandable, Dina? So listed lists are nested lists? Yes. Listed lists. I like your analogies like the cake slices, right? Yeah. Python has a couple of, let’s call it magic functions. For example, this one gives you the length of an array or the length of a string. So you can always call this length function on the input and you will get the right value. It’s quite useful to know. Or for example, you can test if one element is in another one, like if character is inside of a list of characters, like a string. So this membership test is also some generic functionality, but it’s quite useful. for console.io we already covered the print to write something. You can also ask the user for input, like for some request name Control flow. So if you want to control the flow of a program. What do you mean the flow of the program? The flow basicall it’s like a cooking recipe, right? And if the water is hot, then you can boil the potatoes and otherwise you have to wait a little bit longer until the water is ho So you branch the instructions on what has to be done in what conditions. Sequences, yeah. Yes. So you control the sequences basically. And this is how that looks like in Python. So if the water is hot, then you boil the potatoes. It’s not super fancy. Oh, wait, can you go back? Because for me, it doesn’t look like a flow. It looks more like if statements, you know, if something is right, then this, if this doesn’t apply, then that, you know, for me, it doesn’t necessarily means flow for it. It means the condition conditional. Yes, it’s just conditionals. But if you combine more than one, you eventually end up in a flow, right? Like the cooking recipe. But with this flow, everything is in these if sentences or doesn’t matter. Yes, it is the condition is constrained in this line, basically, where you define it. And do you have also other options than if or you have other options? So for example, the possibilities to have other data structures and then perform key based lookups, but if is one of the most easy options. And there is a possibility for loops, for example, to iterate elements, but we will come to this soon. Okay. Yeah, you can basically have some harmonization how your editor looks like, but that’s that’s on this later hands on. If statements like this one are relevant, if you have more complex control flow. But this is exactly what we just discussed a minute ago. So I think that should be rather clear. It’s like complex cooking recipe that I usually don’t understand. And this LIF we currently don’t need to know. Yeah, this we will see. Basically, this is just changing the conditions. Changing. So this is the first condition with if. And then if you have another condition that you want to evaluate, then that’s else if or a lift in short, and then the last that’s the catch all if everything else didn’t apply when this one will happen. we could check given the word, what is the reversed word. And if they are the sam we could say they are palindrome, this is a rather naive implementation. But still, it gets the job done. a possibility to look at what variables are true or false. So for example, you can evaluate the boolean value of none. What is boolean? So we have discussed before, right? Volume values true and false. Okay, this is the same. And this is sort of the same thing. So given some input, you can evaluate if it’s true or false. Loops, Dina. Wel we can iterate over items. So when you have a basket of cake slice you eat them already one after the othe At least this is what I would do. It’s what normal people do, of course. Eat all of them. And then this is what we can do here with the loop. Basically, we iterate and take the item from the iterable, and then call a function, for example, and then do something with it. For example, consume it and eat it. Enjoy the chocolate. We can generate lists of number for example, numbers from a range her And that can be useful if you want to iterate over the first three items, for example. Always keep in mind that this is zero indexed. So it is including the lower bound and then excluding the upper bound. Four loops can be stopped, for example, with a break condition. And then it will jump out of the loop before the loop has ended. Can you come agai Four loops? What? So if you’re assuming you eat all the cake slices and you have ten in the basket, but you only manage three because you’re already full after three cake slice well, then you have to break out of the loop, right? And stop eating the other side. Okay. And this is the same thing, basically. And with continue, we can skip some statements, but in general, these two are not the easiest ones to handle, especially as things get more complex. about functions. Well, we can easily create a function that creates some parameters, like here, function name, parameter one, parameter two, and then do something with data. Let’s explore a function here. You know, let’s make a function that is multiplying, for example, the input. So we def multiply a and b and we return eight times b. Now we can print multiply dinner, for example, is. And we can also run multiply on numbers like And then we will see an outpu If we click rerun, here we get it. Mm hmm. So this is all that there is to functions, basically. And if the return statement, we ensure that the value gets returned to the caller. Assuming you only want to find prime numbers, then we could iterate over all the numbers and then check if it is prime and only if it is a prime number. Basically return the value. do we check if a number is prime? If we divide it with some numbers, whether there’s left, how do you say this left over? Yes, exactly. So there is no nothing left over. All the cake is eaten. You can basically take all the numbers that come before this number and check if any one of them would divide it. And if it would divide it cleanly without any remainder, then it is not a prime number, basically. So there are a lot of more topics to come. This was a brief dive about what we were looking at for now. And slowly, you will look into more and more and get more hands-on.