1
00:00:04,680 --> 00:00:09,629
Now that we can perform calculations and
display the results, it's time to move on

2
00:00:09,629 --> 00:00:15,940
to see how we can get Python to make
decisions and perform tasks repeatedly.

3
00:00:15,940 --> 00:00:20,440
Like many programming languages, Python
works in blocks of code. One of the

4
00:00:20,440 --> 00:00:23,960
design principles of Python, though, was
that it should be uncluttered and

5
00:00:23,960 --> 00:00:29,540
easy to read. Because of this, Python doesn't use
delimiters around blocks of code.

6
00:00:29,540 --> 00:00:33,040
If you've used other programming languages,
you may be used to seeing braces,

7
00:00:33,080 --> 00:00:37,980
that's left and right curly braces, or BEGIN and
END around code. Python doesn't use

8
00:00:37,980 --> 00:00:45,200
anything like that, but instead uses
indentation to indicate when a new block starts.

9
00:00:45,200 --> 00:00:48,800
I've opened the exchange_chart program in IntelliJ

10
00:00:48,800 --> 00:00:53,040
so that I've got some code to talk about.
Now you won't need this file in your project.

11
00:00:53,040 --> 00:00:56,000
We're not going to be running it
in this section. This is one of the few

12
00:00:56,000 --> 00:00:59,700
times in the course when you won't be
typing along. So relax, sit back and

13
00:00:59,700 --> 00:01:03,880
watch the video, as l show you blocks
of code in this program.

14
00:01:03,880 --> 00:01:09,540
Alright, so let's look at the blocks of code in this
program, firstly, starting on line 66,

15
00:01:09,540 --> 00:01:14,620
we've got a for loop. That starts a new
code block and we can see that line 67

16
00:01:14,620 --> 00:01:18,120
through to lines 82, which is just off
screen, well it's actually on-screen but

17
00:01:18,120 --> 00:01:23,910
I'll move up a little bit more, like so.
So you can see those lines 67 to 82,

18
00:01:23,910 --> 00:01:29,480
they're all part of that code block,
which is the for loop defined on line 66.

19
00:01:29,480 --> 00:01:32,550
Now I don't want you to try and
understand the code here, because most of

20
00:01:32,550 --> 00:01:36,120
it probably won't make sense at this
point in the course. You should be able

21
00:01:36,120 --> 00:01:41,010
to work out that line 67, though, is using
replacement fields, and I'll just move that

22
00:01:41,010 --> 00:01:45,810
over a little bit more so we can see it,
and that's using a replacement field to

23
00:01:45,810 --> 00:01:49,890
put the year and month at the end of
that URL. But most of this code in

24
00:01:49,890 --> 00:01:55,020
general will be confusing, and that's
fine. What we're interested in now at the

25
00:01:55,020 --> 00:01:59,640
moment are the various levels of
indentation. So concentrate on the shape

26
00:01:59,640 --> 00:02:04,380
of the code and we'll learn what it all
means in due course. Alright, so we've got

27
00:02:04,380 --> 00:02:10,979
a block of code from line 67, through to
and including lines 82. We can make it

28
00:02:10,979 --> 00:02:16,650
easier to see the blocks by turning on
indentation guides. Now most modern IDEs

29
00:02:16,650 --> 00:02:21,040
will have a similar feature.
In IntelliJ, you go into Settings.

30
00:02:21,040 --> 00:02:25,180
Now that's Preferences if you're using a Mac.
In my case, I'm on a Windows machine,

31
00:02:25,180 --> 00:02:30,500
and it'll be the same for Linux, File, I'm
going to select Settings.

32
00:02:30,500 --> 00:02:38,760
You want to then navigate to Editor, then down to
General and then here into Appearance.

33
00:02:38,760 --> 00:02:42,700
You want to make sure that the Show
indent guides is actually checked, so I'm

34
00:02:42,700 --> 00:02:47,480
going to check that now. Click OK,

35
00:02:47,480 --> 00:02:50,460
and you can see now that IntelliJ draws guides

36
00:02:50,460 --> 00:02:54,800
to show the indentation levels. You can
see that slight, really gray, light gray

37
00:02:54,800 --> 00:03:02,260
line now in the indentation. So line 67
and line 69 are indented one level.

38
00:03:02,260 --> 00:03:09,720
So below that, line 70 is indented another
level, line 72 is also at that second level

39
00:03:09,720 --> 00:03:14,300
and so are all the lines 74 to
79, and you can see there's a third level

40
00:03:14,300 --> 00:03:21,540
of indentation on lines 80 through 82. So
each level of indentation is a new block of code.

41
00:03:21,540 --> 00:03:25,680
So let's see them again. The main
code has no indentation and starts

42
00:03:25,690 --> 00:03:30,340
up against the left margin. Lines 67 to 82
are all part of another block of code,

