Loading...
Hello, I'm Mr. Hogan.
We're going to be having a great time learning all about Python in today's lesson.
I'm so pleased you are going to be learning with me today.
Should we make a start? This is the third lesson from the Introduction to Python Programming Unit, and it's called Using Selection.
By the end of this lesson, you'll be able to write selection statements that make decisions about which path a programme will follow.
This is only your third lesson in this unit, so it will be difficult in some places, but hopefully, we can work together.
Keywords from the lesson today, there's four of them, relational operators, so this is where two values are compared and produces the result of True or False.
Condition, an expression that evaluates to True or False.
Selection, so this is used when there is more than one possible path for a programme to follow.
Logical expression, this is an expression that combines relational and the logical operators.
So the lesson outline for today, it's split into three.
First of all, we're going to use relational operators to compare values.
Then, we're gonna move on to use selection to control the flow of a programme.
And finally, you're going to construct logical expressions that use operators.
So let's start with the first one.
Use relational operators to compare values.
Arithmetic expressions can be used to calculate values.
So, "total = 5 + 10," so total becomes 15.
Relational operators are used to build expressions that compare values.
So the, "top_score variable = total less than 10." Relational operators can be used to compare values within an expression.
So, for example, Andeep here is saying that, "Anyone older than 12 who requires an adult ticket to enter the theme park." So, "adult_ticket is if the variable age is greater-than 12." You can use these relational operators in conditions.
So a double equals symbol is equals to.
An explanation mark, and an equals symbol is not equals to, there's the less-than symbol, there is the less-than or equals to symbols put together.
There's a greater-than symbol, and then there's a greater-than or equals to symbol.
Examples of relational operators are here, so we can say that, "total == 1, does total equal 1?" "Total! = 1, Does total not equal 1? and you can see the rest going down the list, so, "total less than 3, total at most 3," and, "Is total greater than 10?" Or, "Is total at least 10?" Let's have a quick check.
Which one of the following relational operators would you use to check if the value of total is greater than 100? Remember, you can pause the video at any time, and have a think about the answer.
The answer is, A, greater than.
Relational operators evaluate to either True or False.
So we can see on line 2, "adult_ticket = age greater-than 12," where "age is 9".
So, is 9 greater than 12? Well, the answer is no, so it's False.
It's False because it's not greater than 12.
Let's have a look at this one, where, "age is 15", we've still got the same expression on line 2.
So this time, what will it return? That's right, True, 'cause the expression returns True because the age is greater than 12.
Let's have another quick check.
Which of these instructions would set the variable child_ticket to True if age has the value 10? Is it, A, child_ticket = age greater-than or equals to 12, B, child_ticket = age == 12, C, child_ticket = age less-than equals 12? Remember, you can pause the video at any time to have a little think.
Should we have a look at the answer? The answer is C, child_ticket = age less-than or equals to 12.
Let's have a practise.
So task A, 1, Match relational operators to the correct description.
Remember, you can pause the video at any time, to have a think and work out the answers.
2, State whether the following conditions would evaluate to either True or False.
The conditions are there, so we've got 5 greater-than 10.
10 == to 10.
10!= 10, 10 greater-than = to 10, and 12 less-than or equals to 13.
Do you see that I'm not actually using the words to explain the conditions, I'm just explaining the symbols to see if you can remember what they do? Remember, you can stop the video at any time, to have a think about the answer.
Great, you've done really well to get this far, so let's look at these answers.
So the first symbol is greater than, the second symbol is less than, the third symbol is not equals to, the next one is equal to, and the last one is less than or equal to.
For Task A, 2, we're asked to state whether the following conditions would evaluate to either True or False.
5 greater-than 10 is False.
10 == 10 is True.
10 not equals 10 is False.
10 greater or equals to 10 is True.
12 less-than or equals to 13 is True.
Well done.
Let's move on to the second part of the lesson.
Use select selection to control the flow of a programme.
Selection is used when there are more than one possible path for a programme to follow.
It allows programmes to run sequences of code depending on whether its condition evaluates to True or False.
A programme checks the condition and selects the path of action.
So you can see the flow diagram on the right, you've got the condition and you've got two paths, you see, it follows the False path or the True path.
So for example, a ticket programme at a theme park would display a different price depending on the age of the person requesting it.
So if we replace, put the condition into the flow chart and look at the different paths it could take, so the condition is, is age greater-than 12? So if the condition is True, the price = 25, if it's False, price = 18, So it's a child's price, slightly less.
In programming, selection is carried out using "if" statements.
So have a look at this code.
You can see the description in the top code.
So condition, then a block of code, then else, then another block of code, and then in the code at the bottom, we've got, "if age is greater than 12, then the price variable is assigned 25, else: price is assigned 18." So that makes sense, doesn't it? It either takes either path.
If you notice, you'll need an "if" and an "else" when you want code to run on either one of the two possible paths.
Shall we have a quick check? A programme you are creating use selection and has two possible paths, which would you use? A, if, B, if else, or, C forever? Remember, you can pause the video at any time to think about the answer.
The answer is B, if else.
This programme outputs some of the message no matter what name the users enter.
It can be extended to recognise one particular name and treat it differently by using selection.
A condition is needed to check the value of "user" is equals to the string "Alex" so that a different output can be provided.
The relational expression "user == to 'Alex'" is used for the condition in the "if" statement, it will then return and evaluate to True or False.
This is the "if" block of code and it contains the code that will be run if the condition is True.
This is the "else" block of code and contains the code that will run if the condition is False.
Only one of these blocks will be run depending on the result of the condition.
Be careful when you do your "if else" statements and looking at your conditions, there's many pitfalls you can look at and make mistakes in your syntax.
So, for example, "if" and "else" have no capitals, A "Colon" is always required after the "if" and after the "else".
The "==" operator checks that the "user" and "Alex" are the same.
The single "=" symbol is only used in assignments.
The code blocks inside the "if" and "else" conditions should also be indented.
Let's have a quick check.
Which piece of code would correctly check that the variable "name" is equal to "Alex"? Is it A, name == "Alex", is it B, name and then we have open bracket speech marks, "Alex", speech marks, close bracket, or C, name single equal symbol, and then we have "Alex" inside speech marks, inside brackets? Remember, have a little think, you can pause the video at any time.
Let's have a look at the answer.
It's, A, "name == "Alex".
Let's have a practise.
Complete the starter programme to ask a user to guess a lucky number.
So when you open your starter code, you'll see these three lines of code, Line 1 has a variable "lucky" and it's assigned a value "13", so that becomes the lucky number, in effect in the programme.
On Line 2, we've got.
"print ("Guess my number:") And on Line 3, we have a variable "guess", which stores the value that the user enters when they're guessing the number.
We want you to add to this programme, to make sure that when the user guesses the lucky number correctly, the programme should print, ("Amazing, you guessed it") If the user guesses the lucky number incorrectly, the programme should print, (f"Sorry, it's not followed by the variable {guess}), and then print what the lucky number was.
Have a little think about it, you can always pause the video at any time, and hopefully, you can crack the code.
So here we have the first three lines of the starter code, and on line 5, we've got the first "if", so, "if the variable guess equals the variable lucky," then on line 6 we go to, "print(Amazing, you guessed it").
On Line 7, it's the "else", followed by the Line 8 and 9, which prints, (f"Sorry, it's not the variable {guess})" followed by, "print(f"My lucky number is the variable {lucky})".
Well done if you got that right.
Two, extend the lucky guest programme to generate a random lucky number rather than a set value.
Hint: you'll need these two lines of code, "from random import randint", So that just imports the randint function from the random library, and also you need "lucky = randint (1,100).
So that will produce a random number between 1 and 100, and store it in the variable "lucky".
Three, Amend the code to allow the user to specify minimum and maximum values for the random number.
So have a think about it, and hopefully, take your time, you can then pause the video at any time to produce some code.
Shall we have a look for a possible answer to 2? So here on this code, you can just see the lines 1 and 2 has been entered.
So we have imported the randint function from the random library and assigned a variable "lucky" with a random number between 1 and 100.
A possible answer for 3 is slightly more difficult, so bear with me and let's work through this together.
So we have a "print (Enter the minimum number:"), then on line 3, we have a variable "min_value", which is then assigned whatever the user inputs, so the minimum number.
We've got similarly on line 4 and 5, we've got on line 4, "print ("Enter the maximum number:") And then on line 5, we have a variable "max_value", which stores whatever the user inputs as the maximum number.
We then use these variables, "min_value" and "max_value" for the randint function.
So instead of saying 1 for the minimum number, and 100 for the maximum number, we use the variables, and what the user has inputted for those variables, "min_value" and "max_value".
I hope you got that right, well done if you did.
Brilliant, you're doing really, really well.
It can be very complicated, these Python lessons, but we're moving on to the third part of the lesson, Construct logical expressions that use operators.
Relational operators can be used to compare to values within a condition.
Logical operators can be used alongside relational operators to build logical expressions when more values need to be compared.
Before, we had, "Is the adult ticket greater than 12?" Which evaluates to True or False.
What about this, though? If we have, "adult_ticket = age greater-than 12 and age less-than 60".
So we're trying to say the adult ticket now, is only between the age of 12, and less than 60.
Logical operators are used when you need to compare multiple values with relational operators to return a single value of True or False.
So we've got "and", "or", "not".
"and" is True when both relational operators are True, "or" is True when either or both relational operators are True, "not" is True when the relational operator is False, so not True.
Let's have a quick check.
Match the logical operator to the correct description.
We've got "and", "or", "not", and then we've got the description of True when the relational operators False.
True when both relational operators are True, and True when either or both the relational operators are True.
Remember, you can pause the video at any time, and have a little think about your answer.
Okay, let's have a look at the answer.
So, "and" is True when both relational operators are True, "or" is True when either or both relational operators are True, and "not" is True when the relational operator is False, not True.
Combining logical operators and relational operators mean, that you can build lots of logical expressions.
So for example, we can say is, "x ==1 and y == 1".
Or, we can say, "x == 1 or y == 1".
We can say, "not x == 1." Let's have a quick check.
Which of the following logical operators would you use to check if both x and y are greater than 3? Is it A, "not", B, "or", or C, "and"? There's some code, though, as well, just to perhaps, replace x with 5, and y with 4 to help you.
Remember, you can pause the video at anytime, to have a think about the answer.
Okay, let's have a look at the answer, it's, "and".
We can start to build logical expressions.
"The kettle is on when the button is pressed on and the temperature is less than 100 boiling point." So we can say, build this logical expression to be True, when we say, "button == "on" and temperature is less than 100, then kettle_on." It is True.
"Children under 12 or seniors over 60 are eligible for discounted tickets at this theme park we keep referring to.
So we could build this logical expression.
This "discounted_ticket is True if it equals ages less-than 12 or ages greater-than 60." Let's have a quick practise.
One, construct a logical expression for a fire sprinkler system that only turns on if the alarm is on and the temperature is greater than 120 degrees Celsius.
Two, assuming that each "print() statement is output on a new line, what would be the output from the following programme? So the programme, we have two variables, "number" and "city", and then we've got some expressions that we can read and go through.
Remember, you can stop the video anytime, to think about your answer.
Create a Python programme to display the entrance ticket for a theme part that follows this algorithm.
Ask the user to enter the age, if the user is less than 12 or more than 60, the ticket price is set to 18.
If the user is between the ages of 12 and 60, the ticket price is set to 25.
Display the correct ticket price to the user.
Remember, you can pause the video at any time, to think about your answer.
Let's have a look at the answers.
So, 1, asks you to construct a logical expression for a fire sprinkler system that only turns on if the alarm is on and the temperature is greater than 120 degrees Celsius.
So the, "sprinkler_on = temperature is greater-than 120 and alarm is == to "on"".
So evaluate to True, sprinkler "on".
Two, assuming that each print statement is output on a new line, what would be the output from the following programme? So it would be False from line 4, True from line 5, and True from line 6.
We then asked you to create a Python programme, and we have possible answer here, so let's talk through the code.
So, lines 1 is asking the user for their age, and then the input from the user is then assigned in the age variable, to the age variable.
And then we've got an "if: statement, an "if", "else:" statement, so we've got on line 4, "if age is less-than 12 or age is greater-than 60: price = 18, and then else: price = 25," the two puffs that are still.
And then on line 9, we've got "print(price)".
Fantastic, we've reached the end of this lesson.
I hope you've enjoyed it, it's been hard along the way, but I think we've learned a huge amount of things.
So in summary, we've learned that relational operators are used to compare values, and evaluate to True or False.
Selection is used when there is more than one path to follow in a programme, and logical expressions can be built using a combination of relational and logical operators.
Well done, for reaching the end of the lesson, I really hope you've enjoyed it!.