Making Decisions - Other Conditions

Simple Conditions

Python code Explanation Example
== is the same as, or is equal to if name=="Fred":
!= is not same as, or is not equal to if number!=4:
< is less than (aka fewer than) if age<18:
> is greater than, (aka more than) if age>18:
<= is less than or equal to if age<=18:
>= is more than or equal to if age>=18:
in is in a list if 3 in [1,2,3,4]:

Some Points to Notice

  • Python uses two equals signs to check if things have the same value as in if name=="Fred":
  • Python uses one equals sign to assign a value to a variable as in name="Daphne"
  • You can test strings in conditions too - just remember to use quote marks around text strings.

Run the following code and enter the password


In [ ]:
password = input("Please enter the password: ")
if password=="Beeblebrox":
    print("Welcome Zaphod. How improbable of you.")
else:
    print("Get lost!")

Study the code you just ran. Hopefully you can see why getting the password right or wrong affects which print function is executed

Try This Yourself - Speed

No road in Shetland, UK has a speed limit above 60mph.
Let's write a program that could be part of a speed warning system in a car.
The program is partially complete - you just need to put the correct condition after the if statement.
Don't forget to add a colon : after the if condition


In [ ]:
speed = int(input("Please enter speed in mph: "))
if :
    print("You are exceeding the speed limit. Please slow down.")

Test your program three times with speeds of 30, 60 and 65.
If you've done it right, only the 65 miles per hour speed should result in the message being printed.

Try This Too - Police Car

Police and emergency vehicles are allowed to exceed the speed limit under certain circumstances.
Let's write a program which knows about emergency vehicles
The program is partially complete - you just need to print the message "You can exceed 60mph in an emergency if safe to do so" in the correct place.


In [ ]:
answer = input("Are you driving an emergency vehicle Y/N: ")
if answer=='Y':
    is_emergency_vehicle = True
else:
    is_emergency_vehicle = False
    
if is_emergency_vehicle==True:
    print("Change these words")

Test your program twice, answering first N then Y.
If you've done it right, answering Y to the question will print the message 'You can exceed 60mph...'

Complex Conditions

In real programs you often need to test several things at the same time, for example; is a person under 18 and living in Scotland?
Python can do this with complex conditions:

Python Code Explanation Example
and both conditions must be True if age>=13 and age<18:
or either condition must be True if age<13 or age>65:
not condition must not be True (i.e. must be False) if not(age==18):

Try This - Police Car Improved

When answering 'yes' to a question, a user might type:

  • Y (capital)
  • y (lower case)
  • yes
  • aye

Let's improve the emergency vehicle program to handle responses like Y,y,yes,Yes
The program is partially complete - you just need to extend the if statement with
or anwer=='yes'
and so on.


In [ ]:
answer = input("Are you driving an emergency vehicle Y/N: ")
if answer=='Y' or answer==:
    is_emergency_vehicle = True
else:
    is_emergency_vehicle = False
    
if is_emergency_vehicle==True:
    print("You can exceed 60mph in an emergency if safe to do so")

You should now complete questions 14-20