1
00:00:00,420 --> 00:00:04,560
Hello, and welcome to Chapter Two.
Hope you enjoyed Chapter One.

2
00:00:04,560 --> 00:00:08,950
It was one of the longer lectures.
Trying to motivate you a little bit.

3
00:00:08,950 --> 00:00:12,050
And now we're going to kind of go back to
the basics, to the, chapter

4
00:00:12,050 --> 00:00:15,950
Chapter One covered sort of the first four
to five chapters of the book.

5
00:00:15,950 --> 00:00:20,140
So as always, this this video,
these slides are

6
00:00:20,140 --> 00:00:23,620
copyright Creative Common Attribution, as
well as the audio.

7
00:00:24,810 --> 00:00:25,380
And so,

8
00:00:26,390 --> 00:00:28,430
now we're going to talk about
sort of the really

9
00:00:28,430 --> 00:00:32,876
low-level things that make up the Python
language.

10
00:00:32,876 --> 00:00:36,533
Constants. So I'm going to summarize this
terminology just so I

11
00:00:36,533 --> 00:00:39,950
can like say the word "constant" and you
won't freak out.

12
00:00:39,950 --> 00:00:45,590
A constant is as contrasted with something
that changes, a variable.

13
00:00:45,590 --> 00:00:48,840
We talk about variables in the next slide.
But for now, constants.

14
00:00:48,840 --> 00:00:51,480
Constants are in things that are sort

15
00:00:51,480 --> 00:00:54,150
of natural and instinctive.
Things like numbers.

16
00:00:54,150 --> 00:00:56,656
A hundred and twenty-three.

17
00:00:56,656 --> 00:00:59,170
98.6, or Hello world.

18
00:00:59,170 --> 00:01:02,600
And so in, in, what, what I'm doing here
is we're, we're using

19
00:01:02,600 --> 00:01:06,150
a Python interpreter, and that, that's how
you can tell, the chevron prompt.

20
00:01:06,150 --> 00:01:10,296
And I'm saying print 123, and then Python
responds with 123,

21
00:01:10,296 --> 00:01:16,420
print 98.6, Python responds with 98.6,
and print 'Hello world'.

22
00:01:16,420 --> 00:01:21,305
So the constants are the 123, 98.6, and
'Hello world'.

23
00:01:21,305 --> 00:01:22,770
So these are things.

24
00:01:22,770 --> 00:01:26,930
We can use either single quotes or double
quotes to make strings.

25
00:01:26,930 --> 00:01:29,730
And so programs kind of work with numbers
and work with

26
00:01:29,730 --> 00:01:34,720
strings and we have these non-varying
values that we call constants.

27
00:01:34,720 --> 00:01:39,940
So the other side of the picture is a
variable.

28
00:01:39,940 --> 00:01:42,070
And the way I like to characterize a
variable

29
00:01:42,070 --> 00:01:44,740
is it's a place in the memory of the
computer.

30
00:01:46,100 --> 00:01:47,590
We give it a name as a programmer.

31
00:01:47,590 --> 00:01:49,530
We pick the variable name.

32
00:01:49,530 --> 00:01:55,130
In this, I'm saying x equals 12.2 and y
equals 14.

33
00:01:55,130 --> 00:01:58,490
I am choosing the name and I'm choosing
what to put in there.

34
00:01:59,610 --> 00:02:04,040
This is a statement called an assignment
statement, and the way to

35
00:02:04,040 --> 00:02:07,290
think of the assignment statement is that
it sort of has a direction.

36
00:02:08,370 --> 00:02:12,010
We're saying, dear Python, go find some
memory.

37
00:02:12,010 --> 00:02:15,780
I will use label x later to, to refer to
that

38
00:02:15,780 --> 00:02:19,680
memory, and take the number 12.2 and stick
it into x.

39
00:02:19,680 --> 00:02:21,460
Then this is sequential code.

40
00:02:21,460 --> 00:02:24,360
Then the next thing I want you to do is
I'd like you to go find some

41
00:02:24,360 --> 00:02:30,950
more memory, call it y, I will call it y
later, and stick 14 in there, okay?

