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 , while)

Last updated at Posted at 2020-05-21

#for文(リストの場合)
for 変数名 in リスト:

fruits = ["apple" , "banana" , "strawberry"]

for fruit in fruits:
    print("好きな食べ物は" + fruit + "です")

出力結果
好きな食べ物はappleです
好きな食べ物はbananaです
好きな食べ物はstrawberryです


##for文(辞書)
for 変数名 in 辞書:

fruits = {"apple":"りんご" , "banana":"バナナ" , "strawberry":"イチゴ"}

for fruit_key in fruits:
    print(fruit_key + "は日本語で" + fruits[fruit_key] + "です")

出力結果
appleは日本語でりんごです
bananaは日本語バナナです
strawberryは日本語イチゴです


#while文
while 条件式:

x = 1

while x <= 5:
  print(x)
  x += 1

出力結果
1
2
3
4
5


#break
繰り返し処理を終了させる
条件式と一緒に使う

numbers = [1 , 2 , 3 , 4 , 5]
for number in numbers:
    print(number)
    if number == 3:
       break

上記の場合はnumberが3になると処理が終了する
出力結果
1
2
3


#continue
条件式の処理をスキップする

numbers = [1 , 2 , 3 , 4 , 5]
for number in numbers:
    print(number)
    if number % 2 == 0:
       continue

2で割り切れる時に処理をスキップする
出力結果
1
3
5

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?