1 00:00:00,840 --> 00:00:03,570 Instructor: I wanna talk to you about another way 2 00:00:03,570 --> 00:00:05,790 to do conditional logic. 3 00:00:05,790 --> 00:00:10,023 We saw the if, elif, and then the else statement. 4 00:00:11,100 --> 00:00:15,633 But there's another way called the ternary operator. 5 00:00:17,250 --> 00:00:22,250 Now this is still the same as the else if or if else 6 00:00:22,830 --> 00:00:25,530 statements, but it's a shortcut. 7 00:00:25,530 --> 00:00:29,250 So, you can only use these on certain conditional logic. 8 00:00:29,250 --> 00:00:32,130 So, it doesn't mean that, hey this is just 9 00:00:32,130 --> 00:00:33,540 an entirely new way. 10 00:00:33,540 --> 00:00:35,610 This is a shortcut, in a sense, 11 00:00:35,610 --> 00:00:39,870 for you to make your code a little bit cleaner. 12 00:00:39,870 --> 00:00:41,040 Let's have a look. 13 00:00:41,040 --> 00:00:43,110 By the way, these ternary operators 14 00:00:43,110 --> 00:00:46,440 can also be called conditional expressions. 15 00:00:46,440 --> 00:00:48,900 And when you hear the word expression 16 00:00:48,900 --> 00:00:50,790 that should mean something to you, right? 17 00:00:50,790 --> 00:00:55,790 It's an expression because it evaluates to a value. 18 00:00:56,400 --> 00:01:00,870 So, a ternary operator, or a conditional expression, 19 00:01:00,870 --> 00:01:03,480 is an operation that evaluates to something 20 00:01:03,480 --> 00:01:06,630 based on the condition being true or not. 21 00:01:06,630 --> 00:01:10,530 And it's actually a new feature of Python as of version 2.4. 22 00:01:10,530 --> 00:01:12,180 So, let's have a look. 23 00:01:12,180 --> 00:01:16,173 The way we use this is to say, is condition, 24 00:01:17,070 --> 00:01:22,070 if this is true, then do this. 25 00:01:22,380 --> 00:01:25,410 So, this is the condition if true, and bear with me here, 26 00:01:25,410 --> 00:01:28,680 it is a little bit confusing at first. 27 00:01:28,680 --> 00:01:33,363 We say if, and here we give condition. 28 00:01:34,440 --> 00:01:39,440 Otherwise condition if else, all right? 29 00:01:41,340 --> 00:01:43,110 That is, that is a little confusing. 30 00:01:43,110 --> 00:01:44,373 So, let's go through it. 31 00:01:45,210 --> 00:01:49,140 So, the if is going to check this condition. 32 00:01:49,140 --> 00:01:51,240 So, it doesn't actually start with here. 33 00:01:51,240 --> 00:01:54,750 It's going to say, hey, if this condition. 34 00:01:54,750 --> 00:01:58,620 Now, this is going to evaluate to either true or false. 35 00:01:58,620 --> 00:02:01,980 If it's true, then we're going to do this. 36 00:02:01,980 --> 00:02:04,680 Otherwise we're going to do this. 37 00:02:04,680 --> 00:02:08,073 So, let me show you an example of how that would work. 38 00:02:10,110 --> 00:02:12,870 Let's say we're trying to determine if, well, 39 00:02:12,870 --> 00:02:16,017 if a user is your friend, so we'll say is_friend. 40 00:02:17,490 --> 00:02:21,120 And here we can set true or false for friend. 41 00:02:21,120 --> 00:02:24,990 Maybe we can check on Facebook if users can message you, 42 00:02:24,990 --> 00:02:27,510 or on Twitter, whether Twitter users 43 00:02:27,510 --> 00:02:30,180 can give you direct messages. 44 00:02:30,180 --> 00:02:33,643 Well, here, and let's do can_message, 45 00:02:36,300 --> 00:02:40,200 and here we can do our ternary operator. 46 00:02:40,200 --> 00:02:44,370 We're going to say, "Message allowed" if, 47 00:02:44,370 --> 00:02:46,890 so this is, if the condition is true, 48 00:02:46,890 --> 00:02:50,880 the condition is hey, is this person your friend? 49 00:02:50,880 --> 00:02:54,420 Otherwise condition that is false, well, 50 00:02:54,420 --> 00:02:59,337 I'm going to say, "Not allowed to message." 51 00:03:00,240 --> 00:03:02,550 And you can see here that this is a one liner, 52 00:03:02,550 --> 00:03:05,160 the, it looks like two lines but you can see over here 53 00:03:05,160 --> 00:03:08,490 that it's not really, it's just that we're wrapping 54 00:03:08,490 --> 00:03:09,990 so we can see the entire code. 55 00:03:11,370 --> 00:03:16,370 So, let's run this and see what can_message prints. 56 00:03:17,880 --> 00:03:22,880 If I run this, "message is allowed," because, well, 57 00:03:23,400 --> 00:03:27,720 we've assigned "message allowed" to can_message because, 58 00:03:27,720 --> 00:03:29,760 well, the condition was true. 59 00:03:29,760 --> 00:03:33,607 If this person wasn't my friend, and I run this, 60 00:03:33,607 --> 00:03:35,700 "not allowed to message." 61 00:03:35,700 --> 00:03:40,200 Because the condition, the if condition, evaluates to false. 62 00:03:40,200 --> 00:03:42,750 I know the ordering here can be a little bit confusing. 63 00:03:42,750 --> 00:03:45,870 So, you might have to look at it a couple of times 64 00:03:45,870 --> 00:03:50,340 and you do need to use it a few times to really remember it. 65 00:03:50,340 --> 00:03:52,890 But this is the general rule. 66 00:03:52,890 --> 00:03:57,890 Condition if true, if condition else, condition if- 67 00:03:58,080 --> 00:04:00,780 Not if else, if false. 68 00:04:00,780 --> 00:04:02,310 Can't spell. 69 00:04:02,310 --> 00:04:03,150 All right. 70 00:04:03,150 --> 00:04:07,770 A nice shorthand way to do something when a condition 71 00:04:07,770 --> 00:04:11,730 is either true or false, or evaluates to true or false. 72 00:04:11,730 --> 00:04:13,305 All right, I'll see you in the next video. 73 00:04:13,305 --> 00:04:14,193 Bye-bye.