if 陳述

Intermediate Python for Finance

Kennedy Behrman

Data Engineer, Author, Founder

只列印賣出紀錄

trns = { 'symbol': 'TSLA', 'type':'BUY', 'amount': 300}
print(trns['amount'])
300
Intermediate Python for Finance

複合陳述

控制陳述
   陳述 1
   陳述 2
   陳述 3
Intermediate Python for Finance

控制陳述

if <expression> :
if x < y:
if x in y:
if x and y:
if x:
Intermediate Python for Finance

程式碼區塊

if <expression>:
    statement
    statement
    statement
if <expression>: statement;statement;statement
Intermediate Python for Finance

只列印賣出紀錄

trns = { 'symbol': 'TSLA', 'type':'BUY', 'amount': 300}
if trns['type'] == 'SELL':
    print(trns['amount'])
trns['type'] == 'SELL'
False
Intermediate Python for Finance

只列印賣出紀錄。

trns = { 'symbol': 'APPL', 'type':'SELL', 'amount': 200}
if trns['type'] == 'SELL':
    print(trns['amount'])
200
Intermediate Python for Finance

Else

if x in y:
    print("I found x in y")
else:
    print("No x in y")
Intermediate Python for Finance

Elif

if x == y:
    print("equals")
elif x < y:
    print("less")
Intermediate Python for Finance

Elif

if x == y:
    print("equals")
elif x < y:
    print("less")
elif x > y:
    print("more")
elif x == 0
    print("zero")
Intermediate Python for Finance

搭配 elif 的 else

if x == y:
    print("equals")
elif x < y:
    print("less")
elif x > y:
    print("more")
elif x == 0
    print("zero")
else:
    print("None of the above")
Intermediate Python for Finance

一起來練習吧!

Intermediate Python for Finance

Preparing Video For Download...