1
00:00:04,460 --> 00:00:10,100
So let's look at simplifying chained
comparisons. The code on line 3,

2
00:00:10,100 --> 00:00:14,090
if you check carefully, is underlined
with s squiggly line, which intellij and

3
00:00:14,090 --> 00:00:18,140
PyCharm do when they have a suggestion
about your code. So if we hover our mouse

4
00:00:18,140 --> 00:00:23,440
over that, you can see there it's saying
simplified chained comparison.

5
00:00:23,440 --> 00:00:26,640
When dealing with numerical values in a
condition, you can often replace

6
00:00:26,640 --> 00:00:31,699
and with a simpler comparison. So what I'm
going to do is comment that line out and

7
00:00:31,699 --> 00:00:36,800
we're going to make this a bit simpler.
So instead what we're going to type,

8
00:00:36,800 --> 00:00:46,780
is if 16 less that equal to age less
than equal to 65 :

9
00:00:46,780 --> 00:00:51,940
So I think that's much easier to  understand now because it's obvious that age is in the range

10
00:00:51,940 --> 00:00:57,460
of 16 to 65. The condition is the same as
the previous one but it's phrased in a

11
00:00:57,469 --> 00:01:01,129
more understandable way. We should still
test it, though, so I'm going to run the

12
00:01:01,129 --> 00:01:05,059
program and repeat the inputs that we've
used previously. Alright, so when we ran it

13
00:01:05,059 --> 00:01:11,240
previously we tested a value between 16
and 65. I'll enter 34. That's working okay.

14
00:01:11,240 --> 00:01:15,770
We're getting the message printed out. So next
if we run it again, we tested a value

15
00:01:15,770 --> 00:01:21,640
less than 16 and a value greater than 65.
So I'm going to enter 12 for the first test

16
00:01:21,640 --> 00:01:26,000
and nothing's printed out, which is
correct. Run it again. This time I'm going

17
00:01:26,000 --> 00:01:32,600
to enter 78. No output again, so that's
correct, and the final two tests

18
00:01:32,600 --> 00:01:39,120
were to make sure the code works when we enter 16 and 65. So I'm going to do that, one at a time.

19
00:01:39,120 --> 00:01:46,100
16 works, 65, that also works. So
alright, that's working fine.

20
00:01:46,100 --> 00:01:50,500
We could also turn the condition around or use or
instead. As I've said a few times,

21
00:01:50,500 --> 00:01:54,640
there's often several ways to get the same
result when you're programming. But let's

22
00:01:54,650 --> 00:01:58,220
make a change to the program now so that it prints a message when the condition's

23
00:01:58,220 --> 00:02:04,549
false, in other words, if age is outside of the range 16 to 65. So I'm going to come up end up put

24
00:02:04,549 --> 00:02:11,440
an else in there, so else : and on the next
line indented, print parentheses

25
00:02:11,440 --> 00:02:18,660
and in double quotes I'm going to type Enjoy your free time, like so.

26
00:02:20,120 --> 00:02:23,600
Let's also, on line 9, put a
row of dashes so that we can tell which

27
00:02:23,600 --> 00:02:26,920
block of code has produced which bit
of output. So I'm going to type print

28
00:02:26,920 --> 00:02:32,120
parentheses double quotes and put a dash
in the double quotes, and outside of the

29
00:02:32,120 --> 00:02:40,360
double quotes * 80. Let's have a go at
doing this now, using or instead of and.

30
00:02:40,360 --> 00:02:48,920
So staying on line 11 I'm going to
type if age less than 16 or age

31
00:02:48,920 --> 00:03:01,100
greater than 65 : Then we're going to type print
Enjoy your free time, else

32
00:03:01,100 --> 00:03:08,820
print, on the next line, parentheses and double quotes,
Have a good day at work.

33
00:03:08,820 --> 00:03:13,060
So now if age is less than 16, we're printing the message, Enjoy your free time.

34
00:03:13,060 --> 00:03:18,340
The code will do the same thing if the other part of the condition is true, if age is greater than 65.

35
00:03:18,340 --> 00:03:22,460
Now we're using or on line 11, which
means we'll get that message if

36
00:03:22,460 --> 00:03:27,760
either of those conditions is true, as opposed
to and, where they both needed to be true.

37
00:03:27,760 --> 00:03:32,300
If neither of them is true, we'll get the
message in the else block.

38
00:03:32,300 --> 00:03:40,880
So let's run the program. I'm going to enter 16, and you can see we have a good day at work printed twice.

39
00:03:40,880 --> 00:03:44,060
Both bits of code are
doing the same thing. If you recall, when

40
00:03:44,060 --> 00:03:48,660
we used and, and that's the code on line
4, both the conditions had to be true.

41
00:03:48,660 --> 00:03:53,630
With or, as you can see on line 11, Enjoy
your free time will be printed if either

42
00:03:53,630 --> 00:03:58,180
condition is true. Generally, one way or
the other will be more natural in some

43
00:03:58,190 --> 00:04:02,840
situations and which to use will depend
on the context. There is one important

44
00:04:02,840 --> 00:04:07,240
difference, though, that's useful to be
aware of.

45
00:04:07,240 --> 00:04:11,600
When comparing conditions using and,
Python will stop checking as soon as it

46
00:04:11,600 --> 00:04:16,339
finds a condition that is False. When
comparing conditions using or, it'll stop

47
00:04:16,339 --> 00:04:22,540
as soon as it finds one that's True. So
looking at line 11, if age is less than 16,

48
00:04:22,540 --> 00:04:26,840
then Python doesn't have to check to see
if it's also greater than 65.

49
00:04:26,840 --> 00:04:30,300
This can be very important and we'll be coming back to that later.

50
00:04:30,300 --> 00:04:34,980
For now, it's just something to be aware of. Right, so I'm going to stop the video here and in the next one,

51
00:04:34,980 --> 00:04:40,600
we're going to take a look at
boolean values. See you in the next video.

