1
00:00:00,000 --> 00:00:03,990
Whitespace is significant in Python in ways that you may not be accustomed to,

2
00:00:04,000 --> 00:00:07,990
if you are familiar with other scripting languages.

3
00:00:08,000 --> 00:00:11,990
Let's go ahead and make a working copy of syntax.py, and we can look at

4
00:00:12,000 --> 00:00:19,990
whitespace in Python. Call it syntax-whitespace.py.

5
00:00:20,000 --> 00:00:25,990
Go ahead and open that up, and you will notice that this main function consists

6
00:00:26,000 --> 00:00:29,990
of one statement, a print statement, and that print statement is indented four

7
00:00:30,000 --> 00:00:36,990
spaces, ands that actually tells Python that this print statement is associated

8
00:00:37,000 --> 00:00:39,990
with this main function.

9
00:00:40,000 --> 00:00:42,990
It's actually the body of the main function.

10
00:00:43,000 --> 00:00:44,990
Four spaces are traditional in Python.

11
00:00:45,000 --> 00:00:48,990
You can use two. You can use one. It will work just as well.

12
00:00:49,000 --> 00:00:53,990
It does have to be consistent. In other words, if I have more than one line here,

13
00:00:54,000 --> 00:01:05,990
and if that other line is indented, say three spaces instead of four, if I save

14
00:01:06,000 --> 00:01:10,990
that and run it, you will see that I get a syntax error, and it says unindent

15
00:01:11,000 --> 00:01:13,990
does not match any outer indentation level.

16
00:01:14,000 --> 00:01:18,990
So Python got confused. We indented this one four spaces, we indented this one

17
00:01:19,000 --> 00:01:21,990
three spaces and it just got confused.

18
00:01:22,000 --> 00:01:26,990
It didn't know what to do with that. So the indention has to be the same amount.

19
00:01:27,000 --> 00:01:29,990
Save that and run it, and we will see that that works.

20
00:01:30,000 --> 00:01:34,990
Now if I take this second line and I indent it back at the same level as the

21
00:01:35,000 --> 00:01:39,990
function definition, then it is no longer part of the body of that function.

22
00:01:40,000 --> 00:01:43,990
So what will happen when I run this is it will actually see this line, this is

23
00:01:44,000 --> 00:01:49,990
another line, before the syntax file and the reason for that is because that will

24
00:01:50,000 --> 00:01:52,990
get executed on the first pass through here before we call main.

25
00:01:53,000 --> 00:01:55,990
main gets called at the end of the file.

26
00:01:56,000 --> 00:01:59,990
So this line will be printed first and then this line.

27
00:02:00,000 --> 00:02:04,990
So we will save that, and we will run it, and you see this is another line, and

28
00:02:05,000 --> 00:02:06,990
then this is the syntax file.

29
00:02:07,000 --> 00:02:12,990
So, by bringing the indentation out to the same level as the definition, it is

30
00:02:13,000 --> 00:02:14,990
now no longer part of the body of that function.

31
00:02:15,000 --> 00:02:20,990
We will go ahead and indent that again and save it and run it, and we see that it

32
00:02:21,000 --> 00:02:24,990
works as we expect with this second line after the first line.

33
00:02:25,000 --> 00:02:30,990
So indentation as whitespace is significant in Python because Python doesn't use

34
00:02:31,000 --> 00:02:37,990
curly braces, or any other characters, to indicate the body of a function, or what

35
00:02:38,000 --> 00:02:41,990
they a call suite - what we might call a block of code that's - associated with a

36
00:02:42,000 --> 00:02:42,990
particular control structure.

37
00:02:43,000 --> 00:02:47,990
These indents are used for conditionals. They are used for classes.

38
00:02:48,000 --> 00:02:51,990
They are used for any place where you might need to associate a block of text

39
00:02:52,000 --> 00:02:53,990
with a particular control structure.

40
00:02:54,000 --> 00:02:58,990
Now there is one circumstance where the indentation and the whitespace is not

41
00:02:59,000 --> 00:02:59,990
actually significant.

42
00:03:00,000 --> 00:03:05,990
If you have just one line of code in a particular structure - and that's all there

43
00:03:06,000 --> 00:03:10,990
is, there is not more than one line of code - that one line of code can be on the same line

44
00:03:11,000 --> 00:03:12,990
as the control structure itself.

45
00:03:13,000 --> 00:03:16,990
So if I put this print here - and I can put a space, or not put a space, or put several

46
00:03:17,000 --> 00:03:18,990
spaces - it will work exactly the same.

47
00:03:19,000 --> 00:03:24,990
I save this and run it, you see that it still works. Just like this 'if'

48
00:03:25,000 --> 00:03:29,990
statement down here has one line of code, which is the function call for main,

49
00:03:30,000 --> 00:03:34,990
now this function definition for main has just one line of code, the print, and

50
00:03:35,000 --> 00:03:36,990
in this case the whitespace is not significant.

51
00:03:37,000 --> 00:03:41,990
I can have five spaces, I can have one space, and it still runs just the same.

52
00:03:42,000 --> 00:03:45,990
That's a special case for whether its just one line of code in a

53
00:03:46,000 --> 00:03:48,990
particular structure.

54
00:03:49,000 --> 00:03:52,990
Under most circumstances, where you have a function, or a class, or something like

55
00:03:53,000 --> 00:03:57,990
that, and you have more than one line of code, you will see the indentation used,

56
00:03:58,000 --> 00:04:03,990
and four spaces is traditional, and the indentation has to be consistent all the

57
00:04:04,000 --> 00:04:14,000
way through the block or the suite of code.