42
00:02:30,950 --> 00:02:34,280
And so that ends up sort of with two
little areas

43
00:02:34,280 --> 00:02:34,800
of memory.

44
00:02:36,500 --> 00:02:38,950
You know, the one labeled x, and
here's a

45
00:02:38,950 --> 00:02:41,930
little cell in which we, like a drawer, or
something.

46
00:02:41,930 --> 00:02:44,960
And one labeled y. And we put 12.2.
After these

47
00:02:44,960 --> 00:02:49,550
lines run, we have 12.2 in one and
14 in the other.

48
00:02:49,550 --> 00:02:55,050
Then, for example, if there's another line
that's down here, so there's this

49
00:02:55,050 --> 00:02:59,250
third line after this has happened, after
this has happened, x equals 100.

50
00:02:59,250 --> 00:03:02,300
Remember, this has kind of got an, a
direction to it, see?

51
00:03:02,300 --> 00:03:07,460
Oh, remember that x that I had, you know,
I would like now to put 100 in that.

52
00:03:07,460 --> 00:03:09,770
So as I'm thinking this through, I think
of that as sort of

53
00:03:09,770 --> 00:03:14,920
removing the 12.2 or overwriting the 12.2
and putting 100 in its place.

54
00:03:14,920 --> 00:03:20,830
And so at the end here, x is left with 100
and y is left with 1 4 with 14.

55
00:03:20,830 --> 00:03:24,510
So these variables can kind of have one
value in them and

56
00:03:24,510 --> 00:03:26,100
what we can look at them and we can

57
00:03:26,100 --> 00:03:28,850
reuse them and put different values in if
we want.

58
00:03:30,900 --> 00:03:33,870
There are some rules for naming your
variables.

59
00:03:33,870 --> 00:03:35,840
Again, you get to pick the variable names.

60
00:03:37,290 --> 00:03:39,530
Often we pick variables that make some
sense.

61
00:03:39,530 --> 00:03:41,560
We'll talk about that in a second.

62
00:03:41,560 --> 00:03:44,900
In Python variables can start with an
underscore.

63
00:03:44,900 --> 00:03:48,090
We tend not to, as normal programmers, use
those.

64
00:03:48,090 --> 00:03:52,330
We let libraries use those.

65
00:03:52,330 --> 00:03:54,880
It has to have letters, numbers, and
underscores.

66
00:03:54,880 --> 00:03:56,230
And, and start

67
00:03:56,230 --> 00:04:00,030
with start with a letter or an underscore.

68
00:04:00,030 --> 00:04:05,600
Case matters, so spam is good, eggs is

69
00:04:05,600 --> 00:04:08,300
good, spam23 is good because
the number is not

70
00:04:08,300 --> 00:04:11,860
the first character, _speed, that's also
perfectly fine

71
00:04:11,860 --> 00:04:13,605
because it start with an
underscore or a letter.

72
00:04:13,605 --> 00:04:20,840
[COUGH] 23Spam starts with a letter,
starts with a number, so that's bad.

73
00:04:20,840 --> 00:04:21,290
This starts with something

74
00:04:21,290 --> 00:04:24,390
other than a letter or an underscore.

75
00:04:24,390 --> 00:04:28,050
And you can't use a dot in
the variable name.

76
00:04:28,050 --> 00:04:31,280
It turns out the dot has meaning to Python
that would confuse it.

77
00:04:34,130 --> 00:04:37,080
That would confuse it and wouldn't
understand [COUGH] what we

78
00:04:37,080 --> 00:04:39,150
really mean there, and so that would be a
syntax error.

79
00:04:39,150 --> 00:04:41,030
That would be a syntax error.

80
00:04:41,030 --> 00:04:45,850
Because case is sensitive, that means that
things like all lowercase

81
00:04:45,850 --> 00:04:49,720
spam is different than a upper case S and
all uppercase.

82
00:04:49,720 --> 00:04:54,990
These are three distinct variables that
are unique.

83
00:04:54,990 --> 00:04:58,590
Most people don't use, choose variables
that might be so confusing.

84
00:04:58,590 --> 00:04:59,440
So that's to

