1
00:00:05,460 --> 00:00:08,320
So now that we can perform calculations
and just play the results,

2
00:00:08,320 --> 00:00:13,230
it's time to move on and see how we
can get Python to make decisions and

3
00:00:13,230 --> 00:00:15,370
also how to perform tasks repeatedly.

4
00:00:15,370 --> 00:00:18,460
Unlike most programming languages
Python works in blocks of code,

5
00:00:18,460 --> 00:00:21,660
and actually one of the design
principals of Python

6
00:00:21,660 --> 00:00:24,110
was that it should be uncluttered and
easy to read.

7
00:00:24,110 --> 00:00:28,410
Because of this Python's doesn't
use delimiters and blocks of code.

8
00:00:28,410 --> 00:00:30,860
And so what I'm going to do is
show you what I mean by that, and

9
00:00:30,860 --> 00:00:32,600
show you the Python solution.

10
00:00:32,600 --> 00:00:37,390
So, I've opened up a new project, and I'm
going to create a new path and file, and

11
00:00:37,390 --> 00:00:42,860
I'm going to call it Tabs, for
reasons that will become apparent shortly.

12
00:00:44,100 --> 00:00:48,460
So firstly, let's just type in an example
in Python on how indentation works.

13
00:00:48,460 --> 00:00:52,860
You saw this in the previous video,
we create a for loop.

