Instructor: I wanna talk to you about another way
to do conditional logic.
We saw the if, elif, and then the else 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 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 if, 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 can_message,
and here we can do our ternary operator.
We're going to say, "Message allowed" 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 prints.
If I run this, "message is allowed," because, well,
we've assigned "message allowed" to can_message because,
well, the condition was true.
If this person wasn't my friend, and I run this,
"not allowed to message."
Because the condition, the 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 else, condition if-
Not if else, 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.