For- en while-lussen

Python voor Finance - gevorderd

Kennedy Behrman

Data Engineer, Author, Founder

Een codeblok herhalen

CUSIP

037833100

17275R102

68389X105

SYMBOL

AAPL

CSCO

ORCL

Python voor Finance - gevorderd

Lussen.

For-lus

While-lus

Python voor Finance - gevorderd

Onderdelen van een statement

<Control Statement>
    <Code Block>
execution 1
execution 2
execution 3
Python voor Finance - gevorderd

For-lussen

for <variable> in <sequence>:
for x in [0, 1, 2]:
d = {'key': 'value1'}
for x in d:
for x in "ORACLE":
Python voor Finance - gevorderd

Lijstvoorbeeld

for x in [0, 1, 2]:
    print(x)
0
1
2
Python voor Finance - gevorderd

Dictionary-voorbeeld

symbols = {'037833100': 'AAPL',
           '17275R102': 'CSCO'
           '68389X105': 'ORCL'}
for k in symbols:
    print(symbols[k])
AAPL
CSCO
ORCL
Python voor Finance - gevorderd

String-voorbeeld

for x in "ORACLE":
    print(x)
O
R
A
C
L
E
Python voor Finance - gevorderd

While-controle

while <expression>:
Python voor Finance - gevorderd

While-voorbeeld

x = 0

while x < 5:
    print(x)
    x = (x + 1)
0
1
2
3
4
Python voor Finance - gevorderd

Oneindige lussen

x = 0

while x <= 5:
     print(x)
Python voor Finance - gevorderd

Overslaan met continue

for x in [0, 1, 2, 3]:
   if x == 2:
       continue
   print(x)
0
1
3
Python voor Finance - gevorderd

Stoppen met 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 voor Finance - gevorderd

Laten we 'for'- en 'while'-lussen oefenen!

Python voor Finance - gevorderd

Preparing Video For Download...