43
00:03:30,340 --> 00:03:35,980
and are indented one level. The
convention is to indent four spaces at a time.

44
00:03:35,980 --> 00:03:38,859
Within that second block we have
other blocks for lines

45
00:03:38,860 --> 00:03:47,560
70 and 72 and also lines 74 to 82, then
finally there's a fourth block, lines 80 to 82.

46
00:03:47,560 --> 00:03:50,900
So that's how we create blocks of
code in Python.

47
00:03:50,900 --> 00:03:54,430
Now I haven't explained why you want to
do that. At the moment we're looking at

48
00:03:54,430 --> 00:03:59,700
what a code block is. So as you can see, a
block of code can contain other blocks.

49
00:03:59,700 --> 00:04:03,780
We start a new code block by indenting
the code. Now have you noticed anything

50
00:04:03,780 --> 00:04:06,860
else about the lines that start a new
code block?

51
00:04:06,860 --> 00:04:15,520
Well lines 66, 69, 71, 73 and 79 all start a new code block.
If you examine those lines carefully,

52
00:04:15,520 --> 00:04:18,060
hopefully you can spot something
that they all have in common.

53
00:04:18,060 --> 00:04:24,920
Did you spot it? Well all those lines end
in a colon. Whenever a line ends with a colon,

54
00:04:24,920 --> 00:04:27,840
Python will expect a new code block to follow it.

55
00:04:27,840 --> 00:04:31,510
That means that the next line must
be indented. When you've finished

56
00:04:31,510 --> 00:04:34,620
all the code for a block,
you remove the indentation

57
00:04:34,620 --> 00:04:40,420
for the next line. The blocks of code on
lines 67 to 82 finished with line 82,

58
00:04:40,460 --> 00:04:43,320
because the indentation for the next
line is removed, and you can see that

59
00:04:43,320 --> 00:04:48,630
line 84, right towards the bottom of
the screen, isn't indented. Line 72 is a

60
00:04:48,630 --> 00:04:54,360
code block containing only one line.
Line 73 is indented back one level.

61
00:04:54,360 --> 00:04:58,100
There's another code block a bit further down
and I'll scroll that so you can see it,

62
00:04:58,100 --> 00:05:03,700
and the the block of code on lines
87 to 88. You can see those there.

63
00:05:03,700 --> 00:05:08,660
Only those two lines are indented, which means only
those two lines are part of the for loop.

64
00:05:08,660 --> 00:05:12,840
Once again, don't worry about the code,
just concentrate on how indentation is

65
00:05:12,840 --> 00:05:17,560
used to create a new code block. We're
learning why in the rest of this section.

66
00:05:17,560 --> 00:05:21,580
Alright so that's the end of the
demonstration and what code blocks are.

67
00:05:21,580 --> 00:05:25,980
Next we'll see how to create them. So I've
created a new project for this section

68
00:05:25,980 --> 00:05:30,320
and I've called the project ProgramFlow,
which probably isn't very imaginative.

69
00:05:30,320 --> 00:05:32,960
You may want to call your project
something else, like flow control or

70
00:05:32,960 --> 00:05:36,260
program flow or something of that nature,
or something that makes sense. I'm going

71
00:05:36,260 --> 00:05:41,000
to close this file now because we are
done with it for now. Close it down.

72
00:05:41,000 --> 00:05:43,940
So what I'm going to do is click on my
project to open it. I'm going to right

73
00:05:43,940 --> 00:05:51,700
click it, select New, Python file and
let's go with the name, blocks.

74
00:05:51,740 --> 00:05:56,190
Alright and I'll just close down the project pane to
make a bit of space. Now I'm going to start with

75
00:05:56,190 --> 00:06:00,240
some code that we've seen before. It
still won't make sense but it will let us

76
00:06:00,240 --> 00:06:07,740
see how to create a code block. So I'm
going to type on the first line, for i in range

77
00:06:07,740 --> 00:06:14,010
parentheses 1 comma 13 right
parentheses and a colon. Now when I type

78
00:06:14,010 --> 00:06:19,140
that colon, IntelliJ IDEA is showing an
error. If I hover the mouse over it,

79
00:06:19,140 --> 00:06:23,720
you can see a description that says indent
expected. So if I then press ENTER here,

80
00:06:23,720 --> 00:06:28,640
you see that the error has now moved to the
next line, and that can be very confusing.

81
00:06:28,640 --> 00:06:33,120
You've hardly typed anything and you've
already got an error. Don't worry,

82
00:06:33,120 --> 00:06:36,500
you will sometimes get things like this
until you finish typing the code.

83
00:06:36,500 --> 00:06:40,680
IntelliJ's trying to make sense of
code that isn't finished yet, and it gets

84
00:06:40,680 --> 00:06:46,000
a bit confused, and most IDEs will do
the same thing if they provide syntax checking

