1
00:00:00,000 --> 00:00:02,990
There are two different types of conditionals in Python.

2
00:00:03,000 --> 00:00:06,990
There is conditional execution, and there is conditional values.

3
00:00:07,000 --> 00:00:08,990
Let's take a look at what these look like.

4
00:00:09,000 --> 00:00:18,990
We'll make a working copy of syntax.py, call it syntax-conditionals.py.

5
00:00:19,000 --> 00:00:24,990
Go ahead and open our working copy, and we'll start in here by defining a

6
00:00:25,000 --> 00:00:30,990
couple of variables. We'll just call them a and b, and give them values of 0 and 1.

7
00:00:31,000 --> 00:00:47,990
And then we'll say if a < b, and we'll print a is less than b. We can get rid of this line here.

8
00:00:48,000 --> 00:00:53,990
So what we have here - I'm going to save that - is an assignment for assigning two values,

9
00:00:54,000 --> 00:00:58,990
0 and 1, two variables, a and b. And then we're testing if a is less than b, and

10
00:00:59,000 --> 00:01:00,990
so this is a conditional statement.

11
00:01:01,000 --> 00:01:05,990
And it has colon here, and that colon means there is going to be a suite, or a

12
00:01:06,000 --> 00:01:08,990
block of code, that is indented under that level.

13
00:01:09,000 --> 00:01:13,990
And so here we have just a print statement, so that's the only statement in that

14
00:01:14,000 --> 00:01:20,990
suite, and it will only print if a is actually less than b. So if I save this

15
00:01:21,000 --> 00:01:25,990
and run it, we get a is less than b in our result.

16
00:01:26,000 --> 00:01:31,990
If I change this 0 here to a 1, then we will get nothing in our result.

17
00:01:32,000 --> 00:01:34,990
It won't print anything, you see. There's nothing there.

18
00:01:35,000 --> 00:01:40,990
Now, I can handle that exceptional case, and I can say else:

19
00:01:41,000 --> 00:01:49,990
print(a is not less than b), and save that and run it, and now we see a is not

20
00:01:50,000 --> 00:01:56,990
less than b because, in fact a is equal to b. So this is the common if and else

21
00:01:57,000 --> 00:02:06,990
in Python. You also have an else if, which in Python is spelled like this, elif.

22
00:02:07,000 --> 00:02:17,990
And you need another condition, else if a > b, and we'll print a is greater than

23
00:02:18,000 --> 00:02:22,990
b, and then our else. For else, we know that if it's not less than and if it's

24
00:02:23,000 --> 00:02:28,990
not greater than then it is equal, a = b.

25
00:02:29,000 --> 00:02:35,990
So, if we save this and run it, of course we get a = b, and if we make the a a

26
00:02:36,000 --> 00:02:40,990
larger number than the b, and save that and run it, then we get a > b.

27
00:02:41,000 --> 00:02:45,990
So, this is the conditional execution form of conditionals in Python.

28
00:02:46,000 --> 00:02:49,990
We have the if clause, we have the else if, and you can have a string of else

29
00:02:50,000 --> 00:02:50,990
ifs, if you want to.

30
00:02:51,000 --> 00:02:55,990
And we have the else, for if the case where none of the ifs and else ifs

31
00:02:56,000 --> 00:02:58,990
are evaluated to true.

32
00:02:59,000 --> 00:03:02,990
There is another form of conditional in Python, which is the conditional

33
00:03:03,000 --> 00:03:05,990
expression, or the conditional value.

34
00:03:06,000 --> 00:03:09,990
So we'll start with our same assignment (a, b = 0, 1).

35
00:03:10,000 --> 00:03:22,990
We'll say (s = "less than" if a < b else "not less than) and then we'll go

36
00:03:23,000 --> 00:03:26,990
ahead and print(s).

37
00:03:27,000 --> 00:03:31,990
So what this does is this is a conditional expression.

38
00:03:32,000 --> 00:03:36,990
We have a value that's getting assigned only if this condition is true;

39
00:03:37,000 --> 00:03:40,990
otherwise, this other value would be assigned.

40
00:03:41,000 --> 00:03:44,990
You may have seen this in other languages done with question marks and colons,

41
00:03:45,000 --> 00:03:49,990
and the designers of Python felt that that was too obscure and too difficult to

42
00:03:50,000 --> 00:03:53,990
read and decided instead to use syntax that's much easier to read, and in

43
00:03:54,000 --> 00:03:56,990
fact, this is much easier to read.

44
00:03:57,000 --> 00:04:02,990
It's very clear from just looking at this that (s = "less than" if a < b else

45
00:04:03,000 --> 00:04:05,990
"not less than), and otherwise, we'll say "not less than".

46
00:04:06,000 --> 00:04:09,990
Let's go ahead and save it and run it, and we see that we get the word less

47
00:04:10,000 --> 00:04:16,990
than, and if I make this a 1, now it's now less that anymore, and I'll run that,

48
00:04:17,000 --> 00:04:18,990
and it says "not less than".

49
00:04:19,000 --> 00:04:23,990
So that is the conditional value, or the conditional expression in Python.

50
00:04:24,000 --> 00:04:25,990
So Python has two different types of conditionals.

51
00:04:26,000 --> 00:04:31,990
It's got conditional Execution, which is with the traditional if, elif, and else,

52
00:04:32,000 --> 00:04:37,990
and it has conditional expressions, which don't use the question mark and colon

53
00:04:38,000 --> 00:04:42,990
syntax of some other C-based languages, which instead use the same keywords if

54
00:04:43,000 --> 00:04:53,000
and else. It is actually a whole lot easier to read.

