A use-case for for...else with no break

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Thu Nov 2 07:56:43 EDT 2017


On 11/02/2017 12:45 PM, Alberto Berti wrote:
>>>>>> "Steve" == Steve D'Aprano <steve+python at pearwood.info> writes:
> 
>      py> for x in "abcdefgh":
>      Steve> ...     print(x, end='')
>      Steve> ...
>      py> efghpy>
> 
> 
>      Steve> "For ... else" to the rescue!
> 
>      py> for char in "abcdefgh":
>      Steve> ...     print(char, end='')
>      Steve> ... else:
>      Steve> ...     print()
>      Steve> ...
>      Steve> abcdefgh
>      py>
> 
> else doesn't seem to bring any advantage over:
> 
> for char in "abcdefgh":
>      print(char, end='')
> print()
> 

Try running it interactively and you'll see,
wolfgang




More information about the Python-list mailing list