I want to talk to you about another way to do conditional logic.
We saw the F L F and then the else statement but there's another way called the ternary operator.
Now this is still the same as the else f 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 the 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 look let's go through it so the f 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 a friend.
And here we can set to True or false for a 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 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 is just that we're wrapping so we can see the entire
code so let's run this and see what can message prints.
If I run this message is allowed because well we've assigned message aloud 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
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 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 else condition if not a 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.
Bobby.