14
00:00:52,860 --> 00:01:00,170
So for i in range(1, 12 colon.

15
00:01:00,170 --> 00:01:04,410
And notice when I press enter here,
it's automatically indented by Python.

16
00:01:04,410 --> 00:01:08,210
So python has moved forward four letters,
or four spaces I should say,

17
00:01:08,210 --> 00:01:11,110
so that when we start typing the next
part, it's actually indented.

18
00:01:11,110 --> 00:01:13,290
So this is the concept of indenting.

19
00:01:13,290 --> 00:01:16,120
And I'm just gonna finish typing this,
and I won't go through the code,

20
00:01:16,120 --> 00:01:18,800
because we explained that and
went through that in the previous video

21
00:01:35,500 --> 00:01:36,270
Okay, so and

22
00:01:36,270 --> 00:01:40,760
if we actually run that we have the
answers that we saw in the previous video.

23
00:01:40,760 --> 00:01:45,280
Now a point I wanna make though is
about the use of indentation and

24
00:01:45,280 --> 00:01:49,380
using braces in other
programming languages.

25
00:01:49,380 --> 00:01:54,060
For example with Java, you can type
something and you'd put if something.

26
00:01:55,650 --> 00:01:59,410
And to do the same thing with indentation,
you actually put a brace like so,

27
00:01:59,410 --> 00:02:01,340
and then you'd actually add another brace.

28
00:02:01,340 --> 00:02:03,360
Now obviously we're getting
an error on the screen now,

29
00:02:03,360 --> 00:02:07,560
because IntelliJ is saying,
well this is the valid path and code.

30
00:02:07,560 --> 00:02:10,710
So the point I wanna make is that, you
have to actually denote the code block,

31
00:02:10,710 --> 00:02:13,710
the code that you want executed in
the middle, by using something.

32
00:02:13,710 --> 00:02:15,420
Something actually like a brace.

33
00:02:15,420 --> 00:02:18,100
Well, the deleting this out now in
order to get rid of that error.

34
00:02:18,100 --> 00:02:18,850
Python doesn't do that.

35
00:02:18,850 --> 00:02:23,720
So what Python does, it's quite happy when
you actually start typing a statement,

36
00:02:23,720 --> 00:02:25,480
it's gonna indent that for
you automatically.

37
00:02:25,480 --> 00:02:28,610
It doesn't use delimiters around
the blocks of code, in other words.

38
00:02:28,610 --> 00:02:32,180
Now if you actually delete the spaces,
you'll actually get an error.

39
00:02:32,180 --> 00:02:36,610
So if I should delete those now,
we'll actually get an error on the screen.

40
00:02:36,610 --> 00:02:37,550
Indent expected.

41
00:02:37,550 --> 00:02:41,280
And that's because python is actually
saying, it needs at least one space

42
00:02:41,280 --> 00:02:46,130
to know whether this is part of
the previous line's code in other words.

43
00:02:46,130 --> 00:02:48,260
So in other words,
to indent it needs at least one space.

44
00:02:48,260 --> 00:02:52,590
So we could go back up there and indent
that one space and it will be quite happy.

45
00:02:52,590 --> 00:02:54,200
But generally speaking it will do it for
you.

46
00:02:54,200 --> 00:02:57,040
So again, on the previous line you
still want to start a topping.

47
00:02:57,040 --> 00:03:00,460
When I pressed enter there, it
automatically indented it four spaces, and

48
00:03:00,460 --> 00:03:03,820
if I press delete now I can get that
other code back up there on the screen.

49
00:03:03,820 --> 00:03:07,770
Now both IntelliJ and
Idol make it easy for you by doing that in

50
00:03:07,770 --> 00:03:11,290
default indention when appropriate,
and then also continue the indention.

51
00:03:11,290 --> 00:03:16,640
So if we come back to the end of the line
here, pressed enter, notice that it knows,

52
00:03:16,640 --> 00:03:19,980
because of you're already in an indented
line, it's assuming that you don't need to

53
00:03:19,980 --> 00:03:22,590
be indented again, and we can actually
start topping from that point.

54
00:03:22,590 --> 00:03:26,030
So in other words it's handling a lot
of that for you automatically, and

55
00:03:26,030 --> 00:03:28,430
also pressing backspace,

56
00:03:28,430 --> 00:03:31,370
moves the indent when you're finished
entering the code for a block.

57
00:03:31,370 --> 00:03:32,590
In other words, you can go there.

58
00:03:32,590 --> 00:03:35,700
So if you're on the next line there,
and let's just say you decided,

59
00:03:35,700 --> 00:03:37,100
well, you're gonna need
a little more code.

60
00:03:37,100 --> 00:03:39,260
You wanna go back and
print another statement.

61
00:03:39,260 --> 00:03:44,550
If I press backspace now, I'll go back
automatically four spaces as you saw there

62
00:03:44,550 --> 00:03:47,100
to the start line,
I can actually start typing again.

63
00:03:47,100 --> 00:03:50,100
Now you might be asking, now what
is this block you're talking about?

64
00:03:50,100 --> 00:03:53,590
Well, Python actually
referred to that as being

65
00:03:53,590 --> 00:03:57,210
a piece of Python program text
that is executed as a unit.

66
00:03:57,210 --> 00:04:00,920
So in the example above, you can actually
say that print itself was a call to

67
00:04:00,920 --> 00:04:04,130
the printer method to be more precise,
is a block.

68
00:04:04,130 --> 00:04:07,390
Now we could have included some extra
lines at the same level of indentation,

69
00:04:07,390 --> 00:04:09,260
and they'd become part of the same block.

70
00:04:09,260 --> 00:04:16,110
So in other words, you could actually
start typing, print, calculation complete.

71
00:04:19,050 --> 00:04:20,910
Try again, for example.

72
00:04:20,910 --> 00:04:24,820
All of these are actually part,
in this case, of the same block.

73
00:04:24,820 --> 00:04:27,450
The reason for that is they're at
the same level of indentation.

74
00:04:27,450 --> 00:04:30,260
Now when we get to the point in this
course where we're defining our own

75
00:04:30,260 --> 00:04:34,070
functions or methods,
the body of the function is a block, and

76
00:04:34,070 --> 00:04:37,140
that may in turn contain other blocks.

77
00:04:37,140 --> 00:04:41,250
Now we actually need some more complex
code examples to demonstrate this fully,

78
00:04:41,250 --> 00:04:45,130
so what I'm going to do now is finish by
showing you some of the preferences in

79
00:04:45,130 --> 00:04:49,420
IntelliJ to do with tabs, and then in the
next lecture we'll start getting into some

80
00:04:49,420 --> 00:04:51,270
more complex code examples.

81
00:04:51,270 --> 00:04:55,910
So if you actually want to change
the level of tabs or spaces

82
00:04:55,910 --> 00:04:59,830
the way to do it is to go into Preferences
IntelliJ so I'm going to select that.

83
00:04:59,830 --> 00:05:02,800
Preferences and we can go into Editor.

84
00:05:02,800 --> 00:05:07,300
And because we've actually installed
the Python plug-in you should

85
00:05:07,300 --> 00:05:08,090
actually see that.

86
00:05:08,090 --> 00:05:11,420
So, let me go into General and
scroll down into Code Style.

87
00:05:11,420 --> 00:05:16,220
We can actually see Python,
so I'm gonna select python.

88
00:05:16,220 --> 00:05:17,100
Once I've actually done that, so

89
00:05:17,100 --> 00:05:19,660
again that's Editor, Code Style,
Python is how to get to it.

90
00:05:19,660 --> 00:05:22,070
You'll notice here that
we've actually got tab size.

91
00:05:22,070 --> 00:05:24,670
The python folder is showing
you that the tab size is 4.

92
00:05:24,670 --> 00:05:27,090
And what that means is if
you press the tab key, and

93
00:05:27,090 --> 00:05:30,840
that's to get this little indentation,
if you do that yourself,

94
00:05:30,840 --> 00:05:33,800
every time you do that's gonna add
four spaces for you automatically.

95
00:05:33,800 --> 00:05:35,460
Now if you click on
this Use tab character,

96
00:05:35,460 --> 00:05:38,900
what will happen if we do that,
notice how this has changed now and

97
00:05:38,900 --> 00:05:41,870
it's flashing to show you that now
this is actually a tab character.

98
00:05:41,870 --> 00:05:45,700
So what that saying is instead of actually
adding four spaces for you, it's actually

99
00:05:45,700 --> 00:05:48,620
gonna add the tab character itself for
you if that's what you wanna do.

100
00:05:48,620 --> 00:05:52,220
So you can play around with this,
you can change the level of indentation,

101
00:05:52,220 --> 00:05:55,990
you can get yourself a little
bit of difficulty doing that.

102
00:05:55,990 --> 00:05:58,020
If you do find that things aren't working.

103
00:05:58,020 --> 00:06:02,080
Come here back in editor,
code style python, click on reset, or

104
00:06:02,080 --> 00:06:05,200
just make sure that your tab size and
indent is both set to 4.

105
00:06:05,200 --> 00:06:07,600
And optionally you can leave that on, but

106
00:06:07,600 --> 00:06:09,530
I'll probably suggest that
you actually leave it off.

107
00:06:09,530 --> 00:06:13,490
And what that means is that IntelliJ will
automatically be adding spaces, as and

108
00:06:13,490 --> 00:06:14,750
when you actually need them.

109
00:06:14,750 --> 00:06:17,450
So, that's what you need to do
if you do get in any difficulty.

110
00:06:17,450 --> 00:06:19,330
And just to confirm how that works again.

111
00:06:19,330 --> 00:06:20,150
Because we added four,

112
00:06:20,150 --> 00:06:23,870
every time I press Tab,
you could see I'm going four characters.

113
00:06:23,870 --> 00:06:27,330
And that's because we've specified four
in the preferences that the number of

114
00:06:27,330 --> 00:06:29,760
characters that were gonna move forward.

115
00:06:29,760 --> 00:06:31,410
And just one final note.

116
00:06:31,410 --> 00:06:34,100
You don't wanna get into the situation
where you're mixing spaces and

117
00:06:34,100 --> 00:06:35,800
tabs when you're indenting blocks.

118
00:06:35,800 --> 00:06:37,810
And the reason for
it is it can get in the difficulty and

119
00:06:37,810 --> 00:06:39,120
it can get a little bit confusing.

120
00:06:39,120 --> 00:06:41,630
And IntelliJ does a pretty
good job at that.

121
00:06:41,630 --> 00:06:45,930
So if you do do that, chances are, it's
gonna fix that and correct that for you.

122
00:06:45,930 --> 00:06:49,170
But if you're bringing in source code or
editing in another environment and

123
00:06:49,170 --> 00:06:52,890
then bring it into IntelliJ you might
run into a bit of difficulties, and

124
00:06:52,890 --> 00:06:54,330
you may have to re-fix things up.

125
00:06:54,330 --> 00:06:58,040
Also the other thing to keep in mind is,
you come up here to Code.

126
00:06:58,040 --> 00:06:59,730
Try doing reformat code.

127
00:06:59,730 --> 00:07:02,430
That can actually help you
if you actually select that.

128
00:07:02,430 --> 00:07:04,170
That will often actually fix errors.

129
00:07:04,170 --> 00:07:05,830
So if you've got some weird
sort of formatting and

130
00:07:05,830 --> 00:07:09,380
things not working, go into other
option and click reformat code and

131
00:07:09,380 --> 00:07:11,650
that can get you out of
difficulties as well.

132
00:07:11,650 --> 00:07:14,070
So that's it for this video,
short, sharp, and shining.

133
00:07:14,070 --> 00:07:15,620
In the next one, as I mentioned,

134
00:07:15,620 --> 00:07:19,180
we're gonna move on to some more
complex examples of how this all works.

135
00:07:19,180 --> 00:07:21,880
And specifically we're gonna start
looking at the if statements.

136
00:07:21,880 --> 00:07:23,290
So I'll see you in that next video.

