@[email protected] to Programmer [email protected]English • edit-21 day agoThe infamous "if loop" actually existsdiscuss.tchncs.deimagemessage-square21fedilinkarrow-up1156arrow-down19file-text
arrow-up1147arrow-down1imageThe infamous "if loop" actually existsdiscuss.tchncs.de@[email protected] to Programmer [email protected]English • edit-21 day agomessage-square21fedilinkfile-text
minus-square@[email protected]linkfedilink7•6 hours agoDid you know python for loops have an else clause? Did you also know it runs after the loop iterator is done, and not when there was never anything to iterate over at all? https://docs.python.org/3/reference/compound_stmts.html#for
minus-square@Vigge93link1•2 hours agoYour point about it not running when there is nothing to iterate over is incorrect. The else-statement runs when the iterator is exhausted; if the iterator empty, it is exhausted immediately and the else-statement is executed.
minus-square@[email protected]linkfedilink2•38 minutes agoI think it’s intended as “not only when” because it would make sense to have an “if empty” case but the way it is, it doesn’t make sense
Did you know python for loops have an else clause?
Did you also know it runs after the loop iterator is done, and not when there was never anything to iterate over at all?
https://docs.python.org/3/reference/compound_stmts.html#for
Your point about it not running when there is nothing to iterate over is incorrect. The else-statement runs when the iterator is exhausted; if the iterator empty, it is exhausted immediately and the else-statement is executed.
I think it’s intended as “not only when” because it would make sense to have an “if empty” case but the way it is, it doesn’t make sense
That actually seemed quite useful at first.