Tips and Tricks for Advanced Python
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is...
For further actions, you may consider blocking this person and/or reporting abuse
I think it depends on how complicated the comprehension or the ternary expression is. If it's hard to comprehend, then a loop or an if...elif...else statement would be better, but if it's a simple statement, then a comprehension or ternary expression is just as good and is more Pythonic.
Thank you! Very helpful!!
WIth my pleasure~~~
I agree with you.
Hi, thanks for sharing :)
"else statement also be used after for loop, it is executed after for loop", but why it could be needed? Just put some code after the for loop do the job, nop?
The 'else' is used if the loop completes successfully, so if the loop ends early, such as by using a 'break' statement, the 'else' won't run. It's just an easier way to run code if and only if the loop finishes successfully without needing to use a flag variable to detect whether the loop finishes.
With flag:
With 'else':
In both cases, the text won't print, since the loop didn't complete. However, if the loops did complete, then the text would print.
Hi, well thank a ton! :)
Hi, you're welcome.
else: after for loop confirms the for loop is finished, not confirms the exit of the loop.
else: does not be executed when it is exited by 'break' statement.
Thanks for the feedback. I agree with you.
Wow, I know only half of them. Thanks for the great tips!
You're welcome!