video

Lesson video

In progress...

Loading...

Hi, I'm Ben, your computing teacher for this lesson.

And welcome back to programming part four, which is subroutines.

And this lesson, we're going to explore the logic of XOR.

So all you'll need for this lesson is your computer, a web browser and again, you'll need access to your records account.

So please do ask your parents or carers for permission before accessing that, okay? And it's always helpful to have a pen and paper, so please make sure you've got one at hand.

And other than that, you just need to clear away any distractions that you might have, and when you're ready, let's get started.

Okay, so in this lesson, you will learn how to use a truth table, you'll be able to describe the function of an XOR operator, and then finally you're going to be able to design and create a function on XOR operator, okay? So to get us started, I've got a little bit of code for you to read and a question for you to answer, okay? So this requires you to read the code, and then answer the question which is, what will be the output when the programme is executed? So you've got four options.

Option number one, it will say that is true, that is true.

Number two, it will say that is true, that is false.

Number three, it'll say that is false, that is true.

And number four, it'll say that is false, that is false, Okay, so there's your options, so pause the video now, and when you think you've got the correct one of those four options.

you mind then unpause the video, and we'll go through the answer.

Okay, so what did you come up with then? So is it one, two, three or four? See if you can shout out the answer to me.

So three, two, one, it was.

one, okay, so it should say that is true, that is true.

Okay, so let's go through why that was.

Now what we want to do is highlight these key statements here, which are highlighted in bold, okay? I'm not highlighting those because those are the expressions that will evaluate to true or false, okay? So let's go through them in turn.

So, first of all, the first one, it says, when an AND is used, both expressions must be true in order for the whole condition to be true, okay? So the first one, it says if num1 is less than six, which it is, that's true.

And num2 is greater than eight.

Okay, so num2 is greater than eight, which it is, it's 10, sorry, so that is also true, and therefore, because both are true, both are true, because the AND statement there, the whole thing evaluates to true.

So let's look at the second one, which is using a slightly different operator, which is using the OR operator.

So when the OR is used only one expression must be true in order for the whole condition to be true, okay? So it only requires one, so we'll look at that.

If num1 is equal to five and it is, or num2 is greater than 11, 10 is not greater than 11, so that actually evaluates to false.

But because we've used the OR operator, it only requires one of them, so the whole expression evaluates to true, okay? So we will output that as true.

So that is true, that is true is the correct answer.

So what we can do to keep track of this is using something called truth tables, okay? So in order to keep track of the logical operators, and how they work, we can create a truth table.

Now a truth table is where we can record all possible outcomes of an operator, okay? And they're used when creating logic circuits.

So they're also used sorry, when creating logic circuits.

And you will learn about these in the computing systems unit as well.

Now let's look at this truth table.

Now a truth table is being created for the AND operator, okay? Now, if we run the code below, both expressions are false in the condition so evaluates as false, okay? So we can see this condition here, Num1 equals zero, num2 equals three, okay? Num1 is one, and num1 is two, okay? Num1 equals zero so that equals false 'cause num1 doesn't equal zero.

And num2 equals three, well num2 does not equal three.

So then those cases, it's false and false, because we're using the AND operator, which requires both to be true, that's going to evaluate to being false, okay? So what I'd like you to do now, is head over to your worksheet and go to task one.

And your job is to complete the AND and OR truth tables, okay? Using the code snippets to help you if you need to, okay? So have a go at that, and all you need to do is write true or false in the relevant boxes in the relevant places, okay? And when you've done that, unpause the video, and I'll be here when you get back.

Okay, so how do you get on with that? So hopefully you completed those tables, okay? So now let's introduce a new operator called the XOR.

Now the XOR is another logical operator and it stands for Exclusive OR, okay.

It will only output as True if one input is true.

If they're both true, then it will evaluate as False, okay? Now the condition is evaluated as False if both expressions are true.

The condition is evaluated as False if both expressions are false.

The condition is about as True if only one expression is true.

So the difference here clearly between OR and XOR, Exclusive OR, as we talked about before, OR operator, is one or the other but can include both where XOR is exclusive.

So it's one or the other, but not both, okay? So what will be outputs on this truth table? So see if you can pause it and again, complete this truth table, just in your head or on a piece of paper if you'd like to, and we'll go through the answers in just a moment.

Okay, so what did you come up with then? So let's go through the answers then.

So the first one was, if it's false or false, that is false, okay? So expression one is false, expression two, B is false sorry, is definitely false.

Right, the next one then, if expression A is false, but expression B is true, that would be? What do you reckon, shouts out to me True or False.

