1
00:00:00,000 --> 00:00:03,990
So, continuing with our Python Quick Start, let's take a look at how

2
00:00:04,000 --> 00:00:05,990
conditionals work in Python.

3
00:00:06,000 --> 00:00:10,990
I am going ahead and make a copy of this conditionals.py, and it's a good idea

4
00:00:11,000 --> 00:00:12,990
to work with copies.

5
00:00:13,000 --> 00:00:23,990
I am just going to name this conditionals -working. We'll go ahead and open that.

6
00:00:24,000 --> 00:00:30,990
There we have an example of a conditional, a very simple conditional. It's if and else.

7
00:00:31,000 --> 00:00:32,990
And you'll notice that the assignment - it's really cool.

8
00:00:33,000 --> 00:00:37,990
In Python, you can assign multiple values at a time, so this assigns a 0 to a and

9
00:00:38,000 --> 00:00:41,990
assigns 1 to b. So that makes a nice little test for the conditional. If a is

10
00:00:42,000 --> 00:00:45,990
less than b, which it is, then it will print this.

11
00:00:46,000 --> 00:00:50,990
You'll also notice, in Python, you do the replacement, and this is Python 3;

12
00:00:51,000 --> 00:00:52,990
Python 2 works differently.

13
00:00:53,000 --> 00:00:57,990
In Python 3, these curly braces will be replaced by the values in the format, and

14
00:00:58,000 --> 00:01:02,990
we'll get more into how this works exactly, and some of the details of it, later

15
00:01:03,000 --> 00:01:05,990
on when we talk about strings.

16
00:01:06,000 --> 00:01:11,990
But in fact this format is a method of the string object.

17
00:01:12,000 --> 00:01:14,990
Any place where you have a string, it allows you to do these kinds of

18
00:01:15,000 --> 00:01:17,990
replacements; it's very useful.

19
00:01:18,000 --> 00:01:23,990
So, when we run this - and we'll get this one because a is less than b, and unless

20
00:01:24,000 --> 00:01:25,990
we were to change these values and then maybe we could get this one.

21
00:01:26,000 --> 00:01:30,990
So let's go ahead and run it, and I'll run it here in Eclipse, and I'll select

22
00:01:31,000 --> 00:01:33,990
Python Run, and there we go.

23
00:01:34,000 --> 00:01:37,990
a is less than b, and see there is the value 0 and 1.

24
00:01:38,000 --> 00:01:43,990
If I change this, if I make this say a 5, and I save that and run it, a is not

25
00:01:44,000 --> 00:01:45,990
less than b anymore.

26
00:01:46,000 --> 00:01:52,990
So the syntax of the conditional is, if, and then you have the condition, and you have a colon.

27
00:01:53,000 --> 00:01:57,990
This is how blocks work in Python. You'll see this same type of syntax and

28
00:01:58,000 --> 00:02:05,990
loops and functions and classes, where you have the colon and then you have code that's indented.

29
00:02:06,000 --> 00:02:09,990
There aren't any braces signifying a block of code.

30
00:02:10,000 --> 00:02:14,990
Blocks are actually called suites in the Python documentation, and they

31
00:02:15,000 --> 00:02:19,990
simply consist of code that is indented under the level of whatever it is

32
00:02:20,000 --> 00:02:20,990
that's controlling it.

33
00:02:21,000 --> 00:02:23,990
So here we are controlling it with a conditional, and we have the colon, and then

34
00:02:24,000 --> 00:02:25,990
we have this code indented.

35
00:02:26,000 --> 00:02:30,990
Now four spaces of indenting is considered standard in Python.

36
00:02:31,000 --> 00:02:31,990
It's actually not required.

37
00:02:32,000 --> 00:02:37,990
I can make this one space and do the same down here and it would still work,

38
00:02:38,000 --> 00:02:42,990
save and run, but four is traditional.

39
00:02:43,000 --> 00:02:47,990
So we are just going to go ahead and go with the tradition here and save that

40
00:02:48,000 --> 00:02:50,990
and run it, and there we have it.

41
00:02:51,000 --> 00:02:54,990
Python also has conditional expressions. If you are familiar with any C-based

42
00:02:55,000 --> 00:02:57,990
languages, you'll be familiar with something that might look like this, where you

43
00:02:58,000 --> 00:03:07,990
would say print, and you would have a condition a < b and a question mark, and then one value, and a colon,

44
00:03:08,000 --> 00:03:09,990
and then another value.

45
00:03:10,000 --> 00:03:12,990
If the condition were true, you would get the first one; if the condition were

46
00:03:13,000 --> 00:03:14,990
false, you would get the second one.

47
00:03:15,000 --> 00:03:17,990
Python has something like this, but it works very differently.

48
00:03:18,000 --> 00:03:25,990
Here, you would say "foo" if a < b else "bar".

49
00:03:26,000 --> 00:03:32,990
If I save that and run it, then we get bar because a is not less than b. If I put

50
00:03:33,000 --> 00:03:37,990
this back to a 0, like we had it originally, and save and run,

51
00:03:38,000 --> 00:03:41,990
now we get foo because a is less than b. So this is how conditional

52
00:03:42,000 --> 00:03:44,990
expressions work in Python.

53
00:03:45,000 --> 00:03:49,990
A lot of the syntax that you will see here is a little bit different than in

54
00:03:50,000 --> 00:03:51,990
other C-based languages.

55
00:03:52,000 --> 00:03:55,990
And that's because the designers of Python, they didn't consider themselves be

56
00:03:56,000 --> 00:04:00,990
beholden to tradition, and instead they wanted to do things that seem to flow

57
00:04:01,000 --> 00:04:03,990
better or make sense in some way.

58
00:04:04,000 --> 00:04:08,990
And actually, this does have its own set of reason in that you have print "foo"

59
00:04:09,000 --> 00:04:11,990
just like you would have if you didn't have a condition.

60
00:04:12,000 --> 00:04:15,990
You only really care about having the condition if you have another value.

61
00:04:16,000 --> 00:04:21,990
So you have this whole if-else all in one place, and it's spelled out; it's not

62
00:04:22,000 --> 00:04:27,990
punctuation marks that might just be hard to remember or hard to read.

63
00:04:28,000 --> 00:04:32,990
It's very clear: "foo" if a < b else "bar"; it's kind of readable.

64
00:04:33,000 --> 00:04:36,990
So that is how conditions work in Python 3.

65
00:04:37,000 --> 00:04:47,000
You have the traditional if-else, and you have the conditional expressions.

