Alright, so conditional expressions aren't that hard, really.
It is just an expression: a piece of code that evaluates to a result.
But then, it is tied to a condition: depending on whether the condition is truthy or falsy, the final result changes.
Check the function below:
This function just checks if a number is odd or even.
It does so with an `if` statement and the condition `n % 2`, that just checks if `n` leaves a remainder when divided by 2.
Then, depending on whether the condition evaluates to truthy or falsy, we assign to parity.
But assigning to `parity` like that is what conditional expressions excel at!
Here's the same function, but using a conditional expression:
The conditional expression has three parts:
1. the value we want if the condition is truthy;
2. the condition to check; and
3. the value we want if the condition is falsy.
Here is the conditional expression from the function above.
Can you spot the three parts?
Here is a different example with a custom implementation of the built-in `abs` function.
Given a number, return the same number if the argument is already positive.
If it's negative, flip the sign:
Again, depending on whether the number is positive or not (the condition `x > 0`) we return one of two different values!
So...
Conditional expressions to the rescue!
Again, we can take a closer look at the conditional expression itself.
Can you figure out what are the three parts that make up this conditional expression?
Now, a conditional expression has three parts, and none of them are optional.
For example, it doesn't make sense to get rid of the `else ...` part:
So, if you have a variable that you may or may not want to modify, you need to do one of two things.
You can use a standard `if` statement to modify it in case you need that:
Ooooooor, you can use a conditional statement, but the expression that comes after the `else` is the variable itself.
This means the conditional expression acts as a βdo-nothingβ if the condition is falsy:
Another cool thing about conditional expressions is that they do βshort-circuitingβ.
I wrote about Boolean short-circuiting in depth in an article of mine (that you can see linked here) and short-circuiting for conditional expressions is similar.
Do you want to master Python π list comprehensions?
Do you want to become more proficient working with lists in Python π?
If you answered with βyesβ, then here's a cheatsheet of mine to help you out.
This is also a thread π𧡠that breaks it down and explains everything:
First off, here is the anatomy of a list comprehension.
There's 4 parts to a list comp:
1. enclosing brackets to create the list [];
2. expression that transforms your data;
3. `for` iteration over the initial data;
4. (optional!) condition to filter some data.
Do list comprehensions look confusing?
Maybe.
But that's because you didn't realise that list comprehensions have equivalent `for` loops!
The coloured boxes below show the equivalent bits in the list comp and in the `for` loop: