1 00:00:00,780 --> 00:00:09,180 I want to talk to you about another way to do conditional logic, we saw the if L.F. and then the Elst 2 00:00:09,300 --> 00:00:09,960 statement. 3 00:00:11,090 --> 00:00:14,750 But there's another way called the ternary. 4 00:00:15,740 --> 00:00:25,550 Operator, now, this is still the same as the else, if or if else statements, but it's a shortcut, 5 00:00:25,550 --> 00:00:28,910 so you can only use these on certain conditional logic. 6 00:00:29,240 --> 00:00:33,520 So it doesn't mean that, hey, this is just an entirely new way. 7 00:00:33,560 --> 00:00:39,350 This is a shortcut, in a sense, for you to make your code a little bit cleaner. 8 00:00:39,890 --> 00:00:40,570 Let's have a look. 9 00:00:41,000 --> 00:00:45,890 By the way, these ternary operators can also be called conditional expressions. 10 00:00:46,400 --> 00:00:50,300 And when you hear the word expression, that should mean something to you. 11 00:00:50,300 --> 00:00:50,590 Right? 12 00:00:50,780 --> 00:00:55,590 It's an expression because it evaluates to a value. 13 00:00:56,390 --> 00:01:03,920 So a ternary operator or a conditional expression is an operation that evaluates to something based 14 00:01:03,920 --> 00:01:05,890 on the condition being true or not. 15 00:01:06,590 --> 00:01:10,370 And it's actually a new feature of Python as of version two point four. 16 00:01:10,490 --> 00:01:11,780 So let's have a look. 17 00:01:12,200 --> 00:01:16,010 The way we use this is to say is condition. 18 00:01:17,050 --> 00:01:21,850 If this is true, then do this. 19 00:01:22,360 --> 00:01:28,270 So this is the condition, if true, and bear with me here, it is a little bit confusing at first. 20 00:01:28,690 --> 00:01:29,950 We say if. 21 00:01:31,170 --> 00:01:33,210 And here we give condition. 22 00:01:34,410 --> 00:01:36,900 Otherwise condition. 23 00:01:39,560 --> 00:01:44,290 Else, all right, that is that is a little confusing, so let's go through it. 24 00:01:45,200 --> 00:01:51,680 So the EFF is going to check this condition, so it doesn't actually start with here, it's going to 25 00:01:51,680 --> 00:01:54,000 say, hey, if this condition. 26 00:01:54,770 --> 00:01:57,950 Now this is going to evaluate to their true or false. 27 00:01:58,580 --> 00:02:01,370 If it's true, then we're going to do this. 28 00:02:01,940 --> 00:02:04,230 Otherwise we're going to do this. 29 00:02:04,670 --> 00:02:07,930 So let me show you an example of how that would work. 30 00:02:10,080 --> 00:02:16,530 Let's say we're trying to determine if, well, if a user is your friend, so we'll say is friend. 31 00:02:17,460 --> 00:02:24,090 And here we can see that true or false for friend, maybe we can check on Facebook if users can message 32 00:02:24,090 --> 00:02:29,660 you or on Twitter, whether Twitter users can give you direct messages. 33 00:02:30,150 --> 00:02:33,540 Well, here and let's do can. 34 00:02:34,840 --> 00:02:45,130 Message and here we can do our ternary operator, we're going to say message aloud, if so, this is 35 00:02:45,130 --> 00:02:53,560 if the condition is true, the condition is, hey, is this person your friend otherwise condition that 36 00:02:53,560 --> 00:02:54,100 is false? 37 00:02:54,130 --> 00:02:59,110 Well, I'm going to say I'm not allowed to message. 38 00:03:00,220 --> 00:03:04,360 And you can see here that this is a one liner, though it looks like two lines. 39 00:03:04,360 --> 00:03:09,550 But you can see over here that it's not really it's just that we're wrapping so we can see the entire 40 00:03:09,550 --> 00:03:09,880 code. 41 00:03:11,340 --> 00:03:18,840 So let's run this and see what can message Prince if I run this. 42 00:03:20,650 --> 00:03:28,540 Message is allowed because, well, we've assigned message loud to can message because while the condition 43 00:03:28,540 --> 00:03:37,120 was true, if this person wasn't my friend and I run this not allowed to message because of the condition 44 00:03:37,120 --> 00:03:42,790 that if condition evaluates to false, I know the ordering here can be a little bit confusing. 45 00:03:42,790 --> 00:03:49,720 So you might have to look at it a couple of times and you do need to use it a few times to really remember 46 00:03:49,720 --> 00:03:49,930 it. 47 00:03:50,290 --> 00:03:59,020 But this is a general rule condition, if true, if condition or else condition, if not false. 48 00:03:59,020 --> 00:03:59,980 If false. 49 00:04:00,790 --> 00:04:01,330 Can't spell. 50 00:04:02,250 --> 00:04:09,960 All right, a nice shorthand way to do something when a condition is either true or false or evaluates 51 00:04:09,960 --> 00:04:10,860 to true or false. 52 00:04:11,700 --> 00:04:13,520 All right, I'll see you in the next video. 53 00:04:13,810 --> 00:04:13,990 Bye.