0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonのいろんな書き方 for-else リスト内包など

Posted at

for-else

list = [0,1,2,3,4]

#普通
for i in list:
  print(i)
print('for分終わりました!')

#for-else
for i in list:
  print(i)
else:
  print('終わりました')

#出力
#0
#1
#2
#3
#4
#終わりました

#forの処理が終わると、elseに飛んで処理を実行する

break

for i in range(1,10):
  print(i)
  if i >= 90:
  break

  #breakで処理を中断できる

リスト内包

#リスト内包表記
# [式 for 変数 in イテラルオブジェクト]

#listに1を足してあげる
plus_one = [i+1 for i in list]
print(plus_one)

# インクリメントではダメらしい
[i++ for i in list]

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?