video

Lesson video

In progress...

Loading...

Hello.

My name is Mrs. Holbrook, and welcome to computing.

I'm so pleased that you've decided to join me for the lesson today.

We're going to be looking at how we can use our knowledge of Boolean logic to incorporate that into some Python programmes.

Don't worry if you haven't done much Python programming before.

We're going to go through it step by step.

Welcome to today's lesson from the unit Boolean Logic.

This lesson is called Implementing Boolean Logic in Python, and by the end of today's lesson, you'll be able to use Boolean logic in a Python programme.

Shall we make a start? We will be using these keywords during today's lesson.

Boolean operator.

Boolean operator.

An operator used in a Boolean expression, for example, AND, OR, and NOT.

Logical operator.

Logical operator.

Another name for a Boolean operator.

Programme flow.

Programme flow.

The order in which instructions in a programme are executed.

Today's lesson has two parts.

We'll start by describing how Boolean logic controls programme flow, and then we'll move on to use Boolean logic in a Python programme.

Let's make a start by describing how Boolean logic controls programme flow.

The three fundamental Boolean operators are AND, OR, and NOT.

In Python, these are written in words as and, or, and not, and notice they're written as lowercase.

These are sometimes also referred to or called logical operators.

Just like logic gates, Boolean operators require their inputs to be either true or false.

Python also has other types of operators.

It uses arithmetic operators like addition, subtraction, multiply or divide, or comparison operators like more than, more than or equal to, or less than.

Arithmetic operators evaluate to a number.

For example, five plus two will evaluate to seven.

Comparison operators create statements that are either true or false.

For example, is a greater than b? Time to check your understanding.

Which of these statements would be suitable to use with a Boolean operator? Is it A, 17 divided by 2, B, total % 3, or C, A not equal to B? Pause the video here whilst you have a think.

Did you select C? Well done.

C is a statement that evaluates to either true or false, depending off the values of a and b.

Both A and B are statements that would evaluate to a number instead of true or false.

Python programmers use selection to make a decision.

The programme tests the condition to check which instruction to execute next.

And a condition, remember, evaluates to true or false.

The order in which instructions are executed is called programme flow.

This code here contains a selection statement, so let's have a look at it carefully.

We start with a printed message to the user which says, "Enter a number." On line two, we take that input in as an integer from the user and we store it in the variable number.

On line three, we have a selection statement which says if number is greater than 0 and number is less than or equal to 5, if that returns as true, it's going to print Valid.

If it's not true, it's going to print Invalid.

The programme flow will start at line one, with each line being executed in turn until the condition on line three is met.

If we look more closely at the condition, we can see there's three parts to it, and I've labelled them as A, B and C.

So A uses a Boolean operator, in this case the and.

B and C both use comparison operators.

Each part of the condition is either true or false, depending of the value of the variable num at the time that the programme is run.

Let's suppose the user runs the programme and types in 2.

What do you think may happen? The value of num is substituted in, so num becomes equal to 2.

And then if we looked at the selection statement, we'd basically be saying, if 2 is greater than 0 and is 2 less than or equal to 5.

The condition is evaluated using the actual values held by the variable, and we can see that all of those conditions return true.

The condition is true, so the programme prints Valid.

The else only happens if the condition is false, so this is ignored and the programme continues.

Time to check your understanding.

What sequence of line numbers would this programme run in if the condition on line three were false? In other words, what would the programme flow be? Pause the video here whilst you look carefully at the code and think about your answer.

The sequence of line numbers that this programme would run in would be 1, 2, 3, 5, and 6.

Because the condition is false, it's going to run the else statement and the code that is contained within that else.

Okay, we're now moving on to the first set of tasks for today's lesson, and you're doing a great job so far, so well done.

I'd like you to identify the operators in each code snippet and write them in the correct column.

So are they a Boolean operator or are they a comparison operator? Pause the video here whilst you complete the activity.

For part two, I'd like you to describe how this line of Python code uses Boolean logic to control the programme flow.

So the line of code reads, while not finished:.

Pause the video here whilst you complete the activity.

How did you get on? Did you manage to identify the operators? Great work.

Let's have a look at the sample answers together.

So the first code snippet was if age is greater than equal to 18 and member is equal to true.

The Boolean operator in this code snippet is and and there's two comparison operators.

