I was going through ways of shortening a normal if else statement.
One of the formats I found was to change this:
if x == 1:
print("Yes")
else:
print("No")
Into this:
print("Yes") if x == 1 else print("No")
Is the code above considered pythonic? Otherwise, what would be a pythonic way of shortening the if else statement?
I also tried using return instead of print. I tried
return total if x == 1 else return half
which resulted in an unreachable statement for the 2nd return. Is there a way of doing it that i'm not able to see?
Finally, I tried to use an else if instead of an else with the code, but that wouldn't work either.
print(total) if x == 1 else print(half) if x == 2
The code above probably looks stupid logic-wise. Am I missing out on the proper format, or is it really not possible to do return and else if with this format?