85
00:04:59,440 --> 00:05:01,860
you as you write it and as to anybody that

86
00:05:01,860 --> 00:05:06,400
might read it would find three variables
named this very confusing.

87
00:05:06,400 --> 00:05:08,060
So it's a bad idea.

88
00:05:08,060 --> 00:05:10,160
Don't do it, but I'm just showing you as

89
00:05:10,160 --> 00:05:14,900
an example that case can make a variable
name distinct.

90
00:05:14,900 --> 00:05:18,170
And again, this variable is a place in
memory

91
00:05:18,170 --> 00:05:22,400
that we are going to store and retrieve
information.

92
00:05:22,400 --> 00:05:24,680
Whether that be numbers or strings or
whatever.

93
00:05:24,680 --> 00:05:25,840
These are things that we control.

94
00:05:27,010 --> 00:05:30,160
Now Python also has a set of reserved
words.

95
00:05:30,160 --> 00:05:33,040
What it really means is you can't use
these for variables.

96
00:05:33,040 --> 00:05:38,854
These words have very special meaning.
And, for, is, raise, if.

97
00:05:38,854 --> 00:05:44,610
So you can't make a variable named i-f.
It would be like, oh no, that is "if".

98
00:05:44,610 --> 00:05:45,470
I know what "if" is.

99
00:05:45,470 --> 00:05:49,350
So these are words that Python has as its
core vocabulary.

100
00:05:49,350 --> 00:05:51,180
And forbids you to use them

101
00:05:51,180 --> 00:05:55,610
for other purposes, like
variable names or later function names.

102
00:05:55,610 --> 00:06:03,000
So that's kind of the vocabulary.
Constants, variables, and reserved words.

103
00:06:03,000 --> 00:06:05,990
Now, we take these and we start assembling
them

104
00:06:05,990 --> 00:06:10,580
into sort of sentences, statements, Python
statements that do something.

105
00:06:10,580 --> 00:06:13,290
So we've already talked about an
assignment statement.

106
00:06:13,290 --> 00:06:15,200
It has kind of an arrow here.

107
00:06:15,200 --> 00:06:18,210
It says, hey Python, go find me a place
called x.

108
00:06:18,210 --> 00:06:22,710
Take the number 2 and stick it in there
for later, then continue on.

109
00:06:22,710 --> 00:06:26,990
Now, because there's an arrow,
the right side of this is done first.

110
00:06:26,990 --> 00:06:31,360
And so it said, so this right side, you
can kind of ignore for the moment the

111
00:06:31,360 --> 00:06:33,410
left-hand side and it calculates the
right-hand

112
00:06:33,410 --> 00:06:35,530
side by looking at the current
value for x.

113
00:06:35,530 --> 00:06:40,620
Which happens to be 2, and adds these
two things together, and then gets 4.

114
00:06:40,620 --> 00:06:44,910
And then, at the point where it knows
4, that this

115
00:06:44,910 --> 00:06:48,740
number is 4, it will then store that
back into X.

116
00:06:48,740 --> 00:06:53,790
And so then, later, we print x and we will
get the 4. And so again, this is

117
00:06:53,790 --> 00:06:57,180
a sequence of steps and the, the
variable x

118
00:06:57,180 --> 00:07:00,800
changes as these steps continue.
And when we're saying print x,

119
00:07:00,800 --> 00:07:03,720
that really means print the
current value for x.

120
00:07:07,630 --> 00:07:12,580
So, we can do a number of different
operators and assignment statements.

121
00:07:12,580 --> 00:07:15,960
We calculate this right-hand side.

122
00:07:15,960 --> 00:07:19,170
This is sort of all calculated, whatever
this is, based on

123
00:07:19,170 --> 00:07:22,450
the current value for x does this
calculation, and then when

124
00:07:22,450 --> 00:07:25,570
it knows what the answer is, it assigns
that into the

125
00:07:25,570 --> 00:07:28,586
variable that's on the left-hand side of
the assignment statement.

126
00:07:28,586 --> 00:07:32,730
Again, calculate the right-hand

