1
00:00:00,000 --> 00:00:05,990
If statements in Python can be extended to test multiple conditions using elif.

2
00:00:06,000 --> 00:00:10,990
Let's go ahead and make a working copy of conditionals.py, call it

3
00:00:11,000 --> 00:00:19,990
conditionals-working.py, and we'll go ahead and open our working copy and

4
00:00:20,000 --> 00:00:21,990
define a variable here.

5
00:00:22,000 --> 00:00:41,990
We'll say if v is == to one, print v is one elif v == two, print v is two, and we could do as many of

6
00:00:42,000 --> 00:00:46,990
these as we wanted to.

7
00:00:47,000 --> 00:00:52,990
So, when we save this and run it, you'll notice we get v is one. And if we change this

8
00:00:53,000 --> 00:00:59,990
one to, say, a three, save that and run it, then we get v is three, but if we make

9
00:01:00,000 --> 00:01:06,990
this a seven, then save that and run it, nothing at all happens.

10
00:01:07,000 --> 00:01:11,990
Now, if you want to have a default case, you can simply say, else:

11
00:01:12,000 --> 00:01:16,990
and this still gets executed if none of the conditions turn out to be true.

12
00:01:17,000 --> 00:01:20,990
Say, print v is some other thing,

13
00:01:21,000 --> 00:01:23,990
and save that and run it, and we get V is some

14
00:01:24,000 --> 00:01:25,990
other thing, because v is seven.

15
00:01:26,000 --> 00:01:31,990
So, the thing to understand about this is that only one of these suites is ever

16
00:01:32,000 --> 00:01:33,990
going to be executed.

17
00:01:34,000 --> 00:01:39,990
So, you have if, and an expression that evaluates to true or false, and a colon,

18
00:01:40,000 --> 00:01:43,990
and a suite, and then with each of the elifs, you also have an expression that

19
00:01:44,000 --> 00:01:47,990
evaluates to true or false, and a colon, and a suite.

20
00:01:48,000 --> 00:01:53,990
Only one of these suites is ever going to get executed, and that's the purpose of elif.

21
00:01:54,000 --> 00:01:58,990
It allows you to have several different conditions that get evaluated, and only

22
00:01:59,000 --> 00:02:01,990
one of them will ever get executed.

23
00:02:02,000 --> 00:02:06,990
And then finally, the else clause is executed in the event that none of the

24
00:02:07,000 --> 00:02:17,000
conditions is true, but again, only one of these suites will ever be executed.

