whileループ

Python中級

Hugo Bowne-Anderson

Data Scientist at DataCamp

if-elif-else

control.py

  • 構文を一度だけ通過します!
z = 6
if z % 2 == 0 :  # True
    print("z is divisible by 2")  # Executed
elif z % 3 == 0 :
    print("z is divisible by 3")
else :
    print("z is neither divisible by 2 nor by 3")

...   # Moving on
  • whileループ = 繰り返されるif文
Python中級

While

while condition :
    expression
  • 数値計算モデル
  • 条件が満たされるまで処理を繰り返す
  • 例:
    • 誤差は50から始まる
    • 実行ごとに誤差を4で割る
    • 誤差が1より大きくなくなるまで続ける
Python中級

While

while condition :
    expression

while_loop.py

error = 50.0


while error > 1:
error = error / 4
print(error)
  • 誤差は50から始まる
  • 実行ごとに誤差を4で割る
  • 誤差が1より大きくなくなるまで続ける
Python中級

While

while condition :
    expression

while_loop.py

error = 50.0
#     50
while error > 1:    # True
      error = error / 4
      print(error)
12.5
Python中級

While

while condition :
    expression

while_loop.py

error = 50.0
#     12.5
while error > 1:    # True
      error = error / 4
      print(error)
12.5
3.125
Python中級

While

while condition :
    expression

while_loop.py

error = 50.0
#     3.125
while error > 1:    # True
      error = error / 4
      print(error)
12.5
3.125
0.78125
Python中級

While

while condition :
    expression

while_loop.py

error = 50.0
#     0.78125
while error > 1:    # False
    error = error / 4
    print(error)
12.5
3.125
0.78125
Python中級

While

while condition :
    expression

while_loop.py

error = 50.0
while error > 1 :    # always True
    # error = error / 4
    print(error)
50
50
50
50
50
50
50
...
  • DataCamp:セッションの接続が切断されました
  • ローカルシステム: Control + C
Python中級

練習しましょう!

Python中級

Preparing Video For Download...