127
00:07:32,730 --> 00:07:35,670
side completely and then move it to the
left-hand side.

128
00:07:35,670 --> 00:07:38,790
Some early languages actually didn't use

129
00:07:38,790 --> 00:07:41,280
the equals sign for the assignment
operator.

130
00:07:41,280 --> 00:07:46,390
This assignment operator in, in a way it
kind of [INAUDIBLE]

131
00:07:46,390 --> 00:07:46,790
Some languages

132
00:07:46,790 --> 00:07:50,750
An early language actually
used an arrow.

133
00:07:50,750 --> 00:07:53,110
Arrows aren't really on people's
keyboards.

134
00:07:53,110 --> 00:07:57,950
Another language used colon equals as
this assignment operator.

135
00:07:57,950 --> 00:07:58,890
But we use equals.

136
00:07:58,890 --> 00:08:03,800
Now, if you're familiar with math this can
be a little confusing, like x equals 1

137
00:08:03,800 --> 00:08:07,780
and then X equals 2. That as mathematics
would be bad math

138
00:08:07,780 --> 00:08:11,580
because in a proof or a problem, x can only
have one value.

139
00:08:11,580 --> 00:08:14,740
But in programming if this was two
statements, that means

140
00:08:14,740 --> 00:08:17,935
just x had a value, and then the value
for x changed later.

141
00:08:17,935 --> 00:08:23,150
Okay. So just kind of go through this
because it's

142
00:08:23,150 --> 00:08:27,090
working from the right-hand side to the
left-hand side on assignment statements.

143
00:08:27,090 --> 00:08:30,940
It is pulling out these x values, so x may
have 0.6.

144
00:08:30,940 --> 00:08:35,680
It pulls the values out before, sort of
ignoring this part

145
00:08:35,680 --> 00:08:39,280
right here, and it's just going to try to
resolve this expression.

146
00:08:39,280 --> 00:08:42,570
And it has multiplication and parentheses
and things like that.

147
00:08:42,570 --> 00:08:46,480
So it basically pulls the 0.6 into the
calculation,

148
00:08:46,480 --> 00:08:48,610
does the 1 minus x, which gives you 0.4.

149
00:08:48,610 --> 00:08:52,660
Then it multiplies these three things
together, giving 0.93.

150
00:08:52,660 --> 00:08:56,904
And then when it is all done with all of
that, it takes that.

151
00:08:56,904 --> 00:08:57,270
Oops.

152
00:08:57,270 --> 00:09:03,980
It takes that 0.93, and then puts it back
into x.

153
00:09:03,980 --> 00:09:08,230
And so this is just sort of emphasizing
how the right-hand side is computed to

154
00:09:08,230 --> 00:09:13,630
produce a value, then it is moved into the
variable, and that is why you

155
00:09:13,630 --> 00:09:15,995
can have sort of x on both sides.

156
00:09:15,995 --> 00:09:20,140
Because this is like the old, and this is
the new.

157
00:09:20,140 --> 00:09:23,690
This is the old x participates in the
calculation, and

158
00:09:23,690 --> 00:09:27,720
then when the calculation is done, it
becomes the new x.

159
00:09:27,720 --> 00:09:28,410
Hope that makes sense.

160
00:09:29,500 --> 00:09:32,980
So, this, on the right-hand side here is
a numeric expression.

161
00:09:32,980 --> 00:09:35,650
So we have a number of different
operators.

162
00:09:35,650 --> 00:09:38,760
Some of them are instinctive,
intuitive.

163
00:09:38,760 --> 00:09:40,260
The plus and the minus.

164
00:09:40,260 --> 00:09:43,310
The reason some of these are so weird is
in the really old days, we

165
00:09:43,310 --> 00:09:45,950
didn't have too many things on the
keyboard,

166
00:09:45,950 --> 00:09:48,100
and a lot of programs were very
mathematical.

167
00:09:48,100 --> 00:09:49,410
And so they figured out what was on

168
00:09:49,410 --> 00:09:52,220
the keyboard of the computer equipment of
the day.

169
00:09:52,220 --> 00:09:55,110
And then they had to fake certain things.

