I want to talk to you about another way to do conditional logic, we saw the if L.F. and then the Elst
statement.
But there's another way called the ternary.
Operator, now, this is still the same as the else, if or if else statements, but it's a shortcut,
so you can only use these on certain conditional logic.
So it doesn't mean that, hey, this is just an entirely new way.
This is a shortcut, in a sense, for you to make your code a little bit cleaner.
Let's have a look.
By the way, these ternary operators can also be called conditional expressions.
And when you hear the word expression, that should mean something to you.
Right?
It's an expression because it evaluates to a value.
So a ternary operator or a conditional expression is an operation that evaluates to something based
on the condition being true or not.
And it's actually a new feature of Python as of version two point four.
So let's have a look.
The way we use this is to say is condition.
If this is true, then do this.
So this is the condition, if true, and bear with me here, it is a little bit confusing at first.
We say if.
And here we give condition.
Otherwise condition.
Else, all right, that is that is a little confusing, so let's go through it.
So the EFF is going to check this condition, so it doesn't actually start with here, it's going to
say, hey, if this condition.
Now this is going to evaluate to their true or false.
If it's true, then we're going to do this.
Otherwise we're going to do this.
So let me show you an example of how that would work.
Let's say we're trying to determine if, well, if a user is your friend, so we'll say is friend.
And here we can see that true or false for friend, maybe we can check on Facebook if users can message
you or on Twitter, whether Twitter users can give you direct messages.
Well, here and let's do can.
Message and here we can do our ternary operator, we're going to say message aloud, if so, this is
if the condition is true, the condition is, hey, is this person your friend otherwise condition that
is false?
Well, I'm going to say I'm not allowed to message.
And you can see here that this is a one liner, though it looks like two lines.
But you can see over here that it's not really it's just that we're wrapping so we can see the entire
code.
So let's run this and see what can message Prince if I run this.
Message is allowed because, well, we've assigned message loud to can message because while the condition
was true, if this person wasn't my friend and I run this not allowed to message because of the condition
that if condition evaluates to false, I know the ordering here can be a little bit confusing.
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
it.
But this is a general rule condition, if true, if condition or else condition, if not false.
If false.
Can't spell.
All right, a nice shorthand way to do something when a condition is either true or false or evaluates
to true or false.
All right, I'll see you in the next video.
Bye.