At diffusion, we believe education is the building block of society.
That’s why we work tirelessly to create the best content and tools to educate students, improve learning, and enhance the accessibility of education globally.
00:00 in this video we will learn more about
00:02 Anne's and/or in Python we will leave
00:05 one of the best ways to learn anything
00:07 is to study a concept and practice it
00:09 that's why we created a page for today's
00:12 tutorial on our website diffusion dot
00:14 Academy for you to view the video read
00:17 the notes and start practicing check out
00:19 the description below to view the link
00:21 in the previous videos we learned that
00:24 when a condition is met one or more
00:26 statements is or are executed for
00:29 example for the code if color equals
00:32 equals red print yes it's a primary
00:35 color the function print would be
00:38 executed if the string red was assigned
00:40 to the variable color Python would print
00:43 yes it's a primary color otherwise
00:45 Python won't display anything now what
00:48 happens if we need to test for two or
00:51 more conditions in that case we can use
00:53 the keyword and you can use the keyword
00:56 and to combine any number of conditions
00:58 together for example if you are looking
01:01 for a new house to rent you may have a
01:03 few requirements for example you may
01:06 want the house to be in London to cost
01:08 less than 2,000 pounds and you have two
01:10 or more rooms you can code this in
01:13 Python if country equals equals London
01:16 and price is less than 2000 and rooms
01:18 are greater than or equal to two print
01:21 this is a good place to rent for this
01:24 code Python only prints this is a good
01:26 place to rent if all the conditions are
01:28 met this means that even if only one of
01:31 the conditions wasn't met for example if
01:34 the house was in London and had three
01:36 rooms but the price was 2500 pound which
01:39 fails the second condition the if test
01:42 would fail and the print function won't
01:43 be executed and nothing would be
01:45 displayed what if you are more flexible
01:48 about the conditions now instead of only
01:50 considering to rent a house if all three
01:53 conditions were met you are considering
01:55 to rent the house if two conditions were
01:57 met first the house must be in London
01:59 and secondly the house can either be
02:02 less than 2,000 pounds or has two or
02:05 more rooms in this case we can use the
02:08 keyword or we use or to create a test
02:11 that passes if either of the
02:14 and or three conditions were met let's
02:16 code this in Python if country equals
02:19 equals London and price is less than
02:21 2,000 or rooms are greater than or equal
02:24 to two print this is a good place to
02:27 rent now let's look at the first line of
02:29 code you will notice that ands and or
02:32 are in the same line you may have also
02:35 noticed that the code could be
02:36 interpreted in two ways the first way it
02:40 might be interpreted is if the country
02:42 is in London and the price is less than
02:44 2,000 pounds then it passes the if test
02:47 alternatively if the rooms are greater
02:50 than or equal to two then it passes the
02:52 F test the second way that it might be
02:55 interpreted is if the country is in
02:58 London and either of the conditions the
03:00 price is less than 2,000 or the rooms or
03:02 greater than or equal to two is met it
03:05 passes the F test to avoid any
03:09 ambiguities we can use parentheses since
03:12 the second interpretation is the
03:14 preferred one we could use parentheses
03:16 to enclose the conditions price is less
03:18 than 2000 or rooms are greater than or
03:20 equal to two in this case it's clear
03:24 that if the house is in London and is
03:26 either less than 2000 pounds or has
03:28 greater than or equal to two rooms it
03:30 would pass the if test and python would
03:32 execute the print function and display
03:34 this is a good place to rent
Leave a Reply