170
00:09:55,110 --> 00:09:57,750
So, it turns out that plus and minus
were on the keyboard,

171
00:09:57,750 --> 00:10:02,620
and so plus and minus are
addition and subtraction, respectively.

172
00:10:02,620 --> 00:10:04,140
There was no kind of times

173
00:10:04,140 --> 00:10:08,360
operator for multiplication, and dot was
used for decimal points.

174
00:10:08,360 --> 00:10:12,930
So they used asterisk for multiplication.
So on computers' languages, nearly

175
00:10:12,930 --> 00:10:17,760
all of them, they basically use a mult
times for multiplication.

176
00:10:17,760 --> 00:10:19,790
Slash is used for division.

177
00:10:19,790 --> 00:10:23,460
So we say like, 8/2, which is 8 divided
by 2.

178
00:10:24,642 --> 00:10:29,727
Raising something to the power like 4
squared,

179
00:10:29,727 --> 00:10:35,680
that is double asterisk.
And then remainder is if you

180
00:10:35,680 --> 00:10:40,480
do a division that gives you the remainder
rather than divisor.

181
00:10:40,480 --> 00:10:44,580
So 8 over 2 is 4 remainder 0. So

182
00:10:44,580 --> 00:10:48,100
the remainder is what you get with this
particular operator.

183
00:10:48,100 --> 00:10:49,900
There's a few cool things that we can do

184
00:10:49,900 --> 00:10:52,610
with remainder that we won't talk about
right away.

185
00:10:52,610 --> 00:10:54,660
But it's there.

186
00:10:54,660 --> 00:10:57,470
And so here's just a couple of sample
expressions.

187
00:10:58,888 --> 00:11:03,270
That's giving me green.

188
00:11:03,270 --> 00:11:04,240
Okay.

189
00:11:04,240 --> 00:11:06,690
So, so again, I'm using a Python
Interpreter.

190
00:11:06,690 --> 00:11:08,310
So you can kind of, this is just the
prompt.

191
00:11:08,310 --> 00:11:10,750
These chevrons are the prompt.

192
00:11:10,750 --> 00:11:13,760
Create the variable xx, and
assign it to 2.

193
00:11:13,760 --> 00:11:16,580
Retrieve the old value and an addition.

194
00:11:16,580 --> 00:11:20,050
Then print it out and put it back into xx
so xx

195
00:11:20,050 --> 00:11:24,110
has 4.
yy, this is a multiplication, 440 times 12.

196
00:11:24,110 --> 00:11:28,780
It is 5,280. yy over 1,000.
Now this is a little counter-intuitive

197
00:11:28,780 --> 00:11:35,060
Because yy is an integer,
it then does it in a truncated division.

198
00:11:35,060 --> 00:11:42,080
And so, 5,280 divided by 1000 is 5.
Now if, and,

199
00:11:42,080 --> 00:11:45,160
and so that's an integer division.
We'll see in a second

200
00:11:45,160 --> 00:11:46,490
about floating point division.

201
00:11:47,990 --> 00:11:51,570
Now we take the variable jj and we set it
to 23.

202
00:11:51,570 --> 00:11:55,860
And now we're going to use the modular or
modulo or remainder operator.

203
00:11:55,860 --> 00:12:01,610
Say what is jj, what is the remainder when
divide this jj by 5.

204
00:12:01,610 --> 00:12:04,710
And so if you think about this, we take
old long division,

205
00:12:04,710 --> 00:12:09,187
23 divided by 5, you end up with 4 and
then remainder 3.

206
00:12:10,590 --> 00:12:13,010
The modulo operator, or the percent of

207
00:12:13,010 --> 00:12:16,090
the remainder operator, gives us back this
number.

208
00:12:16,090 --> 00:12:18,670
And so that's why kk is 3.

209
00:12:18,670 --> 00:12:22,780
It is the remainder of 23 when
divided by 5,

210
00:12:22,780 --> 00:12:27,670
or the remainder of the division
of 5 into 23.

211
00:12:27,670 --> 00:12:32,090
And the raising to the power, 4 cubed.
That's not so nice.

