Conditionals#

Basic conditionals#

Conditionals are statements that evaluate to either True or False.
Here are some examples of conditional statements using numeric values:

print(17 < 17)

print(17 <= 17)

print(4.2 == 7.1)

print(4.2 != 7.1)
False
True
False
True

Here are some examples of conditional statements using strings:

print('this string' == 'this string')

print('this string' == 'something else')
True
False

Important

We use two equal signs == to test logical equality. We use != to mean “not equal to”.

We use a single equal sign = to assign/define variables.

For lists, we can also use the special in and not in.

custom_list = [4, 5, 6, 7, 8]

print(7 in custom_list)

print(10 in custom_list)

print(10 not in custom_list)
True
False
True

IF and ELSE statements#

if#

if statements check whether a conditional is True or False.
If True, a block of indented code is executed.
If False, the indented code block is ignored.
Do not forget the colon at the end of the if statement.

stock_price = 45
computed_value = 60

if computed_value > stock_price:
    print('Buy the stock.')
Buy the stock.

Important

The lines following the if statement are indented. When a line is not indented, it will run regardless of the conditional.

Python cares about “white space”, and a tab is a white space character.

if computed_value > stock_price:
    print('Buy the stock.')

print('Since this part is not indented, it will run no matter what.')
Buy the stock.
Since this part is not indented, it will run no matter what.

else#

The else block can be included if other lines should run when the conditional is false.

computed_value = 20

if computed_value > stock_price:
    print('Buy the stock.')
else:
    print('Do not buy the stock.')

print('Since this line is not indented, it will run no matter what.')
Do not buy the stock.
Since this line is not indented, it will run no matter what.

We can also use if statements with string variables.

report_to_skip = '2017q4'
current_report = '2018q1'

if current_report != report_to_skip:
    print('Continue with current report.')
Continue with current report.

Now let’s use an if statement to check whether something is in a list.
Remember our competitors list? Check to see if another company is in that list.

all_competitors = ['AAPL','MSFT','TSLA','AMZN']

another_company = 'MMM'

if another_company in all_competitors:
    print(another_company, 'is a competitor')
else:
    print(another_company, 'is NOT a competitor')
MMM is NOT a competitor

We can also use boolean variables in our analysis.
Suppose we have a variable that tells us if today is Wednesday.

today_is_wed = False

if today_is_wed:
    print("Tomorrow is Thursday.")
else:
    print("Today is not Wednesday.")
Today is not Wednesday.

On a single line#

For simple expressions, we can put the entire if/else on a single line. For example, this

stock_price = 45
computed_value = 60

if computed_value > stock_price:
    buy_it = True
else:
    buy_it = False

can instead be written like this

stock_price = 45
computed_value = 60

buy_it = True if computed_value > stock_price else False

ELIF statements#

Lastly, we can use elif when there are different ranges/categories/bins that correspond to a set of outcomes.

eps = 4.2

if eps >= 5.1:
    print('Earnings are incredibly high.')
elif eps >= 2.9:
    print('Earnings are moderately high.')
elif eps > 0:
    print('Earnings are positive.')
else:
    print('Earnings are negative.')
Earnings are moderately high.

Important

Use elif when you have mutually exclusive outcomes.

The following implementation does not have mutually exclusive print statements because it uses if instead of elif.

eps = 4.2

if eps >= 5.1:
    print('Earnings are incredibly high.')
if eps >= 2.9:
    print('Earnings are moderately high.')
if eps > 0:
    print('Earnings are positive.')
else:
    print('Earnings are negative.')
Earnings are moderately high.
Earnings are positive.

Multiple conditionals#

Suppose we have variables that contain the following information:

nflx_price = 225
nflx_beta = 1.1
nflx_sector = 'media'
nflx_eps = 7

And we would like to calculate and print the P/E ratio if the beta is larger than 1 and the company is in the “media” sector. If either statement is false, print out a statement saying the company does not match the criteria.

We can combine conditionals using the logical operator and.

if nflx_beta > 1 and nflx_sector == 'media':
    nflx_pe = nflx_price / nflx_eps
    print(nflx_pe)
else:
    print('Company does not meet criteria.')
32.142857142857146

or is also an option if that is the logic we prefer:

if nflx_beta > 1 or nflx_sector == 'media':
    nflx_pe = nflx_price / nflx_eps
    print(nflx_pe)
else:
    print('Company does not meet criteria.')
32.142857142857146