1
00:00:04,700 --> 00:00:09,840
In the last video, I ended by talking
about the term Augmented assignment

2
00:00:09,840 --> 00:00:14,660
IntelliJ and PyCharm highlight errors in
our code. They also show warnings

3
00:00:14,660 --> 00:00:19,720
sometimes, to indicate possible problems
with our code, even if it compiles

4
00:00:19,720 --> 00:00:24,240
successfully. The third thing it can flag
up are suggestions. These are ways that

5
00:00:24,240 --> 00:00:28,440
we can improve our code, and you should
generally pay attention to them. Now that

6
00:00:28,440 --> 00:00:31,920
doesn't mean you have to accept them -
it's your code, after all  - but most of the

7
00:00:31,920 --> 00:00:35,550
time, the suggestions will improve your
code and are based on best programming

8
00:00:35,550 --> 00:00:41,250
practices. Now I can't show you this
particular suggestion, but if you're on a Mac or

9
00:00:41,250 --> 00:00:45,510
Linux machine, most likely when you hover
over line 27, it'll say assignment can

10
00:00:45,510 --> 00:00:50,399
be replaced with augmented assignment,
and I touched on that phrase briefly in

11
00:00:50,399 --> 00:00:54,000
the previous section. I mentioned that
I'd explain it more later so let's see

12
00:00:54,000 --> 00:00:58,289
what this is all about.
Well augmented assignment sounds really

13
00:00:58,289 --> 00:01:03,020
fancy, but it's just a shorthand way of
assigning values to a variable.

14
00:01:03,020 --> 00:01:08,040
The Python documentation describes augmented
assignment as the combination, in a

15
00:01:08,040 --> 00:01:13,110
single statement, of a binary operation
and an assignment statement, which is

16
00:01:13,110 --> 00:01:19,780
true but not terribly understandable.
So looking at the code on line 27 there,

17
00:01:19,780 --> 00:01:24,390
guesses equals guesses plus 1, we're
performing an addition. We take the value

18
00:01:24,390 --> 00:01:28,350
of guesses and add 1 to it.
We then bind the variable guesses to the

19
00:01:28,350 --> 00:01:33,659
result. guesses becomes one greater than
what it was. The addition is the binary

20
00:01:33,659 --> 00:01:38,009
operation, binary because it takes two
operands to work on, and then there is an

21
00:01:38,009 --> 00:01:43,200
assignment. So going back to our code, to
change this code on line 27 to an

22
00:01:43,200 --> 00:01:47,960
augmented assignment, we'll write it this
way: so I want to comment that line out.

23
00:01:47,960 --> 00:01:57,020
On line 28 I'm gonna type guesses plus
equals 1, and that's shorthand for what

24
00:01:57,030 --> 00:02:03,209
we had previously. If you were
programming in Java or C++, IntelliJ

25
00:02:03,209 --> 00:02:07,349
wouldn't have suggested this change.
That's because, in those languages, both

26
00:02:07,349 --> 00:02:11,940
statements are in fact identical. Their
use is just a matter of preference or style.

27
00:02:11,940 --> 00:02:16,220
In Python, however, the augmented
assignment form only evaluates the

28
00:02:16,220 --> 00:02:22,760
assignee, guesses in our example, once.
In the other form, where guess has appeared twice,

29
00:02:22,760 --> 00:02:27,320
the variable is evaluated once
each time it happens, and that's less

30
00:02:27,320 --> 00:02:33,060
efficient. Now I'll admit that the
evaluation isn't very complicated here.

31
00:02:33,060 --> 00:02:36,690
After all, it's not like we're doing some
complex calculation - you know, something like

32
00:02:36,690 --> 00:02:42,180
12 times 850 plus 13 million
divided by 18 is obviously going to take

33
00:02:42,180 --> 00:02:47,940
a bit of time to work out. So evaluating
a variable - what do I mean by that? Well

34
00:02:47,940 --> 00:02:52,319
evaluating a variable basically consists
of looking up its value. That's pretty

35
00:02:52,319 --> 00:02:57,180
fast, and probably takes a small fraction
of a millisecond. But doing it twice, when

36
00:02:57,180 --> 00:03:02,040
you could just do it once, is obviously
less efficient. If you do that in a loop,

37
00:03:02,040 --> 00:03:07,950
going round, say, a million times, you
could be wasting a second or more. Now in

38
00:03:07,950 --> 00:03:11,819
terms of efficiency, that might not sound
bad, unless your code's trying to land an

39
00:03:11,819 --> 00:03:16,889
aeroplane. A jumbo jet can travel at over
600 miles per hour. At that speed,

40
00:03:16,889 --> 00:03:22,590
one second represents one sixth of a
mile, or about 268 meters. Of course, if

41
00:03:22,590 --> 00:03:25,860
your code's trying to land an aeroplane
without slowing it down first, then you've

42
00:03:25,860 --> 00:03:30,060
probably got more serious problems than
missing an augmented assignment.

43
00:03:30,060 --> 00:03:34,950
But hopefully, you get the point here.
There's also another reason why

44
00:03:34,950 --> 00:03:40,300
augmented assignment is more efficient
in Python. With our first form on line 27

45
00:03:40,300 --> 00:03:45,020
Python creates a new variable. It binds
that new variable to the result of

46
00:03:45,020 --> 00:03:50,400
performing the calculation, then destroys
the original variable.

