Yes, you can do this:
<condition> and myList.append('myString')
If <condition> is false, then short-circuiting will kick in and the right-hand side won't be evaluated. If <condition> is true, then the right-hand side will be evaluated and the element will be appended.
I'll just point out that doing the above is quite non-pythonic, and it would probably be best to write this, regardless:
if <condition>: myList.append('myString')
Demonstration:
>>> myList = []
>>> False and myList.append('myString')
False
>>> myList
[]
>>> True and myList.append('myString')
>>> myList
['myString']
Answer from Claudiu on Stack OverflowYes, you can do this:
<condition> and myList.append('myString')
If <condition> is false, then short-circuiting will kick in and the right-hand side won't be evaluated. If <condition> is true, then the right-hand side will be evaluated and the element will be appended.
I'll just point out that doing the above is quite non-pythonic, and it would probably be best to write this, regardless:
if <condition>: myList.append('myString')
Demonstration:
>>> myList = []
>>> False and myList.append('myString')
False
>>> myList
[]
>>> True and myList.append('myString')
>>> myList
['myString']
The reason the language doesn't allow you to use the syntax
variable = "something" if a_condition
without else is that, in the case where a_condition == False, variable is suddenly unknown. Maybe it could default to None, but Python requires that all variable assignments actually result in explicit assignments. This also applies to cases such as your function call, as the value passed to the function is evaluated just as the RHS of an assignment statement would be.
Similarly, all returns must actually return, even if they are conditional returns. Eg:
return variable if a_condition
is not allowed, but
return variable if a_condition else None
is allowed, since the second example is guaranteed to explicitly return something.
If statement, no elif, no else.
Making a single-line 'if' statement without an 'else' in Python 3 - Stack Overflow
single line if statement
python - Putting a simple if-then-else statement on one line - Stack Overflow
Videos
I'm curious about a certain section of some code i'm writing here-
I have a condition that adds additional work, but it is a very specific condition. Because it is so specific, I use an 'If' statement, with no else to follow up. Here is an example:
def run(self):
while not self.stopper.isSet():
try:
## Do some work
if Identity.user == 'username1':
## Do extra work, specific to username1
## Continue usual work
except self.queue.empty():
continueNotice no 'elif' or 'else' statement- I guess my question is, is there a better way to handle this? Or am I mistaken in assuming all 'If' statements need to be closed with 'else'?
Thanks!
EDIT: For context- Code deals with threading and queues, so it is important to get the 'username1' work done with the same item taken from the queue in the beginning of the 'Try' statement.
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 :)
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
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))