212
00:12:32,090 --> 00:12:34,550
4 cubed is 4 star, star 3.

213
00:12:34,550 --> 00:12:35,842
And so that ends up being 64.

214
00:12:35,842 --> 00:12:42,400
So that's just operations.
Now, just like in algebra and mathematics

215
00:12:43,510 --> 00:12:49,650
we have rules about when to which, which
operations happen first.

216
00:12:49,650 --> 00:12:52,940
In general, things like the power happens
before the

217
00:12:52,940 --> 00:12:56,030
multiplication and division, and then the
addition and subtraction happen.

218
00:12:56,030 --> 00:12:58,630
And so there are some rules that, when

219
00:12:58,630 --> 00:13:00,940
you're looking at an expression and trying
to calculate

220
00:13:00,940 --> 00:13:05,910
what its value is, if you don't have
parentheses, it follows these rules.

221
00:13:05,910 --> 00:13:10,120
And so the, the most, the rule
that sort of

222
00:13:10,120 --> 00:13:14,430
trumps all the rules is that parentheses
are always respected.

223
00:13:14,430 --> 00:13:16,328
So a lot of us just write these with

224
00:13:16,328 --> 00:13:19,550
parentheses in place, even sometimes
though you don't need it.

225
00:13:21,100 --> 00:13:24,930
Then after parentheses have been handled,
then it does exponentiation.

226
00:13:24,930 --> 00:13:26,220
Then it does multiplication,

227
00:13:26,220 --> 00:13:30,188
division, and remainder.
And then it does addition and subtraction.

228
00:13:30,188 --> 00:13:33,960
And then, when all else being equal, it
just works left to right.

229
00:13:33,960 --> 00:13:40,370
So let's, let's look through an example.
So here is a

230
00:13:40,370 --> 00:13:47,250
calculation that is, you know, 1, 1 plus 2
times 3 divided 4 over 5.

231
00:13:47,250 --> 00:13:51,380
And the question is, what order does this
happen, okay?

232
00:13:51,380 --> 00:13:53,470
And so let's sort of take a look at this.

233
00:13:55,210 --> 00:13:58,780
And so, we start with are there any
parentheses?

234
00:13:58,780 --> 00:14:00,620
And the answer is no, there are no
parentheses.

235
00:14:00,620 --> 00:14:05,100
So let's go next.
Power.

236
00:14:05,110 --> 00:14:11,100
And so the, the power says okay, let's
look across and find those things that

237
00:14:11,100 --> 00:14:17,380
are raised to a power. And 2 cubed or 2 to
the third power is the, the power.

238
00:14:17,380 --> 00:14:20,080
So we're going to do that one.
Okay.

239
00:14:20,080 --> 00:14:21,880
And then we can, the way I do it when

240
00:14:21,880 --> 00:14:24,150
I'm sort of doing these slowly is I
rewrite it.

241
00:14:24,150 --> 00:14:28,710
So the 2 to the third power becomes 8, so
it's 1 plus 8 over 4 times 5.

242
00:14:28,710 --> 00:14:31,730
And then now we can say oh power, that's
taken care of.

243
00:14:31,730 --> 00:14:35,540
Now we're going to do multiplication and
division and we go across.

244
00:14:35,540 --> 00:14:38,620
Now we have both a division and
multiplication.

245
00:14:38,620 --> 00:14:40,310
Okay? Multiplication and division are done
at the same

246
00:14:40,310 --> 00:14:42,630
time, so that means we do left to right,

247
00:14:42,630 --> 00:14:45,105
which means we do the first one we
encounter first.

248
00:14:45,105 --> 00:14:53,500
And so that will be 8 over 4 because of
the left-to-right rule.

249
00:14:53,500 --> 00:14:55,220
And so we find that one, and that's the

250
00:14:55,220 --> 00:14:58,520
one that gets computed next, and that
turns into 2.

251
00:14:58,520 --> 00:15:00,550
And again, I like to rewrite these
expressions

252
00:15:00,550 --> 00:15:03,910
just to keep my brain really, really
clear.

253
00:15:03,910 --> 00:15:06,860
After a while you just do it in your head,
but I rewrite them.