85
00:06:46,000 --> 00:06:48,860
while your type, like IntelliJ is doing.

86
00:06:48,860 --> 00:06:52,600
Helpfully though, IntelliJ has
automatically indented that new line,

87
00:06:52,600 --> 00:06:56,820
when I pressed enter. So I'm going to start
typing print. You can see there that was

88
00:06:56,820 --> 00:07:02,060
already indented for us. I'm gonna finish
this line, print left parenthesis,

89
00:07:02,060 --> 00:07:11,300
double quotes, No, we'll put number, or No for short,
period, then left and right curly braces,

90
00:07:11,300 --> 00:07:16,840
squared is, and left and right curly
braces again,

91
00:07:16,840 --> 00:07:20,540
and cubed is.

92
00:07:20,540 --> 00:07:24,760
Then we want our left and right curly braces again, but within those we're going to put : 4.

93
00:07:24,760 --> 00:07:29,780
We have talked about this in a previous section,
finishing off the double quote and put

94
00:07:29,780 --> 00:07:41,460
dot format parentheses i comma i ** 2 comma i ** 3

95
00:07:41,460 --> 00:07:47,160
and two right parentheses to finish the line. Note that
that's now cleared the error, and notice

96
00:07:47,160 --> 00:07:51,260
also that the cursor is automatically
positioned when I press ENTER now,

97
00:07:51,260 --> 00:07:55,580
at the same level of indentation.
Now if we wanted more code inside the loop,

98
00:07:55,580 --> 00:07:59,700
we just keep typing. There's only one line I
want in this block, so we're going to use

99
00:07:59,700 --> 00:08:04,040
the backspace key now to delete the
indentation, and you can see just one

100
00:08:04,040 --> 00:08:08,100
backspace went right back to the start.
Now if you want to indent again,

101
00:08:08,100 --> 00:08:12,800
press the tab key.
Notice that the IDE adds four spaces

102
00:08:12,800 --> 00:08:17,700
and removes four spaces, when I use
backspace again. So it knows, in other words,

103
00:08:17,700 --> 00:08:20,580
that we're in a code block and
automatically adds the correct number of

104
00:08:20,580 --> 00:08:25,500
spaces for us. Now you can also select
a line of code and use shift tab to

105
00:08:25,500 --> 00:08:31,220
remove a level of indentation. To see
what I mean I'm going up to line 2.

106
00:08:31,220 --> 00:08:35,820
I'm gonna hold down shift and press tab.
I can indent it again with tab,

107
00:08:35,820 --> 00:08:41,059
shift tab to remove the indentation and tab again to edit.
Alright, so moving on to the

108
00:08:41,059 --> 00:08:48,720
next line now, line 3, I'm going to
type print parentheses double quotes,

109
00:08:48,720 --> 00:08:55,880
and put an asterix in the double quotes, then * 80, right parenthesis.

110
00:08:55,880 --> 00:08:58,600
Now if I run this program,

111
00:09:02,940 --> 00:09:07,300
we can see we get the squares and cubes
printed, followed by a line of asterixes.

112
00:09:07,300 --> 00:09:12,030
So the block of code inside that for
loop executes 12 times. but we only get a

113
00:09:12,030 --> 00:09:17,980
single line of asterixes at the end, and that's
because line 3 isn't part of the block.

114
00:09:17,980 --> 00:09:22,020
However, we can make it part of the
block by tabbing it across. So come over

115
00:09:22,020 --> 00:09:26,610
here on line 3 and tab it over, so it's
now indented at the same line or same

116
00:09:26,610 --> 00:09:31,580
level as line 2. If we run the program again,

117
00:09:31,580 --> 00:09:34,760
notice that we get a very different result this time.

118
00:09:34,760 --> 00:09:37,660
The output's very different, as you can see there.

119
00:09:37,660 --> 00:09:41,040
Both lines are now part of the same
block and therefore both lines get

120
00:09:41,040 --> 00:09:45,600
executed 12 times. Alright, so that's
how we create a code block using the tab

121
00:09:45,600 --> 00:09:50,400
key to indent the code. We're also
starting to see why we might want to do that.

122
00:09:50,400 --> 00:09:53,580
We get very different results when
we include line 3 in the block,

123
00:09:53,580 --> 00:09:57,560
compared to having it outside of the block.
The code still won't make a lot of sense.

124
00:09:57,560 --> 00:10:01,280
What are for and range? Well that's some of the
things we'll be looking at in the rest

125
00:10:01,280 --> 00:10:04,740
of this section, but now that we
understand what code blocks are,

126
00:10:04,740 --> 00:10:08,340
we can start to use them in our code.
We'll make a start on that when we look at

127
00:10:08,340 --> 00:10:13,160
telling Python how to make decisions, in the next video.

