Python Basics Tutorial 11 – Python and or

Notes from the video Python Course Singapore | Learn Python Programming | Tutorial 11 – Python and or’:

In the previous videos, we learnt that when a condition is met, one or more statements is or are executed.

For example, for the code

if colour == “red”:

              print(“Yes, it’s a primary colour.”)

 

The print() function would be executed if the string “red” was assigned to the variable colour. Python would print Yes, it’s a primary colour.

Otherwise, the if-test fails. The print() function is not executed, and nothing is displayed.

Now what happens if we need to test for two or more conditions?

In that case, we can use the keyword and.

You can use the keyword and to combine any number of conditions together.

 

For example, if you are looking a new house to rent. You may have a few requirements:

You want the house to be in London, to cost less than 2000 pounds, and to have 2 or more rooms.

You can code this in Python:

If country == ”London” and price < 2000 and rooms >= 2:

              print(“This is a good place to rent.”)

For this code, Python only displays This is a good place to rent. if ALL the conditions are met.

This means that even if only one of the conditions wasn’t met, for example if the house was in London and had 3 rooms but the price was 2500 pounds – which fails the second condition – the if-test would fail and the print() function won’t be executed and nothing would be displayed.

What if you were more flexible about the conditions?

Now instead of only considering to rent a house if all three conditions were met, you are considering to rent the house if two conditions were met:

First, the house must be in London and

Secondly, the house can either be less than 2000 pound or has two or more rooms.

 

In this case, we can use the keyword or. We use or to create a test that passes if either of the conditions were met.

 

Let’s code this in Python:

if country == “London” and price < 2000 or rooms >= 2:

              print(“This is a good place to rent.”)

 

Now let’s look at the first line of code:

if country == “London” and price < 2000 or rooms >= 2:

 

You will notice that and and or are in the same line. This means you can use a mixture of and and or when you are testing for conditions.

 

You may have also noticed that the code could be interpreted in two ways:

 

The first way it might be interpreted is:

If the country is in London and the price is less than 2000 pounds, then it passes the if-test.

Alternatively, if number of the rooms are greater than or equal to 2, then it passes the if-test.

 

The second way that it might be interpreted is:

If the country is in London and either of the conditions – the price is less than 2000 pounds or the number of the rooms are greater than or equal to 2 – is met, it passes the if-test.

 

To avoid any ambiguities, we can use parentheses.

 

Since the second interpretation is the preferred one, we can use parentheses to enclose the conditions price is less than 2000 or rooms are greater than or equal to 2.

 

if country == “London” and (price < 2000 or rooms >= 2):

 

In this case, it’s clear that if the house is in London, and is either less than 2000 pounds or has greater than or equal to 2 rooms, it would pass the if-test and Python would execute the print function and display This is a good place to rent.

Get FREE LESSONS and TIPS on how YOU can excel at programming and business

Awesome! Thank you for subscribing!