254
00:15:06,860 --> 00:15:07,700
When I was first learning it,

255
00:15:07,700 --> 00:15:09,070
at least, I rewrote it all the time.

256
00:15:10,620 --> 00:15:15,480
And and so next looking at this, there's a
multiplication.

257
00:15:15,480 --> 00:15:19,660
We're not done with multiplication yet.
So the 2 over 5 is the next thing.

258
00:15:21,040 --> 00:15:24,890
And then we do that calculation, and that
becomes 10, and again we rewrite it.

259
00:15:24,890 --> 00:15:28,670
And now we've done the multiplication, and
we're going to do addition next.

260
00:15:28,670 --> 00:15:33,950
And that's just 1 over 10, and that
becomes 11.

261
00:15:33,950 --> 00:15:36,540
And so basically, this big long thing,

262
00:15:36,540 --> 00:15:40,700
through a series of successive steps,
becomes 11.

263
00:15:40,700 --> 00:15:44,110
And indeed, when we print it out, that's
what we get.

264
00:15:44,110 --> 00:15:44,610
Okay?

265
00:15:46,810 --> 00:15:49,140
So, there's the rules that are
parentheses,

266
00:15:49,140 --> 00:15:52,440
power, multiplication, addition, and then,
left to right.

267
00:15:52,440 --> 00:15:58,710
But smart people usually just put
parentheses in, you know?

268
00:15:58,710 --> 00:16:01,760
So here's this, here's an exam.
Oop, go back, go back.

269
00:16:01,760 --> 00:16:03,360
Here's an exam question.

270
00:16:03,360 --> 00:16:09,040
Now, I wouldn't write this code, right, I
wouldn't write this code this way.

271
00:16:09,040 --> 00:16:11,270
I would put a parentheses here.

272
00:16:12,360 --> 00:16:13,100
And a parentheses there.

273
00:16:14,600 --> 00:16:18,920
It's the same thing because that's exactly
the 2 times 3 is going to happen and

274
00:16:18,920 --> 00:16:20,800
4 over 5 is going to happen and then the

275
00:16:20,800 --> 00:16:23,140
plus and the minus will happen
left to right.

276
00:16:23,140 --> 00:16:25,930
But why not make it easier on your readers

277
00:16:25,930 --> 00:16:28,430
and just put the parentheses in. Because
they're redundant.

278
00:16:28,430 --> 00:16:31,460
They're not necessary, but away you go.

279
00:16:31,460 --> 00:16:34,656
Now, if you don't want it to happen in
that order, of

280
00:16:34,656 --> 00:16:38,110
course then you have to put parentheses if
you want the addition

281
00:16:38,110 --> 00:16:40,890
to happen before the multiplication, then
you

282
00:16:40,890 --> 00:16:43,190
have to put parentheses in, which you can.

283
00:16:43,190 --> 00:16:48,500
But we tend to recommend that you use more
parentheses rather than less parentheses.

284
00:16:49,690 --> 00:16:53,530
Now, Python integer division
in Python 2,

285
00:16:53,530 --> 00:16:56,120
which we're using Python 2
for this class.

286
00:16:56,120 --> 00:17:00,090
There's a new Python 3 that the world is
slowly transitioning

287
00:17:00,090 --> 00:17:03,060
to and a lot of people are
using it in teaching.

288
00:17:03,060 --> 00:17:08,370
But it's not as common, sort of, in the
real world with libraries and utilities.

289
00:17:08,370 --> 00:17:10,671
And so we'll stick with
Python 2 for a few

290
00:17:10,671 --> 00:17:14,890
more years until Python 3 really kind of
turns the corner.

291
00:17:14,890 --> 00:17:17,910
It's nice to have it there, but there's so
much Python and it's so

292
00:17:17,910 --> 00:17:22,960
popular, Python 2, that it's just kind of
hard to get everybody up to Python 3.

293
00:17:22,960 --> 00:17:28,900
So in Python 2, integer division truncates
and you saw that before where

294
00:17:28,900 --> 00:17:34,060
I did the 5280 by 1000 and I got 5 as and,
and, but we

