© Distribution of this video is restricted by its owner
00:04 | Welcome to the lecture of the Today's topic is selection condition. Als |
|
|
00:14 | key words here are gonna be bullion um And if then else statements very |
|
|
00:23 | part of python and any other programming first let's understand what our boolean expressions |
|
|
00:34 | And before that, what's a boolean ? Bullion is a variable type that |
|
|
00:40 | only store two things true or It's a special kind of variable that |
|
|
00:48 | take only these two values. And it's extremely important. So now let's |
|
|
00:54 | familiar with what kind of things does boolean expression or the boolean variable |
|
|
01:02 | So the first example we have here four less than three. And the |
|
|
01:08 | is is it true or is it ? Clearly four can never be less |
|
|
01:14 | three. So it's a simple case a false treatment. So it's the |
|
|
01:20 | of the expression is false, it than three. That's always gonna be |
|
|
01:27 | . So that is true. Let's going. What about five equals equals |
|
|
01:36 | . Notice the double equal here we used the single equal for assignment in |
|
|
01:43 | . So because so because of that cannot use the single equal for saying |
|
|
01:48 | something is equal or not. So uses a day double equal to signify |
|
|
01:53 | as an operator essentially saying five is in value to four. And of |
|
|
02:02 | that is not true, which means it's not true it's false. So |
|
|
02:08 | expression is also false, couple more equality and inequality. Look at five |
|
|
02:18 | equal to four. So that is legal operator that looks like this. |
|
|
02:24 | not equal to. So of course is not equal to four. They're |
|
|
02:29 | . So that means this is actually . And what about five equals equals |
|
|
02:38 | . That of course is true to five equals 25 is a simple true |
|
|
02:46 | . Few more examples what? About ? Greater than equal to three. |
|
|
02:52 | clearly it is not equal to but clearly it is greater than |
|
|
02:58 | So when you say greater than equal three, that means one of the |
|
|
03:01 | is true in this case the greater is true. So that translates to |
|
|
03:07 | three greater than three is not They're equal not not greater than so |
|
|
03:13 | is false and three greater than equal three is actually true. Three greater |
|
|
03:20 | three schools because three equals to But when you have this other option |
|
|
03:25 | that translates to true. So you reflect on these here is the same |
|
|
03:33 | with the answers for reference. And you're ever not sure whether a particular |
|
|
03:41 | expression is true or false, you simply write this program says give the |
|
|
03:46 | of the boolean variable equals to the expressions assigned to the boolean expression like |
|
|
03:52 | less than three. And then just it. So you'll find that it'll |
|
|
03:56 | true or it'll print falls and you verify, validate what you're trying to |
|
|
04:02 | our show. So summarizing the operators looked at so far, there's less |
|
|
04:12 | greater than equals, less than or to greater than or equals or not |
|
|
04:18 | . That's about all As far as operators. For for in python are |
|
|
04:25 | , that's all we need to learn . So those are relational operators. |
|
|
04:33 | we're gonna talk about another type of called the logical operator, which is |
|
|
04:40 | extremely important for bullion arithmetic, boolean . So, so we're gonna go |
|
|
04:51 | these three and or not one at time. Let's look at this expression |
|
|
04:56 | is greater than three over here. four is less than three. Is |
|
|
05:02 | true or false? Four is less three is false. We already saw |
|
|
05:09 | eight greater than three. That is . So now the question is what |
|
|
05:16 | the whole expression? Four is less three and eight is greater than |
|
|
05:24 | That is not true. Because when have something and something, both sides |
|
|
05:29 | to be true for the whole thing be true. Since one side is |
|
|
05:34 | , it doesn't matter that the other is true. If either side was |
|
|
05:38 | , the final answer will be Independent of what the other side of |
|
|
05:43 | and evaluates to. So this is . Let's see this one. It's |
|
|
05:49 | the same expression except that we have the end with or here. |
|
|
05:56 | here is the difference. Now So we still have left hand side |
|
|
06:01 | less than threes falls right inside it than three is true, but this |
|
|
06:05 | the operator is not and it's all or something, if either side is |
|
|
06:12 | , then the whole expression is So in this case the answer is |
|
|
06:23 | . So we've gone, we've touched end, we've touched on or let's |
|
|
06:29 | or not, what does it mean have? Not for less than |
|
|
06:34 | So we know that four lesson three false, so clearly not for less |
|
|
06:41 | three is the opposite of um um for less than three computes to, |
|
|
06:47 | that computes to fall not of that going to be true, so this |
|
|
06:52 | true, basically not is essentially if of one is zero, not of |
|
|
07:00 | is one, not a true is , not false is true. So |
|
|
07:04 | notice will often use zero and one represent false and true. So that's |
|
|
07:12 | fairly commonly used. So it's one the same thing to refer to a |
|
|
07:17 | value is true or one and it's same thing to refer to it as |
|
|
07:22 | or zero. Some more um examples already saw not of a variable is |
|
|
07:32 | the opposite of the whatever the expression , 1 to 0 and 0 to |
|
|
07:38 | . So now let's think for a about not X is greater than |
|
|
07:44 | See if you can try to express in um in a way that does |
|
|
07:51 | use the not so to do let's just think about it, What |
|
|
07:57 | it mean to say X is greater why is not true. Okay, |
|
|
08:03 | if X is greater than why is true then what's true? Maybe X |
|
|
08:10 | less than y, maybe x is to y. So that's what it's |
|
|
08:14 | . If something is not greater, can only be one of those other |
|
|
08:19 | . So not X is greater than is same as saying X is less |
|
|
08:26 | equal to y. Okay, so good to understand that these are very |
|
|
08:31 | , not identical in terms of their excess opposite of X is greater than |
|
|
08:38 | . The north of X is greater Y and X less than equal to |
|
|
08:45 | . Let's try something slightly more complex to try to understand these expressions with |
|
|
08:54 | logical operators. Here's an expression that not br one and BR. |
|
|
09:02 | So think about this for a Um The when will this be true |
|
|
09:11 | when will this be false? The time the expression inside here will be |
|
|
09:20 | is if the war is won and two is also one, that's the |
|
|
09:24 | time something and something is true and when the north of it is |
|
|
09:29 | If each, either one of these false then either beaver, one or |
|
|
09:36 | too is false. Then this whole is going to be false and the |
|
|
09:42 | of it is going to be So you may have to think this |
|
|
09:46 | yourself, but you will find that is equal and to not P War |
|
|
09:56 | Gonna shorten it or not. BYR . Okay, they represent the same |
|
|
10:07 | . So the way to make sure you've got this right, but this |
|
|
10:15 | equal to this is using something that sometimes referred to as the truth |
|
|
10:22 | which is like this. So you these two variables be war one and |
|
|
10:30 | war too. Now, what are possible combinations of values of B War |
|
|
10:38 | ? And be war to be War could be zero or two? Could |
|
|
10:42 | zero B. War one could be . B War two could B |
|
|
10:47 | B war one could be one or could B zero and B were |
|
|
10:51 | And the war too could be So this is all the combination. |
|
|
10:57 | are uh these two variables. Now have to convince yourself that in every |
|
|
11:04 | um uh the the computation, if put B11 and you are to whatever |
|
|
11:13 | expression computes is also the same as expression. We're not gonna go |
|
|
11:19 | you can try make yourself that is . Any time you have to boolean |
|
|
11:23 | look at all combinations of values and can verify if they always compute to |
|
|
11:30 | same, that is if one computer others to 01 computes to one other |
|
|
11:36 | one. And if that's true for these four cases, these are the |
|
|
11:39 | four cases, then both boolean expressions equivalent. So here is the uh |
|
|
11:54 | same thing we discussed for reference, move on. So now another thing |
|
|
12:02 | , we had talked about presidents earlier this course, in terms of arithmetic |
|
|
12:11 | , um what is if its multiplication addition? Multiplication comes first and we're |
|
|
12:18 | going to go over it again, there's an order where multiplication and division |
|
|
12:23 | higher up, exponentially ation is even and then lower down is plus and |
|
|
12:29 | . You do those and those Now we've seen these new boolean operators |
|
|
12:37 | logical operators, relational and logical relational are like these. Less than |
|
|
12:44 | than and logical are these. Now you've got additions, subtractions, multiplication |
|
|
12:52 | all these? Um there is still an order. So the question is |
|
|
12:58 | do you do with an expression like ? How do you go about where |
|
|
13:04 | you go about starting evaluating something like ? So the order is first you |
|
|
13:12 | and do all the arithmetic and of within arithmetic, certain things that before |
|
|
13:17 | . We're gonna skip that for We've covered it in other contexts. |
|
|
13:21 | you're done with all the arithmetic then do the relational operators. That is |
|
|
13:26 | than greater than and after that next not nexus and nexus are so let's |
|
|
13:34 | through an example here is the same and we have to evaluate it. |
|
|
13:43 | let's see. First thing we have do is arithmetic. So let's look |
|
|
13:48 | arithmetic operators here. There is one here multiplication. One over here. |
|
|
13:57 | edition and one over here. That's edition. Right. So we will |
|
|
14:06 | do those operators first, technically will the multiplication and then the additions and |
|
|
14:13 | don't see any other arithmetic operator. we're going to try to express that |
|
|
14:22 | are done first by using our old of parenthesis, which forces an |
|
|
14:28 | So this is what it comes down saying that these three things in represent |
|
|
14:36 | should be done first. Now we're with this part. What about |
|
|
14:42 | What are the relational operators? You ? There is this one. Let's |
|
|
14:50 | . And there is this one. others? Yes, there is this |
|
|
14:59 | . So we need to now evaluate and the way we're going to specify |
|
|
15:08 | they're evaluated next is use the next of parenthesis. So basically these parenthesis |
|
|
15:20 | saying that you do this one These are saying you do this one |
|
|
15:25 | and these are specifying the equals so we skipped. No we don't |
|
|
15:33 | So next is not We're done with . Next is not I don't see |
|
|
15:39 | not. So we don't have to about it. Next is end. |
|
|
15:45 | we see one end and it's being across here. So we would specify |
|
|
15:57 | that gets done next by using another of parenthesis and finally we're done with |
|
|
16:03 | so this is the final expression. . So just try to work this |
|
|
16:11 | yourself to get a good feel for this ordering works and these are the |
|
|
16:24 | . Okay, now let's move to more fun topics in case you're wondering |
|
|
16:31 | are we suddenly interested in these boolean and logical operators and whatnot? The |
|
|
16:40 | reason is what's called conditional execution. a program behaves often behaves one way |
|
|
16:52 | a certain condition is true and a way if that condition is false. |
|
|
16:58 | , something like this. If the is more than 7500 C on |
|
|
17:05 | there's nothing to do if the light green, you go if it's any |
|
|
17:12 | color you stop. So you do thing, if it's green, you |
|
|
17:16 | something else. So that's another example conditional execution. Um, this concept |
|
|
17:23 | referred to as conditional execution selection. statements, uh, basically meaning the |
|
|
17:31 | idea and how you express it in or any other programming languages is takes |
|
|
17:40 | several slightly different variant forms and that's we'll go over in the next few |
|
|
17:48 | . The most basic of uh, conditional statements is binary selection. There's |
|
|
17:55 | if there's a condition if the condition true when you say if condition and |
|
|
18:01 | that means that the condition is then you do this part. If |
|
|
18:06 | not true, you do this So an example would be you have |
|
|
18:11 | number if the number is less than , you go ahead and print |
|
|
18:16 | If this condition is not true, not true. That means that the |
|
|
18:23 | is greater than or equal to In that case we jump to the |
|
|
18:28 | and print something is non negative. this is fairly straightforward and clear. |
|
|
18:37 | terms of the flow of execution within program, the if statements are, |
|
|
18:46 | know, have their own pattern as would expect so far in python everything |
|
|
18:52 | have seen go straight from top to with the exception that when there's a |
|
|
18:58 | statement, you go over the same again and again. So you go |
|
|
19:03 | or sometimes you get in a So this presence the if statement presents |
|
|
19:09 | way of uh of executing which is the program comes to this condition here |
|
|
19:18 | the condition here is the condition at point. A decision has to be |
|
|
19:23 | . Is this condition true? Or it false? If it's true, |
|
|
19:28 | go one way, execute whatever it , if it's false, you go |
|
|
19:35 | other way and execute those statements then continue execution. You're not done with |
|
|
19:41 | program. You're done with the if you continue execution with what our statement |
|
|
19:48 | after the FNL. So here it had statements three. After the if |
|
|
19:57 | you would be jumping to statements So whether the condition is true when |
|
|
20:01 | went statements one or with is false you went through statements to after that |
|
|
20:07 | end up at the statements three right the if block. So that is |
|
|
20:14 | most basic binary selection. If statements you have unitary selection unit selection is |
|
|
20:22 | if certain condition is true then I to do something. If it's not |
|
|
20:29 | , I don't need to do I just move on to the next |
|
|
20:32 | , I just skip that step. the condition is not true. An |
|
|
20:36 | here is the no number. You a variable card number. If number |
|
|
20:40 | less than zero, you print that data is invalid but once you printed |
|
|
20:47 | you go to the next statement and keep going no matter what here that |
|
|
20:51 | whether if the number was less than , your print invalid data and then |
|
|
20:57 | keep going no matter what. If is less than zero was not |
|
|
21:01 | the number is say greater than or to zero then you will not do |
|
|
21:06 | print invalid data but still come to next step. Right? So there |
|
|
21:12 | no else, there's only one side recognition variable, you do it or |
|
|
21:17 | don't do anything. So in terms the flow of execution it looks like |
|
|
21:23 | . You go through to the If the condition evaluates to falls you |
|
|
21:28 | skip the whole lift block and keep . If the condition evaluates to true |
|
|
21:34 | you go execute whatever is a bunch statements in the if block and um |
|
|
21:41 | then at that point, once you're you go back and continue again. |
|
|
21:46 | there were statements three here we're talking statements three here. In either case |
|
|
21:54 | go to the statements right after the in one case you do nothing go |
|
|
21:59 | . In other case you execute the statements inside the if block more |
|
|
22:09 | So nested, conditional nested if canals very common quite often you don't have |
|
|
22:17 | one condition but a sequence of conditions something to be done or not |
|
|
22:22 | So. So here is a simple you say that if number is less |
|
|
22:28 | zero then you print negative and in example you're done there's nothing more to |
|
|
22:35 | if number is less than zero so um execution will continue, you |
|
|
22:43 | print negative and come here but if is false, that means number is |
|
|
22:50 | than equal to zero, then you to the L spot The condition is |
|
|
22:56 | true. So you have to go the allspark and now you know that |
|
|
23:00 | greater than equal to zero. The of it is if the number now |
|
|
23:04 | greater than 100, you print large it's uh if this is not true |
|
|
23:11 | ? So that means the number is zero and 100 then you bring small |
|
|
23:17 | then after that you go back to step after the if block. |
|
|
23:22 | so there are two conditions, first is that thing less than zero negative |
|
|
23:27 | ? If it's not less than Next question is it greater than |
|
|
23:31 | Print large? It's not greater than prints small. And you're done. |
|
|
23:36 | in terms of the flow chart, is what it looks like you |
|
|
23:45 | The First test here is the number than zero. There are two |
|
|
23:54 | One is, its true number is -43. You go here, you |
|
|
23:59 | negative and you're done. You just keep going. The other possibility is |
|
|
24:05 | is false because the number is zero it's greater than zero. Then you |
|
|
24:11 | this way and then you asked another is the number greater than 100. |
|
|
24:17 | it is, then you go here large, you grow here print |
|
|
24:23 | And then the execution goes after after whole if block. Okay, so |
|
|
24:30 | slightly more complex, but it should fairly included. So now a few |
|
|
24:43 | nitty gritty ease of these panels, think the big ideas are already |
|
|
24:49 | So, and this uh the concept covering here is quite important for real |
|
|
24:59 | programming. How do you know which matches which else? So remember and |
|
|
25:08 | I can have an else or not an else and within that if they |
|
|
25:13 | again be and if and another So look at this example. Now |
|
|
25:19 | issue is we've got two ifs and got to else's now, which one |
|
|
25:26 | which one is? Sometimes it's it's so clear. Uh And the answer |
|
|
25:36 | like many things in python, look indentation. So these two are invented |
|
|
25:45 | the same level. These two are at the same level. So this |
|
|
25:51 | is inside the inside the top if when you when, when this statement |
|
|
26:01 | not true, you jump here because the matching else. If this statement |
|
|
26:06 | this, this statement is not true this expression evaluates to false, then |
|
|
26:12 | jump to the else here. So a simple way of matching if and |
|
|
26:18 | . And it does work even when are more levels of indentation or more |
|
|
26:24 | of traditional in a program. So more thing here, this is the |
|
|
26:35 | example are a very similar example. have an ifor number and then |
|
|
26:39 | the second part is if the first is false, then you have an |
|
|
26:46 | statement here. And within the else is there is another if, so |
|
|
26:54 | is a shortcut of saying if something , then followed by another if. |
|
|
27:03 | , so first statement is not And then we're gonna test for the |
|
|
27:08 | statement or rather statement, I mean second boolean expression. So this is |
|
|
27:17 | thinking or logical flaw that we're talking . An alternative way to say it |
|
|
27:24 | like this, what we did was we essentially combined this if then else |
|
|
27:34 | into an else if, statement. basically these get combined. So you |
|
|
27:40 | something like this. So you have else if and then else. So |
|
|
27:45 | is just to say that what we down here. But else f is |
|
|
27:51 | a shortcut for the um But the on top. All right. |
|
|
28:09 | yeah, as if just replaces an followed by an if now look at |
|
|
28:15 | statement here or this program here, it starts off by saying if number |
|
|
28:21 | less than zero, you print error top then if the number is less |
|
|
28:29 | 100. So in the second this would mean that the the the |
|
|
28:37 | just kept it means here that number less than zero is not true. |
|
|
28:47 | are at this else if okay, is less than zero is not |
|
|
28:51 | That means it's greater than equal to , is it less than 100? |
|
|
28:56 | if if we reach here, then means that it's between, you |
|
|
29:01 | one or zero and 100. Then just print this and you have another |
|
|
29:08 | . If that is not true, uh is the number less than |
|
|
29:14 | So, if you've gone past if this expression is not true, |
|
|
29:19 | means the number is between 200 So then you sorry, then you |
|
|
29:26 | at this one and if that is , it's less than 200. You |
|
|
29:30 | this if it's less than 300 you this less than 400. Uh 500 |
|
|
29:36 | so on. And then finally if of these is true you finally print |
|
|
29:42 | the number is over 500 and you're . It's important to see this is |
|
|
29:46 | change conditions one if else if else else if but in this whole flow |
|
|
29:54 | one branch is executed. So if example if you were to Um execute |
|
|
30:02 | know something that prints 100 then uh or 200 then the remaining thing would |
|
|
30:12 | uh go will finish execution and you'll done. So yeah so the point |
|
|
30:30 | just that just look at it Anytime you print make one of these |
|
|
30:34 | statements you're done and you go past the whole if then else if else |
|
|
30:46 | . Um Just one mini topic That is brilliant functions We've recently learned |
|
|
30:55 | functions and so far we saw functions return values like uh integers or floats |
|
|
31:06 | strings or nothing at all. Now now we've been introduced to a new |
|
|
31:11 | type called boolean. And of course can return a boolean also. So |
|
|
31:18 | example is here an example is here there is a function and uh it |
|
|
31:30 | as a parameter. This variable value then within the function we just have |
|
|
31:37 | value is greater than zero then it true. If it's not greater than |
|
|
31:44 | then it returns false. So nothing . Except that functions just as they |
|
|
31:51 | return any other data types, they return boolean to that. There's nothing |
|
|
31:56 | special about a boolean function. So up a boolean expression is something that |
|
|
32:03 | evaluates to true or false. And is constructed with relational operators. Um |
|
|
32:12 | less than less than equal equals, equals and and such. The uh |
|
|
32:20 | are the relational operators and then and and not those are the logical operators |
|
|
32:26 | combine these boolean values. And a big reason for doing all of |
|
|
32:32 | is conditional execution. That uh that in the form of if else statements |
|
|
32:40 | we've seen a number of ways those be organized. And the final point |
|
|
32:44 | cover is that functions can return a value Also, just like just like |
|
|
32:51 | other data type. And that's the of this video lecture. Thank |
|