1
00:00:00,000 --> 00:00:05,990
Conditional execution in Python is handled with the If statement.

2
00:00:06,000 --> 00:00:12,990
So, let's go ahead and make a working copy of conditionals.py, call it

3
00:00:13,000 --> 00:00:19,990
conditionals-working.py, and we'll Open that up and start by just defining a

4
00:00:20,000 --> 00:00:21,990
couple of variables.

5
00:00:22,000 --> 00:00:23,990
a and b equals 0 and 1.

6
00:00:24,000 --> 00:00:32,990
If we want to just execute some code, if a is less than b, all we do is we type if a < b:

7
00:00:33,000 --> 00:00:34,990
print ('this is true').

8
00:00:35,000 --> 00:00:41,990
Now, when I save that and run it, you see that 'this is true' got printed.

9
00:00:42,000 --> 00:00:45,990
So, this print statement executed, and the way this works is the if keyword

10
00:00:46,000 --> 00:00:49,990
introduces the conditional, and the expression here, a is less than b, and that

11
00:00:50,000 --> 00:00:55,990
could be any expression that evaluates to True or False and a colon introduces

12
00:00:56,000 --> 00:00:58,990
the suite of code and so this could be a suite of code.

13
00:00:59,000 --> 00:01:03,990
It could be a number of things, and that gets executed if this

14
00:01:04,000 --> 00:01:04,990
expression evaluates to true.

15
00:01:05,000 --> 00:01:09,990
If the expression does not evaluate to true - I'll just change this 0 to 1, and a

16
00:01:10,000 --> 00:01:14,990
is no longer less than b, save that and run it - then nothing happens at all.

17
00:01:15,000 --> 00:01:20,990
So, if we want something different to happen, if the expression is false, then I

18
00:01:21,000 --> 00:01:28,990
can simply say else and a colon and another suite of code, it is not True.

19
00:01:29,000 --> 00:01:34,990
So, if I save that and run it, then we get that here it is not true, and if I

20
00:01:35,000 --> 00:01:39,990
make this 1 a 0 again, save and run, then we get 'this is true.'

21
00:01:40,000 --> 00:01:43,990
So, this expression can actually be any expression that evaluates to true or

22
00:01:44,000 --> 00:01:50,990
false, and so I could just actually put the word True there, and it'll be true,

23
00:01:51,000 --> 00:01:58,990
save and run, this is true, or I can put the word False there, and save and run,

24
00:01:59,000 --> 00:02:00,990
and we get, 'it is not true.'

25
00:02:01,000 --> 00:02:05,990
So, that is the if and else in Python.

26
00:02:06,000 --> 00:02:09,990
If is a keyword, and then it's followed by any expression that evaluates true or

27
00:02:10,000 --> 00:02:15,990
false and a colon and then a suite of code, and the optional else will execute

28
00:02:16,000 --> 00:02:26,000
its suite of code, if the condition is false and not true.