There's greater than or equal to, and then there's the double equals, which is the comparison operator.

Next code snippet was while not finished:, so this one only contains a Boolean operator and that's the not operator.

And then the last one was while a is greater than 10 or b is less than 10.

The Boolean operator here is or.

And again, we've got two comparisons, the greater than and the less than.

You were then asked to describe how this line of Python code uses Boolean logic to control programme flow.

Let's have a look at a sample answer together.

This statement uses the Boolean operator not.

The condition not finished evaluates to either true or false, depending on the value of the variable finished.

The code inside the loop will be repeatedly executed until the variable finished becomes true.

Remember, if you need to add any extra detail to your answer or make any corrections, you can pause the video now.

You're doing a fantastic job so far, so well done.

We're now moving on to the second part of today's lesson, where we're going to use Boolean logic in a Python programme.

Almost all Python programmes use Boolean logic to make decisions about programme flow.

Aisha says, "In a video game, you might need a trophy or a certain number of coins to purchase an item from a shop." Have you ever played any games with some similar scenarios? Time to check your understanding.

Which of these programme scenarios does not use Boolean logic? A, checking whether a new password is between five and 10 characters long, B, selecting an opponent in an online game who has the same rank and is not banned, or C, adding up the total cost of items in an online shopping cart? Pause the video whilst you have a think.

Did you select C? Well done.

Adding up the total costs of items in an online shopping cart does not use Boolean logic.

Have you ever played the game rock, paper, scissors? In the game, a player uses their hand to represent the choice of item.

So scissors, paper, and rock.

Each item beats one of the items and loses to one of the items. So scissors beat paper, paper beats rock, and rock beats scissors.

Calculating the output of the game requires Boolean logic to evaluate a condition.

Jun says, "If I chose scissors and you chose rock," Andeep says, "Then I win!" Time for another check.

Which code represents the logic statement "I chose scissors and you chose rock"? Is it A, B, or C? Look carefully at the code and have a think.

That's right.

The correct answer is C.

If player1 is equal to scissors and player2 is equal to rock.

Remember, in Python, the double equals operator is used for comparison.

We're now moving on to the final set of tasks for today's lesson, and we're going to do some programming activities.

So I'd like you to open the starter project at oak.

link/rock-paper-scissors.

Your task is to complete the programme to implement the rock, paper, scissors game.

So you've been given some starter code, which is displayed on the screen here.

The orange comments are just prompts for where you should add your code.

The game should A, ask the player for their choice, which should be r, p, or s.

It should then use Boolean logic to compare the computer's choice to the player's choice and output whether the player has won, lost, or drawn.

Pause the video here whilst you complete the activity.

How did you get on with your programme? Did you manage to get it to work correctly? Great job.

There are many ways that you could write this code.

So this is not the only correct solution.

If you want to see the solution in more detail, you can go to oak.

link/rock-paper-scissors-solution.

So the first thing you were asked to do was to ask the player for their choice.

So on line six, I've added the code, player1 = input "Enter rock (r), paper (p) or scissors (s):" This is a print and input statement that takes in the user's input and stores it as the variable player1.

We then had to use Boolean logic to compare the choices.

So on line 13, I have an if statement which says if player1 is equal to player2, so they've both picked the same, print Draw.

We then need some code to tell player1 if they have won the game.

So on line 17, I've started my elif, or else if statement.

On line 18, it says if player1 is equal to r, which stands for rock, and player2 is equal to s, which stands for scissors, or, on line 19, player1 is equal to p and player2 is equal to r, or, line 20, player1 is equal to s and player2 is equal to p.

All of those conditions are gonna result in player1 winning, so we then print on line 22, You win.

And then finally, we've got the else, which is the only other results.

So we've got draw, win, the other option is lose.

So we are just going to print You lose.

Remember, if you want to pause your video here to go and have a look at the solution in more detail or go to amend your code, you can do that now.

We have come to the end of today's lesson, and you've done a fantastic job.

So well done.

The Boolean operators in Python are the same as the fundamental logic gates and, or, and not.

A Python programme uses conditions to decide which code statements to execute next.

In other words, it uses conditions to decide the programme flow.

Conditions can contain Boolean operators and evaluate to either true or false.

I hope you enjoyed today's lesson, and I'll see you Again soon.

Bye.