That's more specifically a ternary operator expression than an if-then, here's the python syntax
value_when_true if condition else value_when_false
Better Example: (thanks Mr. Burns)
'Yes' if fruit == 'Apple' else 'No'
Now with assignment and contrast with if syntax
fruit = 'Apple'
isApple = True if fruit == 'Apple' else False
vs
fruit = 'Apple'
isApple = False
if fruit == 'Apple' : isApple = True
Answer from cmsjr on Stack OverflowThat's more specifically a ternary operator expression than an if-then, here's the python syntax
value_when_true if condition else value_when_false
Better Example: (thanks Mr. Burns)
'Yes' if fruit == 'Apple' else 'No'
Now with assignment and contrast with if syntax
fruit = 'Apple'
isApple = True if fruit == 'Apple' else False
vs
fruit = 'Apple'
isApple = False
if fruit == 'Apple' : isApple = True
Moreover, you can still use the "ordinary" if syntax and conflate it into one line with a colon.
if i > 3: print("We are done.")
or
field_plural = None
if field_plural is not None: print("insert into testtable(plural) '{0}'".format(field_plural))
return if else else if in one line
single line if statement
If-else condition in one liner
Does anybody else just not like the syntax for nested one-line list comprehension?
Videos
An example of Python's way of doing "ternary" expressions:
i = 5 if a > 7 else 0
translates into
if a > 7:
i = 5
else:
i = 0
This actually comes in handy when using list comprehensions, or sometimes in return statements, otherwise I'm not sure it helps that much in creating readable code.
The readability issue was discussed at length in this recent SO question better way than using if-else statement in python.
It also contains various other clever (and somewhat obfuscated) ways to accomplish the same task. It's worth a read just based on those posts.
Python's if can be used as a ternary operator:
>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'
in c we can do it like this
if (condition) {
return true;}
else if(condition1) {
return true1;}
else{
return false;}we can make it like this
return condition ? true : condition1? true1 : false
and i know we can do this in python
return true if condition else false
but i can't make it with else if , is there a way to make return if else if and else in one line ?
sorry for my bad english
hi, is there a way to write a single line if statement?
if rows is None:
rowsTot = len(content)
else:
rowsTot = rowsin java
rowsTot = (rows == none) ? len(content) : rows;
Thanks in advance :)
There's this common syntax 'a' if condition else 'b' and I found it more readable and faster to write, but recently I ran into problem like this
# basically means if A exists, it covers the previous one, else prev becomes A. usually used within a loop.
lst = ['x', '', 'y']
prev = None
for x in lst:
if x:
prev = x
else:
x = prevSomehow I cannot write this into prev = x if x else x = prev .
I kind of get it why this is an error but I just wonder if there's a better way to write this kind of code?