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
Add `raise ... if ...` syntax to `raise` statement without `else` - Ideas - Discussions on Python.org
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 :)