295
00:17:34,060 --> 00:17:38,030
can look at a couple of examples that make
this really very quite, quite clear.

296
00:17:38,030 --> 00:17:40,820
So, 10 divided by 2 is 5 as you would
expect.

297
00:17:40,820 --> 00:17:42,960
9 Divided by 2 is 4.

298
00:17:42,960 --> 00:17:44,630
Not exactly what you'd expect.

299
00:17:44,630 --> 00:17:49,220
You kind of expect that to be 4.5,
instead of 4.

300
00:17:49,220 --> 00:17:53,950
But in Python 3, it will be 4.5, but for
now, in Python 2,

301
00:17:53,950 --> 00:18:00,920
9 over, 9 over 2 is 4.
And 99 over 100 is 0.

302
00:18:00,920 --> 00:18:03,520
Now that seems rather counter-intuitive,
but it is a truncating

303
00:18:03,520 --> 00:18:07,150
division, it's not a rounding division,
it's a truncating division.

304
00:18:07,150 --> 00:18:10,980
Now, interestingly, if you make either of
these numbers have a decimal, make them

305
00:18:10,980 --> 00:18:16,282
what we call floating point numbers, then
the division is done in floating point.

306
00:18:16,282 --> 00:18:19,530
So, 10.0 over 2.0

307
00:18:19,530 --> 00:18:24,090
is 5.0.
Now, these are different.

308
00:18:24,090 --> 00:18:26,873
This is an integer number, and this is a
floating point number.

309
00:18:26,873 --> 00:18:27,836
It's 5.0.

310
00:18:27,836 --> 00:18:31,830
And then 99.0 over 100.0 is exactly as you

311
00:18:31,830 --> 00:18:34,580
would expect, and it's a floating point
number, so.

312
00:18:36,830 --> 00:18:41,310
Now you can also mix integers and floating
point numbers as you go.

313
00:18:41,310 --> 00:18:43,430
So here we have 99 over 100.

314
00:18:43,430 --> 00:18:46,890
Those are both integers.
Integer, integer.

315
00:18:46,890 --> 00:18:50,010
And, or, and that comes out with 0 because
it's truncating.

316
00:18:50,010 --> 00:18:53,080
Now if we have an integer and
a floating point

317
00:18:53,080 --> 00:18:57,191
number, 99 over 100.0, then that comes out
as 0.99.

318
00:18:58,350 --> 00:19:01,990
And either one, if we have 99 over 100,
that's a floating point, and

319
00:19:01,990 --> 00:19:02,710
that's an integer.

320
00:19:02,710 --> 00:19:06,520
We still end up with a floating point, so
this is a floating point, floating point.

321
00:19:06,520 --> 00:19:11,320
And even in complex expressions, as it
evaluates when

322
00:19:11,320 --> 00:19:13,270
it sees an integer, so the first thing
when

323
00:19:13,270 --> 00:19:20,820
you evaluate is this would become a 6, so
it would be 1 plus 6 over 4.0 minus 5.

324
00:19:20,820 --> 00:19:27,461
Then it would be doing the 6 over 4.0 and
that would be 1.5, 1 plus 1.5

325
00:19:27,461 --> 00:19:30,900
minus 5. And so this is an integer
and that's

326
00:19:30,900 --> 00:19:33,740
a floating point and the result becomes a
floating point.

327
00:19:33,740 --> 00:19:36,530
And then the rest of the calculation is
done floating point

328
00:19:36,530 --> 00:19:41,200
to the point where the ultimate is a
floating point negative 2.5.

329
00:19:41,200 --> 00:19:45,260
So you can throw a floating point into a
calculation and as soon as the

330
00:19:45,260 --> 00:19:48,290
calculation touches the floating point,
the remainder

331
00:19:48,290 --> 00:19:50,840
of the calculation is done in
floating point.

332
00:19:50,840 --> 00:19:52,644
It kind of converts at the floating point
but it doesn't

333
00:19:52,644 --> 00:19:55,830
want to convert it back because it
considers floating

334
00:19:55,830 --> 00:19:59,429
point sort of the more general of the
representations.