 In this video, we're going to translate the set column position from scratch into Python. And we're going to do it in a slightly cleaner way. And then at the end of the tutorial, I'm going to show you how to do it in just one line. Yeah, that's right. One line. Specifically in this video, we're going to be covering functions, which are just the equivalent of my blocks in scratch. I'm going to show you how to do those. And we'll be looking at some ways to use iteration. And we might even check out Python jitter as well. So let's get stuck into it. It's a lot to cover. Hey crew, it's the Serving Scratcher here, teacher-surfer programmer. And on this channel, I help curious learners just like you along on your learning journeys. In this tutorial, we're going to be continuing on in our From Scratch the Python series, where we're trying to transition you from scratch over to Python. In the last video, we looked into some data types, variables and lists. If you're not too sure what we're up to, go check a link for the starter project down in the description or that video in the top right hand corner. So first up, let's create our first function. You've been using functions before and we've done the type function and the length function in the previous tutorial. So we're going to create our own function now. And the way that you create a function in Python is you use the keyword here def def. So we're defining it. And we now put the name of what we want to call it. So let's just call it the same name set colon position in line. And after the name of the function, we need to put two parentheses or brackets. And then inside these parentheses, we need to put any parameters. Now you'll look over here in our custom my blocks, we've got one parameter here, the parameter of a line. So let's just do the same thing. We're going to put a line here. Now at the end of our function definition, and I guarantee this will trip you up because it certainly tripped me out when I was first learning Python, is you need to put that colon character there. You can see as soon as I put that character there, that little red squiggly line went away from the end of the line. So this is how you define a function in Python. And then we're going to hit return. And you can see that it is automatically indented us one level in. Okay, so we're no longer working at the level of depth. We're working at one indentation level in. Okay, so let's just go through this my block and try and translate it directly into Python. So we know that we can create a variable and call it colon position. We did that in the last tutorial. So we can initialize what's called here a local variable because it belongs inside of this function. And we're going to assign it zero. Then let's create the second variable and let's initialize it to zero as well. So much easier to do in Python than it is in scratch, isn't it? Okay, the next thing we need to do is this repeat until block. This is a little bit trickier. And we're going to use something that you've probably never seen before. We want to repeat until a condition is true. The way to do that over here in Python or one way to do that is using what's called a while loop. So we can say while something is false, do the thing. And when it becomes true, we'll stop doing it. Now the thing that we are going to be called equal to false is while colon position is equal to zero. Notice that we have to change it slightly here in scratch. We say when colon position is greater than zero here, we're saying, well, we set colon position to zero. So while it's equal to zero, we want you to do some stuff. And what's the thing that we want it to do? Well, we want to increment the value of I remember here, we're changing I by one. So let's do that. Let's get that underscore I variable and we'll reassign it to the value that it currently is plus one. That's it. So now if we executed this piece of code, it would run forever. So it kind of created a forever loop here at the moment, which isn't what we want. I want to help you visualize this. So let's go across to this website called Python shooter to help you out. I'm just going to copy the code that we've created so far. Okay, so over here in a website called Python shooter, and I've just put our code here and I'm just going to tidy it up slightly with all of our indentation levels. So they're all nice and gnarly again. Now what we can do here is we can visualize what happens by pressing visualize execution. And then you can step through it. Okay, so let's go next. And you can see the arrows here telling us what is happening. So we've just initialized our two variables. And you can see it over here in the right hand pane. And now we've hit our condition. So while column position is equal to zero, we're going to set I to its value plus one. And you'll see over here we've changed the value of I. And if I keep pressing next, you'll see that I just keeps on incrementing. It just keeps going and going and going because column position is always equal to zero. Even if I click the last here, you'll see we've gone up to 498. You see we've got a little error step limit reached. Okay, so we need to put in a condition to stop this from executing. Let's jump back over into replete. Okay, so we're back over here in a replete and we need to update our while loop here. And the thing that we want to do is we want to check for the column position in this line. So we're going to use this counter variable to step through all of that text. And then we want to locate when the position of I is equal to the column character, we can use the same if block. So the way to do if blocks in Python, is just to type the word if and then you'll have your condition here. Okay, just like what the green boolean block is here, we need that condition as a boolean. And the particular boolean condition that we're looking for is we're going to reference line here. Line is just that variable that we're passing in or this argument. Okay, it is the line. And we want to reference a position of that line. So we need the equivalent of the letter. Of line. And the way that we do that is a special syntax is that we use a square bracket. Okay, we need to use the square brackets here and we want to refer to the index of that line. Okay, so I've just said a lot of things going on there. And the index that we want is that counter variable underscore I. Okay, so if that is equal to the colon character. Okay, and the way that we finish that line is by putting the colon just like we did with the while loop and just like we did without function definition. Noticing that we're using this colon character at the end of our lines. You want to hit enter and notice that it is put us on another indentation level in. Okay, so I just want to demonstrate what is actually going on in here. So I'm going to create a variable down here into the console and I'm just going to assign one of those lines to it. Okay, so now if I type in line, we get that string variable there. Now that syntax that I said before about the square brackets, if we pass in an integer position and zero is the very first position. So that's something to consider as well. We don't start at one, we start at zero and using Python. This would refer to the first letter in this string or the first position which is the D. So if I hit enter, you'll see that we're going to get the letter D. If we put in the second index, well, that's going to give us the letter A. Remember the position of the colon character here is the fifth position. So that's actually going to be four without index because it's one less than what we would usually count with. Okay, remember we're starting at zero here. The next thing, if we have a variable and we call it underscore i and we assign it to that number four, it's currently four and we refer to our line syntax there. Instead of passing in the explicit number four, we can pass in that variable because that variable means four, doesn't it? Okay, if I hit enter, you'll see that we're going to get the colon character again. So that is how this particular line is going to work. We're going to use that i counter in the exact same way that we're using it over here in Scratch but this is how you do it in Python. Now remember, if the current position of line, if the current letter is equal to the colon, well, we want to set the colon position to that value of i and that's exactly what we did over here in the Scratch. Now that should stop our while loop. You may have noticed that over here in Scratch, we changed the variable i before the if condition and over here in Python, we're doing it after. Do you have any idea why we're doing it this way? Well, the reason is in Scratch is that we don't start at zero, we start at one. So we're going to change it straight away. We could have changed it in Scratch and initialized i to b1 and then change it after and then everything would be the same. But remember, zero is an important number because the first letter of line would be the position zero. Okay, the last thing that we need to do here is hit enter and I'm going to go back without indentations. We're going to the level where we've initialized those variables because that means we're now outside of this while loop. And what we need to do now is do this thing called return. And what return does is that it outputs some value. And what we want to output is the value of colon position. So what this will do is print or return or give us the value of colon position. So now when we run our project, we can call our function here by just saying the same, yeah, it's pretty long. We can paste in a line. We can use the closing bracket. And if we press enter, we should get the position four. That is what we've returned. This is the value of colon position that we have returned. Now there's two modifications that I'd like to make to our definition here. Instead of setting the colon position, I'd rather say get because we're getting the colon position here. We're not actually setting it. And rather than having to type in and paste this line every single time for different lines, we can do something pretty sneaky in Python. So when we have a parameter here, we can initialize it to something else. And we can make it have a default value. Now let's just have that default value to that sentence. So if we run this project again, and we say get colon position in line, and I don't feed it a sentence anymore, then it's just going to default to the one that is here. So if I click enter, we'll still get the number four. But if I feed it a different line, then it's going to give me a different value. And here it is at nine. And that's the kind of scratch equivalent of what we've just done over here. There's other ways more efficient ways that we could do this. Now just before we move forward, there's one thing I've got to say. And while we're up here initializing this parameter to this value, see how it's kind of like a variable up here. And every time we use this single equal sign, we're using an assignment here, where assigning the value on the right hand side to whatever we've called it on the left hand side. But down here in our condition, you can see that I've used two equal signs together. And I didn't mention that before. Now this is the actual equal sign. I know that it looks like it up here, but remember a single equal sign means we're assigning one value to a name. And if we put a double equal sign here, that is the equal sign in maths. So down here in the console, just to show you what that looks like, six is equal to six. Okay, and that would be true, right? But if I go six is equal to six, I've just assigned the number six to the variable six. So if I check the value of that variable, it's now equal to six, I could make six equal to seven. And if I type in six again, you can see I get the result seven. Now is six equal to six. No, that's not true, because we know that six, the name is equal to seven here. And seven does not equal six. So that may well just busted your brain. But yeah, what you need to understand and distinguish between is the single equal sign here of assignment and the double equal sign of comparing two values. Okay, very key point. That is a major point. And you'll probably run into some of those bugs down the track. Okay, so I had ambitions to show you a slightly cleaner way to do this, but I'm going to save it for another tutorial because this one's getting a little bit lengthy. Let's move straight on to the one liner. So the first thing we need to do is I'm just going to cut this comment out and we need to put it inside the function definition here, because nothing will get executed after a return. The return signals that, hey, this is the end of the function. If anything's off to here, we're not going to bother about it. Okay, so let's check out this one liner. And what we can do is type in our line and call a special function that is a string function. And I'll pop a link down in the description to the Python documentation for strings. So you can go and check out all the various functions that you can use on strings. We're not going to go through them here. But basically this function index, we can pass it a value that we're looking for. And it will return the first instance of that index. Okay, so this one line just does all this looping that we've done here already. And I can just assign colon position to line dot index of the colon position and then return it. Let's run this program. And then if we get colon position in line, we should get the same number four. And we do. Now I can just go in and comment out all this. Remember, whatever is commented out doesn't work anymore. So it's just going to be this line. Let's run this program just to prove to you that this code is not executed anymore. And it's just this one liner. I'm going to paste that into our console. And you can see we still get the number four. So look how much easier it is to do in Python. And we're always kind of be aiming to do things in one line. But now you know one way of how this might work under the hood. And there you have it. We've successfully implemented the get colon position in a line function. In the next tutorial, we're going to move across and implement our next one, which is this slice custom block. And you're going to get some good practice defining a new function with some other parameters here as well. And we're going to look into some more looping. So we're going to reinforce a lot of what we've done in the next tutorial. Congratulations on making it through this one. There's a lot of hard work, a lot of new concepts that came in here and we're going to reinforce it next time. So I look forward to catching you in that one. Until next time, I'm off to go find a wave. I'll see you then.