Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,900 --> 00:00:05,850
I want to talk to you about another way to do conditional logic.
2
00:00:05,850 --> 00:00:17,150
We saw the F L F and then the else statement but there's another way called the ternary operator.
3
00:00:17,290 --> 00:00:26,650
Now this is still the same as the else f or if else statements but it's a shortcut so you can only use
4
00:00:26,650 --> 00:00:33,550
these on certain conditional logic so it doesn't mean that hey this is just the an entirely new way.
5
00:00:33,550 --> 00:00:39,940
This is a shortcut in a sense for you to make your code a little bit cleaner.
6
00:00:39,940 --> 00:00:40,940
Let's have a look.
7
00:00:41,020 --> 00:00:46,290
By the way these ternary operators can also be called conditional expressions.
8
00:00:46,510 --> 00:00:50,840
And when you hear the word expression that should mean something to you right.
9
00:00:50,860 --> 00:00:56,470
It's an expression because it evaluates to a value.
10
00:00:56,470 --> 00:01:03,910
So a ternary operator or a conditional expression is an operation that evaluates to something based
11
00:01:03,910 --> 00:01:06,570
on the condition being true or not.
12
00:01:06,700 --> 00:01:08,800
And it's actually a new feature of Python.
13
00:01:08,830 --> 00:01:10,560
As of version two point four.
14
00:01:10,570 --> 00:01:22,450
So let's have a look the way we use this is to say is condition if this is true then do this.
15
00:01:22,450 --> 00:01:24,520
So this is the condition if true.
16
00:01:24,520 --> 00:01:28,540
And bear with me here it is a little bit confusing at first.
17
00:01:28,750 --> 00:01:36,890
We say if and here we give condition otherwise condition
18
00:01:39,710 --> 00:01:40,530
else.
19
00:01:40,570 --> 00:01:41,320
All right.
20
00:01:41,390 --> 00:01:48,520
That is that is a little confusing so look let's go through it so the f is going to check this condition
21
00:01:49,210 --> 00:01:51,250
so it doesn't actually start with here.
22
00:01:51,310 --> 00:01:59,080
It's going to say hey if this condition now this is going to evaluate to their true or false if it's
23
00:01:59,080 --> 00:02:01,860
true then we're going to do this.
24
00:02:02,050 --> 00:02:04,720
Otherwise we're going to do this.
25
00:02:04,720 --> 00:02:08,210
So let me show you an example of how that would work.
26
00:02:10,170 --> 00:02:14,250
Let's say we're trying to determine if well if a user is your friend.
27
00:02:14,280 --> 00:02:17,330
So we'll say is a friend.
28
00:02:17,570 --> 00:02:21,150
And here we can set to True or false for a friend.
29
00:02:21,160 --> 00:02:28,590
Maybe we can check on Facebook if users can message you or on Twitter whether Twitter users can give
30
00:02:28,590 --> 00:02:30,240
you direct messages.
31
00:02:30,240 --> 00:02:36,120
Well here and let's do can message.
32
00:02:36,380 --> 00:02:40,220
And here we can do our ternary operator.
33
00:02:40,220 --> 00:02:49,010
We're going to say message aloud If so this is if the condition is true the condition is hey is this
34
00:02:49,010 --> 00:02:50,960
person your friend.
35
00:02:50,960 --> 00:02:53,350
Otherwise condition.
36
00:02:53,360 --> 00:02:54,140
That is false.
37
00:02:54,140 --> 00:03:03,320
Well I'm going to say not allowed to message and you can see here that this is a one liner though it
38
00:03:03,320 --> 00:03:04,360
looks like two lines.
39
00:03:04,370 --> 00:03:09,530
But you can see over here that it's not really is just that we're wrapping so we can see the entire
40
00:03:09,530 --> 00:03:17,860
code so let's run this and see what can message prints.
41
00:03:17,940 --> 00:03:27,960
If I run this message is allowed because well we've assigned message aloud to Ken message because while
42
00:03:27,960 --> 00:03:36,260
the condition was true if this person wasn't my friend and I run this not allowed to message because
43
00:03:36,250 --> 00:03:42,780
of the condition that if condition evaluates to false I know the ordering here can be a little bit confusing
44
00:03:42,780 --> 00:03:49,710
so you might have to look at it a couple times and you do need to use it a few times to really remember
45
00:03:49,710 --> 00:03:59,970
it but this is the general rule condition if true if condition else condition if not a false if false
46
00:04:00,830 --> 00:04:02,220
can't spell.
47
00:04:02,240 --> 00:04:03,140
All right.
48
00:04:03,210 --> 00:04:10,530
A nice shorthand way to do something when a condition is either true or false or evaluates to true or
49
00:04:10,530 --> 00:04:11,790
false.
50
00:04:11,790 --> 00:04:13,850
All right I'll see you in the next video.
51
00:04:13,860 --> 00:04:14,070
Bobby.
4925
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.