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
  • 모델을 수치적으로 계산
  • '조건이 충족될 때까지 동작 반복'
  • 예시: {{3}}
    • 오차 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...