Python Conditionals

What is a conditional?  It is a piece of code that performs some action when a condition is met.  Such as when something is greater than something else.  Imagine if I said you can vote when you are  18 years of age or older.  We can write that as a conditional piece of code as:

Comparison Operators

A relational operator compares two expressions and then returns a True or False.

Operator Name Example
== Equal my_name == “Ferguson”
!= Not Equal cash != 100
< Less Than age_not_drink < 21
<= Less Than or Equal age_still_working <= 62
> Greater Than age_ok_drive > 15
>= Greater Than or Equal age_ok_drink >= 21

  Boolean Operators

Operator Name Example
and And if name == “John” and age == 40:
or Or If name == “John” or name == “Sam”:

Membership Operators

Operator Name Example
in In if name in list_names (checks if is a member, returns 1 if is)
not in Not in for name not in list_names (checks if is not a member, returns 1 if is)

  Identity Operators

Operator Name Example
is Is x is y  (stored values ares equal, returns 1 if true)
is not Is not x is not y (stored values ares not equal, returns 1 if true)

 

Picking up on our adventure game, we will need some programming tools to make the game function.  In this example we will choose the value of a 20 sided die roll on the first line.  We can randomize this if we choose.

Now take a look at the if, elif, else example below.  Can you tell what it does without running it?  Copy it into PyCharms and run it to see if you are right.

die_roll_20 = 2
if (die_roll_20 >= 16):
    print("Great attack!  You did  4 points of damage!")
elif (die_roll_20 >= 12):
    print("Good attack!  You did 3 points of damage.")
elif (die_roll_20 >= 8):
    print("You did OK.  You did 2 points of damage.")
elif (die_roll_20 >= 4):
    print("Only a glancing blow.  You did 1 point of damage.")
else:
    print("You completely missed your target!  Zero damage done.")

if, elif and else statements need to end in a colon!  This is a common error if you get an error message check this out first.

And
Now if we want to make something happen when 2 things occur together we would use the “and” operator.

swingSword = 9
hitSword = 9

if swingSword >= 10 and hitSword >= 10:  #both have to occur to trigger the event
    print("You successfully hit the dragon!")
else:
    print("Sorry you missed!")

Or
This will check if either magic word was used to open the door.  It is just one “or” the other.

#or
magicWord = "sesame"

if magicWord == "abra" or "sesame":
    print("The magic door opened!")
else:
    print("try another magic word.")

In

#in
name = "Xorg"
team = ["Alli", "Brawn", "Xorg"]

if name in team:
    print("You are on the team of adventurers!")
else:
    print("Sorry you are not on the team.")

Not In

#not in
name = "Xorg"
banned = ["Alli", "Brawn"]

if name not in banned:
    print("You are not banned from the pub, welcome!")
else:
    print("You have been banned from the pub!")

Is

#is
word1 = "open"
word2 = "sesame"

if word1 is "open" and word2 is "sesame":
    print("the magic door opened up!")
else:
    print("Sorry that was not the magic phrase.")

Is Not

#is not
word1 = "open"
word2 = "sesame"

if word1 is not "open" and word2 is not "sesame":
    print("Sorry that was not the magic phrase.")
else:
    print("the magic door opened up!")

 

Great job!  These conditionals give us the tools to have our code make some decisions based on values.