for와 while 반복문

금융을 위한 중급 Python

Kennedy Behrman

Data Engineer, Author, Founder

코드 블록 반복

CUSIP

037833100

17275R102

68389X105

심볼

AAPL

CSCO

ORCL

금융을 위한 중급 Python

반복문

for 루프

while 루프

금융을 위한 중급 Python

구문 구성요소

<Control Statement>
    <Code Block>
execution 1
execution 2
execution 3
금융을 위한 중급 Python

for 반복문

for <variable> in <sequence>:
for x in [0, 1, 2]:
d = {'key': 'value1'}
for x in d:
for x in "ORACLE":
금융을 위한 중급 Python

리스트 예시

for x in [0, 1, 2]:
    print(x)
0
1
2
금융을 위한 중급 Python

딕셔너리 예시

symbols = {'037833100': 'AAPL',
           '17275R102': 'CSCO'
           '68389X105': 'ORCL'}
for k in symbols:
    print(symbols[k])
AAPL
CSCO
ORCL
금융을 위한 중급 Python

문자열 예시

for x in "ORACLE":
    print(x)
O
R
A
C
L
E
금융을 위한 중급 Python

while 제어문

while <expression>:
금융을 위한 중급 Python

while 예시

x = 0

while x < 5:
    print(x)
    x = (x + 1)
0
1
2
3
4
금융을 위한 중급 Python

무한 루프

x = 0

while x <= 5:
     print(x)
금융을 위한 중급 Python

continue로 건너뛰기

for x in [0, 1, 2, 3]:
   if x == 2:
       continue
   print(x)
0
1
3
금융을 위한 중급 Python

break로 중단

while True:
    transaction = get_transaction()
    if transaction['symbol'] == 'ORCL':
        print('The current symbol is ORCL, break now')
        break
    print('Not ORCL')
Not ORCL
Not ORCL
Not ORCL
The current symbol is ORCL, break now
금융을 위한 중급 Python

for와 while로 연습해 봅시다!

금융을 위한 중급 Python

Preparing Video For Download...