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?

  • x < y tests whether $x$ is smaller than $y$.
  • It returns True or False.

Modify

Change the following cell so it prints False


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


True

In [2]:
# one way
x = 1
print (x > 2)

# another way
x = 20
print (x < 2)


False
False

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?

  • if takes something that can be interpreted True or False and then executes only if that something is True.
  • Anything indented under the if executes conditionally; anything not indented under the if runs no matter what.

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)

In [ ]:
# one way
x = 2
if x < 5:
    print(x)

# another way
x = 20
if x < 100:
    print(x)

Implement

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


In [ ]:
x = 100 
if x > 100:
    print(x)

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?

  • "and" and "or" let you logically concatenate True and False statements.
  • "not" lets you invert the logic of a True or False statement.

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")

In [ ]:
#answer 
x = 20
if x > 16 and x < 23:
    print("HERE")

Implement

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


In [ ]:
if x < 0 or x > 10:
    print(x)

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?

Python decides what is inside the conditional if using indentation.

Predict what this code will do.


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

Summarize

What does else do?

The else statement executes if the condition is not met.

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").

elif is a way to test another conditional in the same block.

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 [ ]:
if x < 0:
    print("a")
elif x == 0:
    print("b")
else:
    print("c")

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.