Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,480 --> 00:00:03,360
Let's talk about conditional logic.
2
00:00:03,360 --> 00:00:04,710
An important concept.
3
00:00:04,710 --> 00:00:06,200
All over programming.
4
00:00:06,270 --> 00:00:09,420
What do I mean by conditional logic.
5
00:00:09,420 --> 00:00:11,600
We've learned about bullying.
6
00:00:11,640 --> 00:00:11,970
Right.
7
00:00:12,000 --> 00:00:19,950
We have true and we have false and some of you might be wondering why they're so useful.
8
00:00:19,950 --> 00:00:25,300
And when it comes to conditional logic billions are really really important.
9
00:00:25,300 --> 00:00:35,280
For example let's say we create a variable is called and you know what we're going to create a car that
10
00:00:35,370 --> 00:00:38,760
automatically detects if you can start the engine.
11
00:00:38,880 --> 00:00:44,220
Maybe if you're not old enough the engine won't start and it won't let you drive.
12
00:00:44,340 --> 00:00:50,940
So that they make sure that this car is super safe and only people that are perhaps over 18 can drive
13
00:00:50,940 --> 00:00:51,780
it.
14
00:00:51,810 --> 00:00:54,060
So how can we code something like that.
15
00:00:54,120 --> 00:01:00,490
Let's assume Tesla has a new feature like the well we'll have something like is old and we'll set this
16
00:01:00,490 --> 00:01:06,800
to true right maybe we have something else.
17
00:01:07,460 --> 00:01:15,680
Maybe it checks a license to make sure that we have a license so we can say is license and then we can
18
00:01:15,680 --> 00:01:17,510
set it to true.
19
00:01:17,690 --> 00:01:23,020
Now these could change true and false depending on the user and the driver.
20
00:01:23,150 --> 00:01:28,160
But when I talk about conditionals This is what I mean in Python.
21
00:01:28,190 --> 00:01:37,670
We can use the if keyword to say if some condition exists and this is just me writing it it doesn't
22
00:01:37,670 --> 00:01:38,180
exist.
23
00:01:38,180 --> 00:01:42,250
You can see that I get a read on underline with invalid syntax.
24
00:01:42,380 --> 00:01:47,720
But here we want to say a condition that evaluates to true or false.
25
00:01:47,840 --> 00:01:58,340
In our case we can say hey if is old witch what does it evaluate to well evaluates to true if that's
26
00:01:58,340 --> 00:02:06,530
true then let's say print you are old enough to drive.
27
00:02:06,660 --> 00:02:08,440
Now there's a few new things here.
28
00:02:08,530 --> 00:02:16,930
One I've added a semicolon or a call in and then you'll see here that the indentation is different.
29
00:02:17,140 --> 00:02:27,310
As a matter of fact when I press enter after the colon you see that it automatically gives me a space.
30
00:02:27,310 --> 00:02:34,230
This tells the Python interpreter I'm going to do an if statement a conditional operation.
31
00:02:34,630 --> 00:02:43,980
And if this happens to be true run everything that's inside of here that has this indentation and if
32
00:02:43,980 --> 00:02:54,460
I do something without the indentation let's say print check check this to the Python interpreter is
33
00:02:54,460 --> 00:02:56,780
like a completely new line.
34
00:02:56,860 --> 00:03:00,580
It is not part of this F block.
35
00:03:00,580 --> 00:03:03,960
And if you're using the rep you see over here I get a little minus sign.
36
00:03:04,180 --> 00:03:05,440
I click this.
37
00:03:05,440 --> 00:03:13,240
You see how it hides Well what it does is it's saying hey this is a code block over here.
38
00:03:13,330 --> 00:03:22,420
It's an entire thing in itself but anything outside that is not indented well doesn't belong to it.
39
00:03:22,420 --> 00:03:29,020
So to a Python interpreter it's going to say I have this line and then I have this line.
40
00:03:29,170 --> 00:03:30,530
Let me show you what I mean.
41
00:03:30,700 --> 00:03:40,230
If I run this program I get you are old enough to drive and then I also get check check but what if
42
00:03:40,230 --> 00:03:43,180
I say is old is now false.
43
00:03:43,500 --> 00:03:48,370
What do you think will happen if I run this I get.
44
00:03:48,510 --> 00:03:54,010
Check check our Python interpreter is going to say hey.
45
00:03:54,280 --> 00:04:00,840
Set Variable is old to false Hey set is licensed equals too true.
46
00:04:01,150 --> 00:04:02,710
And then it's going to go to line three.
47
00:04:02,740 --> 00:04:03,490
No nothing there.
48
00:04:03,490 --> 00:04:04,510
I'm going to keep going.
49
00:04:04,510 --> 00:04:05,240
Line four.
50
00:04:05,260 --> 00:04:07,510
Hey if is old.
51
00:04:07,600 --> 00:04:08,990
Is this true.
52
00:04:09,040 --> 00:04:09,510
No.
53
00:04:09,550 --> 00:04:17,080
If all this false and the interpreter is going to say OK I'm only supposed to run this piece of code
54
00:04:17,140 --> 00:04:24,940
if this is true because this is now false I'm just going to completely ignore what's underneath here
55
00:04:25,270 --> 00:04:29,260
and just go to the next line that has well no indentation.
56
00:04:29,260 --> 00:04:31,740
So it's going to print check check.
57
00:04:32,050 --> 00:04:35,270
And this is the power of conditionals.
58
00:04:35,380 --> 00:04:42,100
We're able to essentially skip lines the interpreter doesn't even care what's in here because I just
59
00:04:42,100 --> 00:04:46,390
told my program to skip from line for two lines six.
60
00:04:46,420 --> 00:04:54,450
How cool is that so we learnt that there is this If keyword but there's also another thing we can use
61
00:04:54,780 --> 00:05:03,360
called else and you'll notice here that I did and added to the indentation and else as the name suggests
62
00:05:04,200 --> 00:05:09,920
simply says hey if this something is true then do this.
63
00:05:10,290 --> 00:05:14,430
Otherwise also else do this.
64
00:05:14,430 --> 00:05:21,230
And again you see that I've added the indentation so try to guess what's about to happen in this program.
65
00:05:21,550 --> 00:05:29,680
If I click Run I get checked check because it's saying hey is old is that true.
66
00:05:29,680 --> 00:05:30,790
No it's not true.
67
00:05:30,820 --> 00:05:33,080
Okay I'm going to completely ignore that.
68
00:05:33,340 --> 00:05:34,740
The interpreter sees else.
69
00:05:34,780 --> 00:05:35,040
Okay.
70
00:05:35,050 --> 00:05:43,030
Well this wasn't true so I'm going to run whenever this evaluates to false I'm going to always run print.
71
00:05:43,030 --> 00:05:44,810
Check check.
72
00:05:44,960 --> 00:05:51,760
What if I change is old to now equal to true what will happen next.
73
00:05:51,760 --> 00:06:05,920
Well I get you are old enough to drive else only runs if the F block of code evaluates to false very
74
00:06:06,040 --> 00:06:08,270
very cool all right.
75
00:06:08,270 --> 00:06:09,430
Let me ask you another question.
76
00:06:09,500 --> 00:06:23,470
What if I did print here and let's change this to you are not of age if I run this.
77
00:06:23,500 --> 00:06:24,790
Is that what you expected.
78
00:06:24,790 --> 00:06:32,340
Well I hope by now you agree that this is the expected behavior because is old is true.
79
00:06:32,380 --> 00:06:37,840
I'm going to print this and then Python interpreter is going to say well this was true so I'm going
80
00:06:37,840 --> 00:06:41,740
to completely ignore the else block and then just go to the next line.
81
00:06:41,830 --> 00:06:42,880
What's on line 8.
82
00:06:42,880 --> 00:06:43,360
Nothing.
83
00:06:43,360 --> 00:06:46,280
So I'm going to go to line 9 and I'm going to print.
84
00:06:46,380 --> 00:06:47,240
OK OK.
85
00:06:47,260 --> 00:06:57,290
OK as you can see we're now controlling the flow of our programs where instead of going from one to
86
00:06:57,290 --> 00:07:02,440
nine just line by line we're now saying a line for do some sort of check.
87
00:07:02,450 --> 00:07:05,430
And based on that skip a few lines.
88
00:07:05,780 --> 00:07:07,610
Very very cool.
89
00:07:07,630 --> 00:07:07,900
All right.
90
00:07:07,900 --> 00:07:10,120
There's one other thing I want to show you.
91
00:07:10,270 --> 00:07:12,620
And it's the.
92
00:07:13,450 --> 00:07:19,650
If and I know this kind of looks weird you would think that it will be else if.
93
00:07:19,720 --> 00:07:24,540
But no no no it's L. F..
94
00:07:24,570 --> 00:07:26,730
And what do you think this does.
95
00:07:26,730 --> 00:07:37,020
Well you use it in combination with if you say if something if otherwise else if another condition so
96
00:07:37,020 --> 00:07:42,870
let's say is licensed then do another condition.
97
00:07:42,870 --> 00:07:49,300
So let's say print you can drive now.
98
00:07:49,610 --> 00:07:51,860
So let's go through this again.
99
00:07:52,020 --> 00:08:00,810
I'm going to say is this person old enough if that person is old enough and this evaluates to true.
100
00:08:00,930 --> 00:08:11,630
Well then I want you to run this otherwise I want you to run this condition hey is this true.
101
00:08:11,760 --> 00:08:12,380
No.
102
00:08:12,470 --> 00:08:15,110
If it's not true then jumped to else.
103
00:08:15,240 --> 00:08:22,530
So what will happen here if I click Run we get true for the first conditional block.
104
00:08:22,560 --> 00:08:23,770
Right over here.
105
00:08:23,770 --> 00:08:29,010
So automatically Python interpreter is going to say well we just got true here so I'm going to ignore
106
00:08:29,010 --> 00:08:32,550
this and ignore this and then print.
107
00:08:32,550 --> 00:08:32,950
OK.
108
00:08:32,970 --> 00:08:38,150
OK but let's say that is old is now false.
109
00:08:39,650 --> 00:08:44,560
What will happen next if I click Run.
110
00:08:44,700 --> 00:08:46,020
This is false.
111
00:08:46,020 --> 00:08:53,000
So Python interpreter is going to say Nope not gonna care about this block and I'm going to go hey.
112
00:08:53,010 --> 00:08:57,540
Else if I condition hey is this person licensed.
113
00:08:57,690 --> 00:08:59,190
Yep they are.
114
00:08:59,190 --> 00:09:06,140
So I'm going to print you can drive now and ignore else so it works like this.
115
00:09:06,160 --> 00:09:10,000
What if both of these were false.
116
00:09:10,000 --> 00:09:16,610
Well as you can imagine the Python interpreter is going to ignore it's going to run this say no.
117
00:09:16,660 --> 00:09:17,620
This is false.
118
00:09:17,770 --> 00:09:18,200
This.
119
00:09:18,260 --> 00:09:19,590
No this is false.
120
00:09:19,600 --> 00:09:22,450
And then finally it's going to run this.
121
00:09:22,540 --> 00:09:29,080
You see that else is a catch all that is if none of these conditions are true then we're just going
122
00:09:29,080 --> 00:09:30,340
to run this.
123
00:09:30,340 --> 00:09:31,930
So it's a backup in a sense.
124
00:09:31,930 --> 00:09:38,660
Hey if all things fail Jane just do this very very cool.
125
00:09:38,720 --> 00:09:44,030
Now if you look at this program you're thinking this program doesn't really work that well does it.
126
00:09:44,030 --> 00:09:50,390
I mean we're checking if the person is old and if person is licensed but shouldn't we check both.
127
00:09:50,390 --> 00:09:54,530
We want somebody who has a license and who's old enough to dry.
128
00:09:54,560 --> 00:09:55,540
Mm hmm.
129
00:09:55,610 --> 00:09:57,690
This is this is a buggy program.
130
00:09:57,710 --> 00:10:02,320
If we implement this in a Tesla Well we're gonna get a lot of lawsuits right.
131
00:10:02,330 --> 00:10:07,120
Because we can get somebody who's maybe licensed but isn't old enough.
132
00:10:07,220 --> 00:10:14,360
Which I guess doesn't make sense but we can get somebody who is old enough but never got their driver's
133
00:10:14,360 --> 00:10:14,990
license.
134
00:10:14,990 --> 00:10:20,660
And somehow we get access to the car and they can start the engine and drive and oh that's a that's
135
00:10:20,660 --> 00:10:23,460
a lawsuit waiting to happen so how can we fix this.
136
00:10:23,470 --> 00:10:24,220
Mm hmm.
137
00:10:24,440 --> 00:10:25,400
You know what.
138
00:10:25,640 --> 00:10:31,420
The power is that this is an expression right.
139
00:10:31,450 --> 00:10:38,450
An expression if you remember is something that produces a value end up that means that this doesn't
140
00:10:38,450 --> 00:10:41,300
have to you know just have true here.
141
00:10:41,300 --> 00:10:42,650
It could be an expression.
142
00:10:42,650 --> 00:10:53,930
So I could say if is old and is licensed then I can do that.
143
00:10:54,240 --> 00:10:58,020
And this is something new that we haven't seen before the end.
144
00:10:58,020 --> 00:11:00,570
This is another keyword in Python.
145
00:11:00,900 --> 00:11:06,450
And in the now coming video we're gonna talk about more of these keywords but this should read like
146
00:11:06,480 --> 00:11:07,300
English right.
147
00:11:07,350 --> 00:11:21,120
If is old and is licensed then do this so it can now remove the L if and then here both expressions
148
00:11:21,210 --> 00:11:25,460
need to evaluate to True that is whatever happens here.
149
00:11:25,540 --> 00:11:32,930
This has to evaluate to true and this has to evaluate to true and only when both are true.
150
00:11:32,950 --> 00:11:33,910
Again as.
151
00:11:33,910 --> 00:11:45,360
And states then you're old enough to drive and you have a license so that if I run this well I get an
152
00:11:45,360 --> 00:11:50,970
error rate because or an unknown error but it goes into the else block because both of these are false
153
00:11:51,330 --> 00:11:55,800
if only one of them is true and I click Run.
154
00:11:56,020 --> 00:12:01,420
You are not of age because both of these have to be true.
155
00:12:01,540 --> 00:12:04,760
If I click Run Hey.
156
00:12:04,770 --> 00:12:11,510
All right the car is starting you can drive away with your Tesla OK before I finish this video because
157
00:12:11,510 --> 00:12:20,850
this is a lot I want to just note one thing that is the L if statement the l live statement is really
158
00:12:20,850 --> 00:12:27,350
really useful because when you have code like this where you have an if statement and an L statement
159
00:12:28,710 --> 00:12:31,260
usually you only see one of each.
160
00:12:31,290 --> 00:12:36,450
I mean sure in another part of the program I could have another if statement and an if statement with
161
00:12:36,450 --> 00:12:39,300
another else statement inside of here.
162
00:12:39,570 --> 00:12:47,670
But usually when you have this grouping together you only see one F and one else but you can have multiple
163
00:12:47,730 --> 00:12:56,730
l FS so I can have L if another condition here and then I can have another L if another condition here
164
00:12:57,110 --> 00:12:59,890
you can have as many as you want.
165
00:12:59,970 --> 00:13:05,790
Eventually the program is going to either evaluate one of these conditions to true or it's going to
166
00:13:05,790 --> 00:13:07,080
go into the else block.
167
00:13:07,320 --> 00:13:10,860
But usually you follow this order.
168
00:13:11,020 --> 00:13:12,190
All right.
169
00:13:12,220 --> 00:13:13,070
That was a lot.
170
00:13:13,090 --> 00:13:15,940
Let's take a break and explore this topic a little bit more.
171
00:13:16,060 --> 00:13:17,500
I'll see in the next one public.
16040
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.