I want to talk to you about another way to do conditional logic.
We saw the if lf and then the else statement.
But there's another way called the ternary.
Operator.
Now, this is still the same as the elseif 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 a 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 2.4.
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.
If.
Else.
All right.
That is that is a little confusing.
So let's go through it.
So the if 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 either 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 a well, if a user is your friend, so we'll say is friend.
And here we can set 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 ten.
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 not allowed to message.
And you can see here that this is a one liner.
The 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 Ken message because while the condition
was true if this person wasn't my friend and I run this not allowed to message because 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 the general rule condition.
If true if condition ls 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 bye.