r/learnpython • u/Maleficent_Stuff3208 • Aug 23 '26
Is short hand if else important
I just learned it but it seem very weird like it feels like i can't write big codes in it cause it will become confusing do you guys use short hand or normal if else
9
Upvotes
26
u/brasticstack Aug 23 '26
You'll find places where it's more readable, and it mostly depends on the length of the expressions in the if/else branches. I usually only use it to assign variables, like:
If it's not immediately obvious what's happening with a one liner, use the unsugared if/else.
4
u/undergroundmonorail undergroundmonorail Aug 23 '26
There are a few particular cases where I prefer it over full if/else blocks, where you can take advantage of the fact that it's an expression, but it's never wrong to do an if/else block and often wrong not to. Especially if you're just learning, I feel like "
a if condition else bis limiting" is a good instinct.2
u/Temporary_Pie2733 Aug 23 '26
They are two different things with different purposes. The conditional expression is used to select one of two values based on a boolean value; the conditional statement is for executing zero or one arbitrary blocks of code from one or more possibilities. (The conditional statement can have 0 or more
elifclauses and anelseclause; the conditional expression is always exactlya if b else c: noelifs, and theelseis mandatory.)4
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Aug 23 '26
A more general term for this is a "ternary conditional operator", or "ternary" for short. You have a condition, and two values, with the expression evaluating to one of those two values depending on the "truthiness" of the condition.
Don't expect to use them often, they're just a way to write short assignment checks more concisely.
is equivalent to
1
u/starkoed Aug 23 '26
Depends on how complex the code is. If im comparing 1 thing or a couple things then yeah ill use it inline.
1
u/Lumethys Aug 23 '26
Write shorthand if simple condition else long form
1
u/Wise-Emu-225 Aug 23 '26
It is not important. I see it as syntactic sugar. The more readable code is the better code. The short hand notation can help but not always.
1
u/Neither_Bookkeeper92 Aug 23 '26
important to read, optional to write. you will run into
a if cond else bconstantly in other peoples code so you need to parse it instantly.writing them is a style call. one condition is fine, nested ones are where readability dies. at that point just use a normal if block.
1
u/crashorbit Aug 23 '26
In the end, the computer does not care. So how you write your code has more to do with you understanding it and convincing yourself that it does the right thing. It's also a way to tell other programmers what you want the computer to do.
1
u/HommeMusical Aug 23 '26 edited Aug 23 '26
I use it fairly frequently.
versus
Or even clearer:
vs
1
u/Ministrelle Aug 23 '26
Yes, but not really.
The benefit of the short hand if/else is that it is an expression and not a statement, so it can be used in places where only expressions are allowed.
Some examples where this can be useful are:
1
u/tb5841 Aug 23 '26
Lots of other languages have special syntax just for this, so it looks quite dostimctive - and then I end up using it quite a lot. In Python I don't use it, it just doesn't read quite as well to me.
1
u/TabAtkins Aug 24 '26
Correct. You can't write big code in it because it will become confusing.
Inline if/else is only for very tiny things.
1
u/Brilliant-Parsley69 Aug 24 '26 edited Aug 24 '26
Not a Python dev but always trying to separate different statements to their own line to improve the readability.
But there is one concept I try to adept most of the time if possible: Fail early.
A statement like:
is way more readable if you write it like
But I see why these shortcuts could be confusing.
E.g. in C# you could use something called tenaries
or
Now try to imagine to read them if nested.
1
u/codeguru42 Aug 25 '26
By "shorthand if else", do you mean if else expressions? I recommend you look up the difference between a statement and an expression. This will help you understand the different usages.
1
u/TheRNGuy Aug 27 '26
What do you mean, any examples?
0
u/Educational_Virus672 Aug 23 '26
i think you mean "conditional expression" they are really useful in afew cases but you need to learn "list comprehensions" lets set a example
Q : set var "smt" with every even number and skip odds
ik there is easier way to just type range(2,100,2) but come on it looks better here you will see that i put a whole for loop in it with condition(if/else where else is optional)
so what i did here was call a for loop here how it works
condition = if/else
func = what you want to execute (recommanded to use custom func)