47
00:03:50,400 --> 00:03:54,459
Using an Augmented assignment, you can
perform the operation in-place where

48
00:03:54,459 --> 00:03:59,560
possible, modifying the original variable
instead of creating a new one. Now this

49
00:03:59,560 --> 00:04:03,880
isn't always possible, depending on the
type of object, the variable, in question,

50
00:04:03,880 --> 00:04:07,500
but it will be done in place whenever
possible. Alright, so let's switch back to

51
00:04:07,510 --> 00:04:11,349
the code.
So Intellij suggests that you use

52
00:04:11,349 --> 00:04:15,609
augmented assignments in your Python
code, rather than the longhand version

53
00:04:15,609 --> 00:04:20,040
that we used on line 27, because
ultimately, they're more efficient.

54
00:04:20,040 --> 00:04:24,099
The term augmented assignment sounds far
more complicated than it really is.

55
00:04:24,099 --> 00:04:27,310
The symbol for them is just the binary
operator plus minus multiplication

56
00:04:27,310 --> 00:04:32,020
divide, followed by an equal sign. You
can see the example there on line 28.

57
00:04:32,020 --> 00:04:42,220
Alright so let's create a new Python
file. I'm gonna call it aa, and I'm gonna

58
00:04:42,220 --> 00:04:48,009
paste in some code, just to save a bit of
time in the video. Okay, so there's the

59
00:04:48,009 --> 00:04:51,960
code there. Now what I've done is, I've
added comments to the right of the code,

60
00:04:51,960 --> 00:04:57,190
to show the results we'll get from each
augmented assignment. So it's easier to see

61
00:04:57,190 --> 00:05:01,720
that these augmented assignments relate
directly to a normal assignment.

62
00:05:01,720 --> 00:05:05,400
They're easier to type, and as they're also
more efficient, it makes sense really to

63
00:05:05,400 --> 00:05:12,180
use them. So let's run the program, just to
confirm we get the expected values for x.

64
00:05:13,060 --> 00:05:16,800
You can see the values there.
Scrolling down we've got 24, 20 for the

65
00:05:16,800 --> 00:05:28,840
various operations, 100, 25, 5, noting this one's
now a float, 25.0, and the last value, 0.0.

66
00:05:29,400 --> 00:05:32,520
Alright, so we've seen two
binary operations that can be performed

67
00:05:32,520 --> 00:05:37,540
on strings; concatenation and repetition.
They can also be performed using

68
00:05:37,540 --> 00:05:41,560
augmented assignment. So let's actually
type in some code here, and have a look

69
00:05:41,560 --> 00:05:49,080
at that. On line 24, I'm going to type
greeting equals Good in double quotes.

70
00:05:49,080 --> 00:05:58,080
Line 26, greeting plus equals morning, and
what I'm going to do there is, on line 24,

71
00:05:58,080 --> 00:06:02,360
put a space before the double quote.
Moving on, I'm going to print greeting,

72
00:06:02,369 --> 00:06:08,649
print parentheses greeting, just scroll up a bit
more, and on line 29 I'm gonna type

73
00:06:08,649 --> 00:06:21,129
greeting star equals 5, print greeting.
Alright, so if we run the program again now, you can

74
00:06:21,129 --> 00:06:24,309
see the output down the bottom of the
screen. Firstly, we get Good morning

75
00:06:24,309 --> 00:06:30,309
printed out - a combination of the word Good
on line 24 and morning on line 26 using

76
00:06:30,309 --> 00:06:34,659
augmented assignment. But then also, on
line 29, we've used augmented assignment

77
00:06:34,659 --> 00:06:38,289
and you can see we've now got five
versions of Good morning showing on the

78
00:06:38,289 --> 00:06:42,610
second line, or the last line, of output
before the program ended. And we saw that

79
00:06:42,610 --> 00:06:46,280
as well, in the last section and this is
the augmented assignment way of

80
00:06:46,280 --> 00:06:50,520
performing both of these things - these
two operations - concatenation and

81
00:06:50,520 --> 00:06:55,059
repetition with strings. Now there's five
more binary operations that we haven't

82
00:06:55,060 --> 00:06:59,060
looked at, for manipulating individual
bits as well as a matrix multiplication

83
00:06:59,060 --> 00:07:03,879
operator, but we'll leave them for a
later video. So all the binary operators

84
00:07:03,879 --> 00:07:08,229
can be used in augmented assignment, and
we can see the full list at the operator

85
00:07:08,229 --> 00:07:12,400
precedence table, which we looked at
before. Let's just open that up again.

86
00:07:12,400 --> 00:07:17,379
Loading a browser I'll paste in the link,
in the resources section as well, and

87
00:07:17,380 --> 00:07:23,740
it's in section 6.16 down the end of the
page, so let's do a quick search for that.

88
00:07:23,740 --> 00:07:27,159
Okay, there's operator precedence again,
towards the bottom. So you can check out

89
00:07:27,159 --> 00:07:31,809
the binary operators on this page. And
again, all the binary operators can be

90
00:07:31,809 --> 00:07:36,610
used in augmented assignment. Alright, so
moving on. In the next video, we'll take a

91
00:07:36,610 --> 00:07:40,100
break from coding and have a look at the
style of the code that we write.

92
00:07:40,100 --> 00:07:43,020
See you in the next video.

