Loading...
Hello and welcome to lesson three, of our programming part five unit, Strings and Lists.
I'm Ben, your computing teacher for this lesson, and this lesson we're going to build upon that word, that we did in lesson two, where we looked at string handling operations.
And this lesson we're going to specifically look at substrings.
So if you can clear away any distractions that you might have, and get a nice, quiet place to work, that'd be brilliant, and other than that, all you need is your computer, a web browser.
You also need access to your about accounts.
So please, as always do ask your parents and carers for permission.
And it's always really helpful to have a pen and paper ready, okay.
So when you're all set, and you're ready, let's get started.
Okay, so specifically in this lesson, we're going to look at using a substring in a programme.
We're going to use the in operator to check for a substring.
and also we're going to look at chr and ord, and how we can use those to perform ASCII operations, okay.
So you've got the picture by now from the previous two lessons, but I like to start off with a question.
Okay.
So I'd like to just read the code on the left-hand side.
We're going to go through that in just a moment.
And the question is, what will be the output of print, when this programme is executed? So the programme in line one, we got variable that's got linked into string, beach.
We've got count to zero for letter in word, if letter is greater than C count equals count plus one, and then we're going to print count when we finish our loop.
Okay.
So what would be the appropriate, would it be two, that's option one, option two is five, option three is you can not use a great sign with a string, as this would cause an error or is it number four? Would it just help with the word count? Okay.
So what do you think? Okay, so just give you a moment, see what you think.
Okay.
Have you got an answer? Okay.
So let's see if you can give me answers at one, two, three, or four.
So shout it out for me.
So three, two, one, it was, it's actually answer number one.
So the answer is two.
So, so many of you might be thinking, why is it two? Okay.
Well, let's have a look.
Well, the great sign symbol checks that the letter is later in the dictionary than the letter C.
Okay.
So we look through, we iterate through each one, the characters in that string, we come across two letters that are greater or later in the dictionary than C, and those are the letters E and H 'cause they are latency.
Okay.
So that's the reason why.
So substrings.
A substring is a part of a string.
So in Python, we use a slice tool to retrieve that substring.
So again, see if you can read this code and work out what you think might be the output of print when this programme is executed.
So we have wordy codes, Hello.
And then it's going to say, first two equals word, got as square brackets, zero colon two, plus square brackets, and then print first two.
So what do you think? What's going to be outputted.
that might help you in this question, to remember that thing that we learned about in lesson two, when we talked about index positions of strength.
Okay.
So what do you think is going to be not positive? Well, it was H and E.
So I wonder how many of you thought it might be hate E and L maybe.
Okay.
Well, the slice tool accesses a substring based on the in-depth location of its characters.
Okay.
So we mentioned last lesson, the index begins at zero.
Okay.
So the zero is the first, the first number that we put inside those grey brackets, it's a number which you stop that substring.
Okay.
So that is H.
Now for those of you thought it might be AGL, okay.
There, the second number that we put there, if the stopping point.
So it's not inclusive of that number.
Okay.
so zero would be, H1 would be E, and two is off stopping points, which doesn't then carry on iterating, looking at that positions, that string there.
Okay.
And that's just the same as in the, the range we might use forloop.
Okay.
So it might say, for I am ratio five, it starts at zero and it goes up to five, but it doesn't include it.
Okay.
So this means that word zero, or two generates a new string within letters HE from that substring.
So here the question for you to put into practise and what we just learned about, what do you think will be output or print when this programme is executed? So at the same programme, the difference is we changed our range one to four.
What do you think? Okay, so the answer is ELL.
Okay.
'Cause once our starting point before is off stopping point into not inclusive.
So go one, two, three.
Okay.
So, ELL.
We can also check if a word contains a specified substring using the in operator.
Okay.
Now look at this one.
So it says, where do you course hello, if he in word print found? Now, it's a string combination "he" do you find that in the word, hello? Well you do so that what actually print True.
Okay.
So here the question for you, what do you think will be the output of print when this programme is executed? So username equals 12FranksR, if 12 in username, print, valid username, else print, username not valid.
What do you think? So is it valid username or not? Okay.
So say for what do you think three to one is a valid username because 12 the characters of the string, one, two, so yeah, one, two are in the username string.
So it's checking whether it is in that and it isn't that, so it does it the, if 12 and username evaluates to true and therefore it is going to print valid username.
Okay.
So what I'd like to do now is pause your video and complete a set of tasks for me now.
There's tasks for you to create, a three programmes using substrings.
Okay.
So I'd like to follow through the activities on task one.
Okay.
And create the three programmes.
Okay.
So have fun with that.
Enjoy it.
Okay.
Don't be afraid to look back over your previous work.
There's also a section of code at the beginning that you might use to help you.
So in sample code that might help you with the problem.
All right.
So give it a go and then unpause, and when you've unpause, we'll go through the solution.
Okay.
So let's now go through the solution.
So if you didn't manage all three, don't worry about that, okay.
Well I'd like you to do is just watch what I'm about, I just want to pause and minimise, and then you can always go back to your programme, safety and punch off.
Okay.
So let me head over to my regular account now.
Okay.
So what I've done is I've created my three sub routines.
Now you weren't required to put them into routines.
Don't worry about it if you didn't do that, this is just my demonstration purposes so I can run each one in turn.
Okay.
So the first one was to work out the username.
So simply start off with asking the user to input that starting year, I was told to under a variable start year, then I've asked them for their first name, that's on the available first name, then surname.
Okay.
Now what I need to do is work out, the substring of that year group.
Okay.
So with the start year is between zero and a two.
Okay.
And then the initial is the first name up position, zero swells.
I put that first name and I just want the first letter of their first name, and then the username is going to be the year sub string, plus their surname plus the initial.
Okay.
So if I've done that, okay.
I'm going to put in, I'm going to say I am year 10.
Okay.
And then I'm going to put in my first, Oh, hang on what we're doing? Not even running the programme.
That's silly of me.
Right? So let's, under one screen share, let's just call this a function.
Okay.
What's actually a sub-routine.
It's not a function 'cos it's not returning a value, okay, as a procedure.
So I'm going to call that now.
It was from that again so there we go, starting year, I'm going to put intent.
first name Ben, surname Garside.
So what do you think this should show? Okay.
I think it should show, can I sort of look 10 girls I'd be, I think, yeah.
So, year which is 10.
surname is Garside and the initial B.
Okay.
And there you go, 10 gossipy so that works perfect.
Right.
So let's get rid of that one then I'll just hashtag outcome outs.
Okay.
And that's for the next one from a year group one.
So the year group should have worked out what year group men based on my username.
Okay.
So my usernames should start with 20, 19, 18, 17, 16.
Okay And that will tell me what year group I'm in.
So I'm going to type in my username.
Okay.
So I'm going to type in 19 Garside B, let's see what would happen there.
Okay.
So it should say your user is in year eight.
And it does.
Okay.
Let's test it out with the username the previous one generate this on a pro 10 Garside B, now, because that is not in my list it should say, year group not found.
It does, okay, perfect.
So it's just checking if something is in the string.
So 20 is in the string, 19 is in the string, etcetera my thoughts.
Okay.
I just did that with this selection statements.
There we go.
Right.
Not one then password.
Oh my password should I've worked out whether or not I have a strong password.
Right.
So I'm going to test it out before I look at my code, passwords, We can start now by asking my password obviously.
So a valid password has been entered.
Okay.
So I'm going to put the typical password of password.
I just not allowing it 'cause it's not strong enough password, one, two, three.
There we go.
And now it's solid.
So what we've done there, is we've got password length checker and a numbers checker.
So what we've done is we've said if password is invalid, if password length is less than eight or on numbers equals zero.
Okay.
So that's going to evaluate to true or false company as evaluating to true.
So while true is going to say print password, and let's go check the length so reason using the length that let a password, sorry and then we're going to check each character in the password to make sure that there's a decimal.
Okay.
So we'll go under, it's making sure that if there is a decimal is going to increment them as by one.
So we're basically checking as long as a number, at least that's going to therefore evaluate to true.
Okay.
And the password length needs to be longer than eight.
Okay.
And that's how it gets done to better password.
Perfect.
Okay.
So that's how we did those three.
Like I say, I've described how to do that.
So if you did manage to do it, don't worry about it, but do minimise this and see if you can now have a go at coding it.
Okay.
Don't copy and paste it, the best thing to do is just keep looking back at it and trying to work out the best or unpack the bits that you were unsure of.
Okay.
So pause the video to continue with that.
If you didn't finish it, but if you did finish it, let's head over to the slides now.
Okay.
So the next part of our lesson is looking at ASCII conversions.
Now ASCII as a character set, used to represent all the characters on a standard American or English keyboard.
Okay.
Each character has a unique code and we can find the code by looking at an ASCII table.
Okay.
Now there's ASCII on your worksheet so you don't need to worry about memorising any of that table, not at all.
Now for each character in the table, a decimal code increases by one.
Okay.
So if you look at here, we've got A character letter, so the decimal value was 65 and the hex is 41.
Okay.
And you've noticed that increments by one so B is 66, hex is 42, C is 67 hex is 43.
So what do you think the decimal and the hex values will be for capital D.
Hopefully it's pretty straightforward.
And that would be 68 and 44.
Now Python has two functions that perform ASCII conversions and they are char not, okay.
Now the char, I mean open as a char, the chr open brackets and you put in an integer inside there.
Okay.
So I put a 97 as an example.
Now, if you put that integer, what that function does, it returns the character of prevalence.
And in that case, it is a lowercase.
Now ord does the opposite of that.
So ord takes the character, and returns its decimal equivalent.
So if I put in a lowercase, a, it would return to me that integer of 97.
Okay.
So I've got a question for you.
So what do you think will be output when this programme is executed? So we have character equals capital A that print ord character, and that's more equal 66 print chr decimal.
Okay.
Now you don't need to look at the ASCII table, you're not going to know the exact value here, but what we're looking for here is the fact that the character, these ord is going to return an integer, which is 65 and the char is going to return the character it is.
All right.
Perfect.
So what I'd like to do now is that I have not played around with this and the complete another programme for me based on hex decimal and using those char Nodes operations.
Okay.
So can you talk to, when you're working now and try out some ASCII convergence for me, and then when you finished, I'll be here when you get back.
Okay.
So how did you get something like that? And as always, I'm going to show you a sample solution to this.
Okay.
And as always, if you completed it, then you can just watch this and see whether or not you did the same, or if you didn't complete it, then just watch a bit until you got, I explained the bit that maybe got a stuck or helped you become unstuck and then pause it, and go back and see if you can complete the independently.
Okay.
So I've created a function again, you weren't, or sorry, a procedure you weren't expected to do that, I've just put it in one so I can run it when I want.
Okay.
So first of all, I started with a available that's said, and sequence.
Yes.
You'll see why in a minutes.
And I've got a blank message.
Okay.
Now, again, I'm starting off with this variable here that I'm going to use , as a condition for my while loop.
So message not finished equals answer equals equals.
Yes.
Okay.
Now answer does equal, equal, yes.
So that's true.
This evaluates the true therefore message not finished is true.
So essentially what we've gotten a while loop is while true while that's there, while the condition held with this variable is true, then keep doing this.
Okay.
So it's going to say, enter, coded number it's then going to a store that so it asked me to start number stores as an integer.
then we're going to convert, the number that has been in the industry has been entered into a character.
Okay.
And it's going to add it to a message for me.
That's going to say, would you like to continue? And if it does, it's going to loop through this again.
Okay.
If I put no, continue, it's going to set this, well, I was going to hear so if obstacles no so I've started no, therefore we're going to break outside the loop.
Okay, 'cause it's going to make my condition false and therefore break up.
Loop and finally print the message.
Now what I've done here is I've put the sample test data for you in there 'coz I asked you to put some, some run this code with this test dates and see what the output is.
So that's what I'm going to do.
Okay.
So let's run this.
Okay.
But I'm not running it yet.
Let me just run this.
Okay.
So I'm going to call my sub-routine now.
So enter coded number.
So the first one was 77, right? So, Oh, okay.
77.
Go on and continue.
Yes.
Next one was 101.
Go on and continue.
Yes, 101 Yes one, one, six.
A2 105, 8110, A2, 114, 111 Oh no.
I've put one more no Okay.
So, let's Just want to anyway.
Okay.
I know.
Hopefully, because I've made a mistake I'm not going to go through the whole thing again.
We'll work it out by the next one Cause it's the same character.
Okay.
So yes, 109.
Yes, 32.
Last one 50 and I don't want it to be no.
And the message was meat in room two, which I thought was interesting because that's maybe that's a returning character.
So my 11 that I put in would maybe have a turn counter cause put it onto a new line.
So maybe you look in your ASCII tables, so if you can work out what 11 was and why it did that, but we know that that was meant to be a zero that's the message secret message was meant to be, meet in room two.
Okay.
So hopefully you got that and then let's have the rebutted slides now.
Okay.
So to finish off this lesson, we're going to recap on some of the learning that we've done over the past two lessons.
Okay.
And I'm going to ask you to make more predictions.
So read this code here.
So word equals fish, Sub equals words, square brackets.
One colon three print sub, Okay.
So what was going to be output of print when this programme is executed? Do you think that number one is, number two, so fis, number three, sub.
Oh, do you think it's number four, An error will occur.
Okay.
So three, two, one given answer It is okay.
'Cause remember it one is the start points.
It does inclusive of one, but not inclusive of three, which is the stock point.
Okay.
So Scott, you're going to print out positions one and two.
Okay.
Next one.
That I just explained that that's the why.
Okay.
Next one.
So word ABC coded word equals quotation marks, for letter in word convert equals or lesser coded code underscore word equals coded underscore word plus STR and brackets convert and then print coded word.
Now I'm not expecting you to get your ASCII table out to help you answer this.
Some logic might be able to help you with this, this answer.
Okay.
So what would be the output print when this programme is executed? Is it 65 66 67? Is it 65, 66, 67? Is it 65 66 67 or so six, five, six, six, six, seven, or is it coded underscore word? Okay.
Remember logical hope Yes.
With this, okay.
So I'll even give you a clue.
So 65 66 67 are also the values of ABC.
Okay.
So what do you think the answer would be? Okay, so three, two, one.
It was three.
Okay.
And remember we are using a plus that which concatenates with joins.
Might you say common to separate, it joins them together, therefore, no spaces.
Okay.
Right.
So that's all for this lesson.
Okay.
And I really hope you've enjoyed it.
I hope you enjoy building those programmes that we did earlier learning a little bit about asking tip.
Okay.
I'd really love to see the programmes that you've made.
So please do share them with me.
And if you'd like to share them, please do ask your parents or carer to share your work on Instagram, Facebook or Twitter, tagging @national and using the #LearnwithOak.
Okay.
So that's all for me, and I'm looking forward to seeing you next lesson.
So bye.