So three, two, one, it is True, okay? Because this condition is evaluated to True if only one expression is true.

So B is true, A is not, so evaluates as True.

So what do you reckon about the next one? So A is true B is false, therefore, it says, three two one, it is.

True, very well done, I almost heard that, okay.

And then the last one is, A is true, B is true.

And therefore it is, think about it, three, two, one, it is false.

Because in an OR, the condition of OR, that would actually be true, but we're using Exclusive OR so it has to be one or the other, not both, okay? So, unlike AND and OR, Python does not have a built in XOR operator, okay? You would need to create your own function to evaluate XOR, so guess what? That's your next task, okay.

So what I'd like to do is I'd like, head over to task two on your worksheet now, and to create an XOR function, okay.

So there's lots of help on the worksheet for that, but give that a good go, and we'll go through the answers when you're done, okay? So pause the video now, have a go at coding that and when you've got an answer, come back, and we'll go through the answer, good luck.

Okay, so I'm now going to head over to my solution, and you can compare it to yours, so let's have a look.

Right, so there we go.

So this is my function, okay.

So it takes two parameters, just like the other functions that you saw, okay.

So XOR function, and it says, if A equals true, and B equals true, then C equals false.

If A equals false, and B equals false, then C equals false, otherwise, it's true.

And we could have manually coded the other ones, but it's just, there's only one more condition that could have been made so is otherwise.

Or two more conditions True or false.

Okay, one or the other, sorry.

So in otherwise, I just use C equals True, and that returns C, whatever it comes back as.

Now to prove that working, what I've done is, I've put one equals True and two because True.

Now we know if both are true, that should evaluate as False.

So let's run this.

Okay, False, there we go.

So let's make them both false.

Again, you should know that this is going to evaluate as false.

Run that.

There it goes.

Let's make one of them true.

So I'm going to make one true, but two false still, and this should evaluate to true this time.

And it does.

Now let's swap it around, So I'm going to make this true.

Oh, left the F in, okay and get rid of that and make that false, okay.

So this again, should evaluate as True.

Alright, to true perfect, right, and it does.

So that's my working solution.

So again, if you're struggling with that, then pause the video, have a look at my code, and then minimise that for a second and see if you can code it yourself, okay.

So I'm just going to head back over to the slides now.

Okay, so to wrap up this lesson, I'm going to give you another level of a quiz and all you need to do is tell me whether or not it's True or False.

So read that statements on the board.

So one equals four equals equals four, two equals two, is not equals to two.

So remember the exclamation mark equals, that's a not.

So two is not equal to two.

So if I was to run this function, what would come out as? Is that true or false? Remember that we're looking at the XOR here, okay? Therefore, the answer is three, two, one, it is true, okay? Because four does equal four, the two, that's going to evaluate to True.

Two is not equals two, well that'll actually evaluate to False because two does equal two.

So true here false there therefore, because one or the other values is going to equal true.

Let's look at the next one then.

Okay, so let's have a look.

So we've got eight, two times four equals equals 32 times two divide by four.

Two is two equals equals two.

What do you reckon? This is going to be True or False? So slightly tricky one here, what do we think? So see if you can give me an answer if you're ready.

So three, two, one it is? False, okay.

So let's move to the next one then.

Right, next one trickier again here, I guess.

So we got two to the power of three not equal to 16 divided by two.

And then my variable two equals two equals equals three.

So what do you reckon, is this going to equal True or false? So three two one it is.

False, okay, there we go.

And the last one, I think, so four is not equal to four.

So the variable one equals four is not equal to four.

The variable two equal two is equal equal two, so what do you think? Slightly easier.

Okay, so three, two, one, it is.

True, okay, well done.

Oh, there's one more, sorry there's one more, my mistake.

Okay, so last one, okay.

So one variable one equals two to the power three is not equal to 16 divided by two.

And the variable two equals two is not equal to three.

So what do you reckon, True or false? So three, two, one, it was.

True, well done.

Okay, so if you got that, big well done.

And that is the end of that quiz, and the end of the lesson as well, okay.

So I really hope you've enjoyed this lesson, and I'd love to see some of the work you've done.

Tell me what you thought of the operators and I'll also love to see your solutions to that too.

So if you'd like to share the work you've done in this lesson, please do ask your parent or carer to share your work on Instagram, Facebook or Twitter, tagging @OakNational and using the #LearnwithOak.

So next lesson, we're going to be looking at something called structured programming.

So looking forward to seeing you then and I'll see you next time.