Conditional Execution

Conditional execution allows a program to only execute code if some condition is met.

Predict what this code will do.


In [ ]:
x = 5
print(x > 2)

In [ ]:
x = 5
print(x < 2)

Summarize

What does x < y do? What does it spit out?

Modify

Change the following cell so it prints False


In [ ]:
x = 20
print (x > 2)

Predict what this code will do.


In [ ]:
x = 5
if x > 2:
    print(x)

In [ ]:
x = 0
if x > 2:
    print(x)

Predict what this code will do.


In [ ]:
x = 0
if x > 2:
    print(x)
print("hello")

Summarize

How does the if statement work?

Your simple comparison operators are:

  • x < y (less than)
  • x <= y (less than or equal to)
  • x > y (greater than)
  • x >= y (greater than or equal to)
  • x == y (is equal to)
  • x != y (is not equal to)

There are more, depending on the sorts of comparisons being made. We'll get to them later.

Modify

Change the code above so it prints out x.


In [ ]:
x = 20
if x < 5:
    print(x)

Implement

Write code that will only print x if x is more than 100.


In [ ]:

More complicated conditionals

Predict what this code will do.


In [ ]:
x = 2
if x < 5 and x > 10:
    print("condition met")

In [ ]:
x = 2
if x < 5 or x > 10:
    print("condition met")

In [ ]:
x = 2
if not x > 5:
    print("condition met")

Summarize

How do "and", "or", and "not" work?

Modify

Change the following code so it will print "HERE" if x is between 17 and 22.


In [ ]:
x = 20
if x < 5 or x > 10:
    print("HERE")

Implement

Write a conditional statement that prints x if it is either negative or if it is greater than 10.


In [ ]:

Predict what this code will do.


In [ ]:
x = 5

if x > 2:
    print("inside conditional")
    print("also inside conditional")

if x < 2:
    print("inside a different conditional")
    
print("not inside conditional")

Summarize

How does an if statement decide if something is inside the if or not?

Predict what this code will do.


In [ ]:
x = 5
if x > 10:
    print("condition 1")
else:
    print("condition 2")

Summarize

What does else do?

Predict what this code will do.


In [ ]:
x = 1

if x > 1:
    print("condition 1")
elif x == 1:
    print("condition 2")
else:
    print("condition 3")

Predict what this code will do.


In [ ]:
x = -2

if x > 5:
    print("a")
elif x > 0 and x <= 5:
    print("b")
elif x > -6 and x <= 0:
    print("c")
else:
    print("d")

Summarize

What does elif do? (it's pronounced "el"-"if", short for "else if").

In python the:

if CONDITION:
    DO_SOMETHING
elif SOME_OTHER_CONDITION:
    DO_SOMETHING_ELSE
elif YET_ANOTHER_CONDITION:
    DO_YET_ANOTHER_THING
else:
    DO_WHATEVER_WE_WANT

construct allows different responses in response to different conditions. If no conditions are met, whatever is under else is done. If multiple conditions are met, only the first condition is met.

Modify

Change the following program code so it prints "condition 2"


In [ ]:
x = 5

if x > 10:
    print("condition 1")
elif x < 0:
    print("condition 2")
else:
    print("condition 3")

Implement

Write an if/elif/else statement that prints "a" if $x < 0$, "b" if $x = 0$ and "c" otherwise.


In [ ]:

Summary

Your simple comparison operators are:

  • x < y (less than)
  • x <= y (less than or equal to)
  • x > y (greater than)
  • x >= y (greater than or equal to)
  • x == y (is equal to)
  • x != y (is not equal to)

There are more, depending on the sorts of comparisons being made. We'll get to them later.

You can build more complex conditions using and, or, and not.

For example:

x > 10 and x < 20

# Even more complicated
x > 20 and not (x > 22)

Conditionals use if/elif/else to make decisions

if CONDITION:
    DO_SOMETHING
elif SOME_OTHER_CONDITION:
    DO_SOMETHING_ELSE
elif YET_ANOTHER_CONDITION:
    DO_YET_ANOTHER_THING
else:
    DO_WHATEVER_WE_WANT

construct allows different responses in response to different conditions. If no conditions are met, whatever is under else is done. If multiple conditions are met, only the first condition is met.