If statements

Intermediate Python for Finance

Kennedy Behrman

Data Engineer, Author, Founder

Printing sales only

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

Compound statements

control statement
   statement 1
   statement 2
   statement 3
Intermediate Python for Finance

Control Statement

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

Code blocks

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

Printing sales only

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

Printing sales only.

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

Else with elif

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

Let's practice!

Intermediate Python for Finance

Preparing Video For Download...