Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

forにelse節が入る

Pythonのfor文にはelse節があります。
公式ではこんな感じで説明されている。

for_stmt ::= "for" target_list "in" expression_list ":" suite
["else" ":" suite]

実際のところ

for s in [1,2,3,range(1,4)][3]:
  print(s)
else:
  print("fin\n")

#>> 1
#>> 2
#>> 3
#>> fin
#>>