Writing Conditional Statements

Here is the scenario from the Pirates of the Caribbean:

In [1]:
from IPython.display import YouTubeVideo
YouTubeVideo("DvzOjZu0NqI")
Out[1]:

It costs 1 shilling to tie up the boat at the dock, and 3 shillings to do so under a pseudonym (say Mr. Smith). Here is one possible way of writing this

In [2]:
shillings = 2
name = "Jack Sparrow"

if shillings >= 3:
    print("Welcome to Port Royal, Mr. Smith")
elif shillings >= 1:
    print("Welcome to Port Royal, " + name)
else:
    print("Go away please")
    
Welcome to Port Royal, Jack Sparrow

Conditionals: possible bugs

Here is an incorrect way of writing this:

In [3]:
shillings = 3
name = "Jack Sparrow"

if shillings >= 1:
    print("Welcome to Port Royal, " + name)
elif shillings >= 3:
    print("Welcome to Port Royal, Mr. Smith")
else:
    print("Go away please")
print("It was a pleasure serving you")
Welcome to Port Royal, Jack Sparrow
It was a pleasure serving you

The if-elif-else structure works as follows: each condition is evaluated in turn, top-to-bottom. When a condition that evaluates to True is encountered, the corresponding lines are executed, and no more conditions are evaluated.

In the code above, since 3 >= 1, we print Welcome to Port Royal, Jack Sparrow, even though shillings == 3, so that what we wanted to print is Welcome to Port Royal, Mr. Smith.

Note that since the line print("It was a pleasure serving you") is not indented, it is always executed. If it were indented, it would be part of the else caluse, and would be executed if none of the conditions held.

The nice thing about the code in [2] is that the second condition really means 1 <= shillings < 3, because by the time we are evaluating shillings >= 1, we already know that shillings < 3, since we first checked whether shillings was greater or equal to three, and if we are checking whether shillings >= 1, the first condition evaluated to False

Conditionals: multilpe elifs

We can have multiple elifs. For example:

In [4]:
shillings = 300000001
name = "Jack Sparrow"

if shillings >= 300000000:
    print("Welcome to Port Royal, Mr. Smith. Here is your iron ring")
elif shillings >= 3:
    print("Welcome to Port Royal, Mr. Smith")
elif shillings >= 1:
    print("Welcome to Port Royal, " + name)
else:
    print("Go away please")
Welcome to Port Royal, Mr. Smith. Here is your iron ring

Again, all the conditions are evaluated top-to-bottom, until we reach a true condition, which gets executed. If none of the conditions are executed, the else blog is executed.

Conditionals: multiple if statements and not using elifs/elses

Consider the following:

In [5]:
shillings = 300000001
name = "Jack Sparrow"

if shillings >= 300000000:
    print("Welcome to Port Royal, Mr. Smith. Here is your iron ring")
if shillings >= 3:
    print("Welcome to Port Royal, Mr. Smith")
Welcome to Port Royal, Mr. Smith. Here is your iron ring
Welcome to Port Royal, Mr. Smith

Here, we switched the elif in the second clause to an if. The effect is that every conditon is evaluated, and more than one block can get executed.

Writing reusable code

Above, we repeated the phrase "Welcome to Port Royal" multiple times. This is perhaps not ideal -- what if we wanted to change that message? We would have to change the welcome message multiple times. Here is another way of writing similar code to what we have been doing before

In [6]:
shillings = -.5
name = "Jack Sparrow"
welcome_message = "Welcome to Port Royal"
if shillings >= 3:
    name = "Mr. Smith"
elif shillings < 1:             #Could say if shillings < 1 -- why?
    welcome_message = "Go away"
print(welcome_message + ", " + name)
Go away, Jack Sparrow