Python for Informatics: Chapter 2 - Expressions
-
0:00 - 0:05Hello, and welcome to Chapter Two.
Hope you enjoyed Chapter One. -
0:05 - 0:09It was one of the longer lectures.
Trying to motivate you a little bit. -
0:09 - 0:12And now we're going to kind of go back to
the basics, to the, chapter -
0:12 - 0:16Chapter One covered sort of the first four
to five chapters of the book. -
0:16 - 0:20So as always, this this video,
these slides are -
0:20 - 0:24copyright Creative Common Attribution, as
well as the audio. -
0:25 - 0:25And so,
-
0:26 - 0:28now we're going to talk about
sort of the really -
0:28 - 0:33low-level things that make up the Python
language. -
0:33 - 0:37Constants. So I'm going to summarize this
terminology just so I -
0:37 - 0:40can like say the word "constant" and you
won't freak out. -
0:40 - 0:46A constant is as contrasted with something
that changes, a variable. -
0:46 - 0:49We talk about variables in the next slide.
But for now, constants. -
0:49 - 0:51Constants are in things that are sort
-
0:51 - 0:54of natural and instinctive.
Things like numbers. -
0:54 - 0:57A hundred and twenty-three.
-
0:57 - 0:5998.6, or Hello world.
-
0:59 - 1:03And so in, in, what, what I'm doing here
is we're, we're using -
1:03 - 1:06a Python interpreter, and that, that's how
you can tell, the chevron prompt. -
1:06 - 1:10And I'm saying print 123, and then Python
responds with 123, -
1:10 - 1:16print 98.6, Python responds with 98.6,
and print 'Hello world'. -
1:16 - 1:21So the constants are the 123, 98.6, and
'Hello world'. -
1:21 - 1:23So these are things.
-
1:23 - 1:27We can use either single quotes or double
quotes to make strings. -
1:27 - 1:30And so programs kind of work with numbers
and work with -
1:30 - 1:35strings and we have these non-varying
values that we call constants. -
1:35 - 1:40So the other side of the picture is a
variable. -
1:40 - 1:42And the way I like to characterize a
variable -
1:42 - 1:45is it's a place in the memory of the
computer. -
1:46 - 1:48We give it a name as a programmer.
-
1:48 - 1:50We pick the variable name.
-
1:50 - 1:55In this, I'm saying x equals 12.2 and y
equals 14. -
1:55 - 1:58I am choosing the name and I'm choosing
what to put in there. -
2:00 - 2:04This is a statement called an assignment
statement, and the way to -
2:04 - 2:07think of the assignment statement is that
it sort of has a direction. -
2:08 - 2:12We're saying, dear Python, go find some
memory. -
2:12 - 2:16I will use label x later to, to refer to
that -
2:16 - 2:20memory, and take the number 12.2 and stick
it into x. -
2:20 - 2:21Then this is sequential code.
-
2:21 - 2:24Then the next thing I want you to do is
I'd like you to go find some -
2:24 - 2:31more memory, call it y, I will call it y
later, and stick 14 in there, okay? -
2:31 - 2:34And so that ends up sort of with two
little areas -
2:34 - 2:35of memory.
-
2:36 - 2:39You know, the one labeled x, and
here's a -
2:39 - 2:42little cell in which we, like a drawer, or
something. -
2:42 - 2:45And one labeled y. And we put 12.2.
After these -
2:45 - 2:50lines run, we have 12.2 in one and
14 in the other. -
2:50 - 2:55Then, for example, if there's another line
that's down here, so there's this -
2:55 - 2:59third line after this has happened, after
this has happened, x equals 100. -
2:59 - 3:02Remember, this has kind of got an, a
direction to it, see? -
3:02 - 3:07Oh, remember that x that I had, you know,
I would like now to put 100 in that. -
3:07 - 3:10So as I'm thinking this through, I think
of that as sort of -
3:10 - 3:15removing the 12.2 or overwriting the 12.2
and putting 100 in its place. -
3:15 - 3:21And so at the end here, x is left with 100
and y is left with 1 4 with 14. -
3:21 - 3:25So these variables can kind of have one
value in them and -
3:25 - 3:26what we can look at them and we can
-
3:26 - 3:29reuse them and put different values in if
we want. -
3:31 - 3:34There are some rules for naming your
variables. -
3:34 - 3:36Again, you get to pick the variable names.
-
3:37 - 3:40Often we pick variables that make some
sense. -
3:40 - 3:42We'll talk about that in a second.
-
3:42 - 3:45In Python variables can start with an
underscore. -
3:45 - 3:48We tend not to, as normal programmers, use
those. -
3:48 - 3:52We let libraries use those.
-
3:52 - 3:55It has to have letters, numbers, and
underscores. -
3:55 - 3:56And, and start
-
3:56 - 4:00with start with a letter or an underscore.
-
4:00 - 4:06Case matters, so spam is good, eggs is
-
4:06 - 4:08good, spam23 is good because
the number is not -
4:08 - 4:12the first character, _speed, that's also
perfectly fine -
4:12 - 4:14because it start with an
underscore or a letter. -
4:14 - 4:21[COUGH] 23Spam starts with a letter,
starts with a number, so that's bad. -
4:21 - 4:21This starts with something
-
4:21 - 4:24other than a letter or an underscore.
-
4:24 - 4:28And you can't use a dot in
the variable name. -
4:28 - 4:31It turns out the dot has meaning to Python
that would confuse it. -
4:34 - 4:37That would confuse it and wouldn't
understand [COUGH] what we -
4:37 - 4:39really mean there, and so that would be a
syntax error. -
4:39 - 4:41That would be a syntax error.
-
4:41 - 4:46Because case is sensitive, that means that
things like all lowercase -
4:46 - 4:50spam is different than a upper case S and
all uppercase. -
4:50 - 4:55These are three distinct variables that
are unique. -
4:55 - 4:59Most people don't use, choose variables
that might be so confusing. -
4:59 - 4:59So that's to
-
4:59 - 5:02you as you write it and as to anybody that
-
5:02 - 5:06might read it would find three variables
named this very confusing. -
5:06 - 5:08So it's a bad idea.
-
5:08 - 5:10Don't do it, but I'm just showing you as
-
5:10 - 5:15an example that case can make a variable
name distinct. -
5:15 - 5:18And again, this variable is a place in
memory -
5:18 - 5:22that we are going to store and retrieve
information. -
5:22 - 5:25Whether that be numbers or strings or
whatever. -
5:25 - 5:26These are things that we control.
-
5:27 - 5:30Now Python also has a set of reserved
words. -
5:30 - 5:33What it really means is you can't use
these for variables. -
5:33 - 5:39These words have very special meaning.
And, for, is, raise, if. -
5:39 - 5:45So you can't make a variable named i-f.
It would be like, oh no, that is "if". -
5:45 - 5:45I know what "if" is.
-
5:45 - 5:49So these are words that Python has as its
core vocabulary. -
5:49 - 5:51And forbids you to use them
-
5:51 - 5:56for other purposes, like
variable names or later function names. -
5:56 - 6:03So that's kind of the vocabulary.
Constants, variables, and reserved words. -
6:03 - 6:06Now, we take these and we start assembling
them -
6:06 - 6:11into sort of sentences, statements, Python
statements that do something. -
6:11 - 6:13So we've already talked about an
assignment statement. -
6:13 - 6:15It has kind of an arrow here.
-
6:15 - 6:18It says, hey Python, go find me a place
called x. -
6:18 - 6:23Take the number 2 and stick it in there
for later, then continue on. -
6:23 - 6:27Now, because there's an arrow,
the right side of this is done first. -
6:27 - 6:31And so it said, so this right side, you
can kind of ignore for the moment the -
6:31 - 6:33left-hand side and it calculates the
right-hand -
6:33 - 6:36side by looking at the current
value for x. -
6:36 - 6:41Which happens to be 2, and adds these
two things together, and then gets 4. -
6:41 - 6:45And then, at the point where it knows
4, that this -
6:45 - 6:49number is 4, it will then store that
back into X. -
6:49 - 6:54And so then, later, we print x and we will
get the 4. And so again, this is -
6:54 - 6:57a sequence of steps and the, the
variable x -
6:57 - 7:01changes as these steps continue.
And when we're saying print x, -
7:01 - 7:04that really means print the
current value for x. -
7:08 - 7:13So, we can do a number of different
operators and assignment statements. -
7:13 - 7:16We calculate this right-hand side.
-
7:16 - 7:19This is sort of all calculated, whatever
this is, based on -
7:19 - 7:22the current value for x does this
calculation, and then when -
7:22 - 7:26it knows what the answer is, it assigns
that into the -
7:26 - 7:29variable that's on the left-hand side of
the assignment statement. -
7:29 - 7:33Again, calculate the right-hand
-
7:33 - 7:36side completely and then move it to the
left-hand side. -
7:36 - 7:39Some early languages actually didn't use
-
7:39 - 7:41the equals sign for the assignment
operator. -
7:41 - 7:46This assignment operator in, in a way it
kind of [INAUDIBLE] -
7:46 - 7:47Some languages
-
7:47 - 7:51An early language actually
used an arrow. -
7:51 - 7:53Arrows aren't really on people's
keyboards. -
7:53 - 7:58Another language used colon equals as
this assignment operator. -
7:58 - 7:59But we use equals.
-
7:59 - 8:04Now, if you're familiar with math this can
be a little confusing, like x equals 1 -
8:04 - 8:08and then X equals 2. That as mathematics
would be bad math -
8:08 - 8:12because in a proof or a problem, x can only
have one value. -
8:12 - 8:15But in programming if this was two
statements, that means -
8:15 - 8:18just x had a value, and then the value
for x changed later. -
8:18 - 8:23Okay. So just kind of go through this
because it's -
8:23 - 8:27working from the right-hand side to the
left-hand side on assignment statements. -
8:27 - 8:31It is pulling out these x values, so x may
have 0.6. -
8:31 - 8:36It pulls the values out before, sort of
ignoring this part -
8:36 - 8:39right here, and it's just going to try to
resolve this expression. -
8:39 - 8:43And it has multiplication and parentheses
and things like that. -
8:43 - 8:46So it basically pulls the 0.6 into the
calculation, -
8:46 - 8:49does the 1 minus x, which gives you 0.4.
-
8:49 - 8:53Then it multiplies these three things
together, giving 0.93. -
8:53 - 8:57And then when it is all done with all of
that, it takes that. -
8:57 - 8:57Oops.
-
8:57 - 9:04It takes that 0.93, and then puts it back
into x. -
9:04 - 9:08And so this is just sort of emphasizing
how the right-hand side is computed to -
9:08 - 9:14produce a value, then it is moved into the
variable, and that is why you -
9:14 - 9:16can have sort of x on both sides.
-
9:16 - 9:20Because this is like the old, and this is
the new. -
9:20 - 9:24This is the old x participates in the
calculation, and -
9:24 - 9:28then when the calculation is done, it
becomes the new x. -
9:28 - 9:28Hope that makes sense.
-
9:30 - 9:33So, this, on the right-hand side here is
a numeric expression. -
9:33 - 9:36So we have a number of different
operators. -
9:36 - 9:39Some of them are instinctive,
intuitive. -
9:39 - 9:40The plus and the minus.
-
9:40 - 9:43The reason some of these are so weird is
in the really old days, we -
9:43 - 9:46didn't have too many things on the
keyboard, -
9:46 - 9:48and a lot of programs were very
mathematical. -
9:48 - 9:49And so they figured out what was on
-
9:49 - 9:52the keyboard of the computer equipment of
the day. -
9:52 - 9:55And then they had to fake certain things.
-
9:55 - 9:58So, it turns out that plus and minus
were on the keyboard, -
9:58 - 10:03and so plus and minus are
addition and subtraction, respectively. -
10:03 - 10:04There was no kind of times
-
10:04 - 10:08operator for multiplication, and dot was
used for decimal points. -
10:08 - 10:13So they used asterisk for multiplication.
So on computers' languages, nearly -
10:13 - 10:18all of them, they basically use a mult
times for multiplication. -
10:18 - 10:20Slash is used for division.
-
10:20 - 10:23So we say like, 8/2, which is 8 divided
by 2. -
10:25 - 10:30Raising something to the power like 4
squared, -
10:30 - 10:36that is double asterisk.
And then remainder is if you -
10:36 - 10:40do a division that gives you the remainder
rather than divisor. -
10:40 - 10:45So 8 over 2 is 4 remainder 0. So
-
10:45 - 10:48the remainder is what you get with this
particular operator. -
10:48 - 10:50There's a few cool things that we can do
-
10:50 - 10:53with remainder that we won't talk about
right away. -
10:53 - 10:55But it's there.
-
10:55 - 10:57And so here's just a couple of sample
expressions. -
10:59 - 11:03That's giving me green.
-
11:03 - 11:04Okay.
-
11:04 - 11:07So, so again, I'm using a Python
Interpreter. -
11:07 - 11:08So you can kind of, this is just the
prompt. -
11:08 - 11:11These chevrons are the prompt.
-
11:11 - 11:14Create the variable xx, and
assign it to 2. -
11:14 - 11:17Retrieve the old value and an addition.
-
11:17 - 11:20Then print it out and put it back into xx
so xx -
11:20 - 11:24has 4.
yy, this is a multiplication, 440 times 12. -
11:24 - 11:29It is 5,280. yy over 1,000.
Now this is a little counter-intuitive -
11:29 - 11:35Because yy is an integer,
it then does it in a truncated division. -
11:35 - 11:42And so, 5,280 divided by 1000 is 5.
Now if, and, -
11:42 - 11:45and so that's an integer division.
We'll see in a second -
11:45 - 11:46about floating point division.
-
11:48 - 11:52Now we take the variable jj and we set it
to 23. -
11:52 - 11:56And now we're going to use the modular or
modulo or remainder operator. -
11:56 - 12:02Say what is jj, what is the remainder when
divide this jj by 5. -
12:02 - 12:05And so if you think about this, we take
old long division, -
12:05 - 12:0923 divided by 5, you end up with 4 and
then remainder 3. -
12:11 - 12:13The modulo operator, or the percent of
-
12:13 - 12:16the remainder operator, gives us back this
number. -
12:16 - 12:19And so that's why kk is 3.
-
12:19 - 12:23It is the remainder of 23 when
divided by 5, -
12:23 - 12:28or the remainder of the division
of 5 into 23. -
12:28 - 12:32And the raising to the power, 4 cubed.
That's not so nice. -
12:32 - 12:354 cubed is 4 star, star 3.
-
12:35 - 12:36And so that ends up being 64.
-
12:36 - 12:42So that's just operations.
Now, just like in algebra and mathematics -
12:44 - 12:50we have rules about when to which, which
operations happen first. -
12:50 - 12:53In general, things like the power happens
before the -
12:53 - 12:56multiplication and division, and then the
addition and subtraction happen. -
12:56 - 12:59And so there are some rules that, when
-
12:59 - 13:01you're looking at an expression and trying
to calculate -
13:01 - 13:06what its value is, if you don't have
parentheses, it follows these rules. -
13:06 - 13:10And so the, the most, the rule
that sort of -
13:10 - 13:14trumps all the rules is that parentheses
are always respected. -
13:14 - 13:16So a lot of us just write these with
-
13:16 - 13:20parentheses in place, even sometimes
though you don't need it. -
13:21 - 13:25Then after parentheses have been handled,
then it does exponentiation. -
13:25 - 13:26Then it does multiplication,
-
13:26 - 13:30division, and remainder.
And then it does addition and subtraction. -
13:30 - 13:34And then, when all else being equal, it
just works left to right. -
13:34 - 13:40So let's, let's look through an example.
So here is a -
13:40 - 13:47calculation that is, you know, 1, 1 plus 2
times 3 divided 4 over 5. -
13:47 - 13:51And the question is, what order does this
happen, okay? -
13:51 - 13:53And so let's sort of take a look at this.
-
13:55 - 13:59And so, we start with are there any
parentheses? -
13:59 - 14:01And the answer is no, there are no
parentheses. -
14:01 - 14:05So let's go next.
Power. -
14:05 - 14:11And so the, the power says okay, let's
look across and find those things that -
14:11 - 14:17are raised to a power. And 2 cubed or 2 to
the third power is the, the power. -
14:17 - 14:20So we're going to do that one.
Okay. -
14:20 - 14:22And then we can, the way I do it when
-
14:22 - 14:24I'm sort of doing these slowly is I
rewrite it. -
14:24 - 14:29So the 2 to the third power becomes 8, so
it's 1 plus 8 over 4 times 5. -
14:29 - 14:32And then now we can say oh power, that's
taken care of. -
14:32 - 14:36Now we're going to do multiplication and
division and we go across. -
14:36 - 14:39Now we have both a division and
multiplication. -
14:39 - 14:40Okay? Multiplication and division are done
at the same -
14:40 - 14:43time, so that means we do left to right,
-
14:43 - 14:45which means we do the first one we
encounter first. -
14:45 - 14:54And so that will be 8 over 4 because of
the left-to-right rule. -
14:54 - 14:55And so we find that one, and that's the
-
14:55 - 14:59one that gets computed next, and that
turns into 2. -
14:59 - 15:01And again, I like to rewrite these
expressions -
15:01 - 15:04just to keep my brain really, really
clear. -
15:04 - 15:07After a while you just do it in your head,
but I rewrite them. -
15:07 - 15:08When I was first learning it,
-
15:08 - 15:09at least, I rewrote it all the time.
-
15:11 - 15:15And and so next looking at this, there's a
multiplication. -
15:15 - 15:20We're not done with multiplication yet.
So the 2 over 5 is the next thing. -
15:21 - 15:25And then we do that calculation, and that
becomes 10, and again we rewrite it. -
15:25 - 15:29And now we've done the multiplication, and
we're going to do addition next. -
15:29 - 15:34And that's just 1 over 10, and that
becomes 11. -
15:34 - 15:37And so basically, this big long thing,
-
15:37 - 15:41through a series of successive steps,
becomes 11. -
15:41 - 15:44And indeed, when we print it out, that's
what we get. -
15:44 - 15:45Okay?
-
15:47 - 15:49So, there's the rules that are
parentheses, -
15:49 - 15:52power, multiplication, addition, and then,
left to right. -
15:52 - 15:59But smart people usually just put
parentheses in, you know? -
15:59 - 16:02So here's this, here's an exam.
Oop, go back, go back. -
16:02 - 16:03Here's an exam question.
-
16:03 - 16:09Now, I wouldn't write this code, right, I
wouldn't write this code this way. -
16:09 - 16:11I would put a parentheses here.
-
16:12 - 16:13And a parentheses there.
-
16:15 - 16:19It's the same thing because that's exactly
the 2 times 3 is going to happen and -
16:19 - 16:214 over 5 is going to happen and then the
-
16:21 - 16:23plus and the minus will happen
left to right. -
16:23 - 16:26But why not make it easier on your readers
-
16:26 - 16:28and just put the parentheses in. Because
they're redundant. -
16:28 - 16:31They're not necessary, but away you go.
-
16:31 - 16:35Now, if you don't want it to happen in
that order, of -
16:35 - 16:38course then you have to put parentheses if
you want the addition -
16:38 - 16:41to happen before the multiplication, then
you -
16:41 - 16:43have to put parentheses in, which you can.
-
16:43 - 16:48But we tend to recommend that you use more
parentheses rather than less parentheses. -
16:50 - 16:54Now, Python integer division
in Python 2, -
16:54 - 16:56which we're using Python 2
for this class. -
16:56 - 17:00There's a new Python 3 that the world is
slowly transitioning -
17:00 - 17:03to and a lot of people are
using it in teaching. -
17:03 - 17:08But it's not as common, sort of, in the
real world with libraries and utilities. -
17:08 - 17:11And so we'll stick with
Python 2 for a few -
17:11 - 17:15more years until Python 3 really kind of
turns the corner. -
17:15 - 17:18It's nice to have it there, but there's so
much Python and it's so -
17:18 - 17:23popular, Python 2, that it's just kind of
hard to get everybody up to Python 3. -
17:23 - 17:29So in Python 2, integer division truncates
and you saw that before where -
17:29 - 17:34I did the 5280 by 1000 and I got 5 as and,
and, but we -
17:34 - 17:38can look at a couple of examples that make
this really very quite, quite clear. -
17:38 - 17:41So, 10 divided by 2 is 5 as you would
expect. -
17:41 - 17:439 Divided by 2 is 4.
-
17:43 - 17:45Not exactly what you'd expect.
-
17:45 - 17:49You kind of expect that to be 4.5,
instead of 4. -
17:49 - 17:54But in Python 3, it will be 4.5, but for
now, in Python 2, -
17:54 - 18:019 over, 9 over 2 is 4.
And 99 over 100 is 0. -
18:01 - 18:04Now that seems rather counter-intuitive,
but it is a truncating -
18:04 - 18:07division, it's not a rounding division,
it's a truncating division. -
18:07 - 18:11Now, interestingly, if you make either of
these numbers have a decimal, make them -
18:11 - 18:16what we call floating point numbers, then
the division is done in floating point. -
18:16 - 18:20So, 10.0 over 2.0
-
18:20 - 18:24is 5.0.
Now, these are different. -
18:24 - 18:27This is an integer number, and this is a
floating point number. -
18:27 - 18:28It's 5.0.
-
18:28 - 18:32And then 99.0 over 100.0 is exactly as you
-
18:32 - 18:35would expect, and it's a floating point
number, so. -
18:37 - 18:41Now you can also mix integers and floating
point numbers as you go. -
18:41 - 18:43So here we have 99 over 100.
-
18:43 - 18:47Those are both integers.
Integer, integer. -
18:47 - 18:50And, or, and that comes out with 0 because
it's truncating. -
18:50 - 18:53Now if we have an integer and
a floating point -
18:53 - 18:57number, 99 over 100.0, then that comes out
as 0.99. -
18:58 - 19:02And either one, if we have 99 over 100,
that's a floating point, and -
19:02 - 19:03that's an integer.
-
19:03 - 19:07We still end up with a floating point, so
this is a floating point, floating point. -
19:07 - 19:11And even in complex expressions, as it
evaluates when -
19:11 - 19:13it sees an integer, so the first thing
when -
19:13 - 19:21you evaluate is this would become a 6, so
it would be 1 plus 6 over 4.0 minus 5. -
19:21 - 19:27Then it would be doing the 6 over 4.0 and
that would be 1.5, 1 plus 1.5 -
19:27 - 19:31minus 5. And so this is an integer
and that's -
19:31 - 19:34a floating point and the result becomes a
floating point. -
19:34 - 19:37And then the rest of the calculation is
done floating point -
19:37 - 19:41to the point where the ultimate is a
floating point negative 2.5. -
19:41 - 19:45So you can throw a floating point into a
calculation and as soon as the -
19:45 - 19:48calculation touches the floating point,
the remainder -
19:48 - 19:51of the calculation is done in
floating point. -
19:51 - 19:53It kind of converts at the floating point
but it doesn't -
19:53 - 19:56want to convert it back because it
considers floating -
19:56 - 19:59point sort of the more general of the
representations. -
20:02 - 20:07So, here we are, talking about integers
and floating points. -
20:07 - 20:11These are a concept in programming
languages and in Python called type. -
20:12 - 20:14Variables and constants have a type.
-
20:16 - 20:19We can see that if you say 1, versus 1.0,
-
20:19 - 20:22they have different, they, it works
different, it functions differently. -
20:22 - 20:28And so Python keeps track of both
variables and literals/constants, and -
20:28 - 20:32having them have a type.
And we've seen this, right? -
20:32 - 20:35Now, the interesting thing is, is Python
is very aware of -
20:35 - 20:40the type and can use the same syntax to
accomplish different things. -
20:40 - 20:44So if we look at this line here, where we
say dd equals 1 plus 4. -
20:44 - 20:46Well it looks at the 1 and looks at the 4
and it says, -
20:46 - 20:49oh those are two integers. I will add those
together and give you a 5. -
20:49 - 20:53So it gives you an integer, an integer, and an
integer comes back, -
20:53 - 20:53Okay?
-
20:53 - 20:58And then ee equals 'hello ' plus 'there'.
Well these are two strings, -
20:58 - 21:03'hello ' and 'there'. And it says hmm, this must
be a concatenation. -
21:03 - 21:07Alright? So I'm going to concatenate those
together because -
21:07 - 21:10those are strings and I know how to
concatenate strings. -
21:10 - 21:13And that's kind of like string addition,
right? -
21:14 - 21:18And so we see a "hello there" as a result.
Now the interesting thing is, where -
21:18 - 21:22did this space come from?
Let me change colors here. -
21:22 - 21:23Oops.
-
21:23 - 21:27Where did that space come from?
Well, the plus does not add the space. -
21:27 - 21:30Here's a space right there, and that's the
space. -
21:30 - 21:35So I can concatenate it, hello space plus
there, and that's how I got hello there. -
21:35 - 21:37But, the key thing is, is this plus
-
21:37 - 21:43operator, clear, this plus operator looks
to either side -
21:43 - 21:44and says oh,
-
21:44 - 21:47they're strings.
I think you mean concatenation. -
21:47 - 21:49Here it looks either side and says oh,
-
21:49 - 21:52those are integers, I think you mean
addition. -
21:52 - 21:58So Python is very aware of type and type
informs Python what you really mean. -
21:58 - 21:59So, it looks like those are kind
-
21:59 - 22:01of the same, but they're quite different
operations. -
22:04 - 22:09So the type can get you into trouble.
Remember Python is looking at the type. -
22:09 - 22:11So here we have a little problem, our
-
22:11 - 22:15first traceback, first of many
tracebacks. -
22:15 - 22:20So here we have ee which is hello there
which is -
22:20 - 22:22exactly what we did. This is a string and
this is a string. -
22:22 - 22:27So ee should be a string. And then we try
to add 1 to it. -
22:27 - 22:29And again, Python is saying oh, I see
-
22:29 - 22:32a plus sign here, so I'm going to look
over here, yeah, -
22:32 - 22:34that's a string, and look over here, and
that's an integer. -
22:34 - 22:38And it's like, aaah! And this is a traceback.
-
22:38 - 22:41Now, here's a good time to talk about
tracebacks. -
22:41 - 22:43Tracebacks, I color them red.
-
22:43 - 22:47Because you might think that Python
dislikes you or -
22:47 - 22:51thinks that you're, you know, unworthy of
its brilliance. -
22:52 - 22:54And certainly the way these things are
worded it sounds like, -
22:54 - 22:58you know, the, you're being scolded.
It's like, hey, type error. -
22:58 - 23:02You can, cannot concatenate str and int
objects, right? -
23:02 - 23:06That's, I'm, I'm scolding you, you bad,
bad programmer. -
23:06 - 23:08And it does feel a bit like you're
scolded. -
23:08 - 23:12But, if you go back to lecture one, this
is also -
23:12 - 23:16the moment where, really, we shouldn't
think of this as like scolding. -
23:16 - 23:19We should think of this as Python sort of
asking for help. -
23:19 - 23:20It's like,
-
23:20 - 23:26wow, you gave me this line, and I, Python,
have no idea. -
23:26 - 23:29In all your greatness, could you give me
some possible -
23:29 - 23:31clue as to what you really
mean for me to do? -
23:31 - 23:32Because I'm so lost.
-
23:32 - 23:36And given that I'm Python and I'm lost and
you are the only -
23:36 - 23:41purpose for my existence, I must stop
until you give me better guidance. -
23:41 - 23:45So, don't look at tracebacks as scolding.
-
23:45 - 23:50They sound like scolding.
I'll stop coloring them red after a while. -
23:50 - 23:54So, if Python is so obsessed with the type
of things, you -
23:54 - 23:57should be able to ask Python what the type
of something is. -
23:57 - 24:00So there's a built-in function called
type. -
24:00 - 24:01This is part of the Python language.
-
24:01 - 24:05Type (), and you can put a variable in
here. -
24:05 - 24:06What's the type of the variable ee?
-
24:06 - 24:10And it says, oh yeah, I know what that is,
that would be a string. -
24:10 - 24:12And then you can also put a constant in
here. -
24:12 - 24:16And say what's the type of quote, hello,
quote, and that's a string too. -
24:16 - 24:17And what's the type of the number 1?
-
24:17 - 24:19Well that would be an integer.
-
24:19 - 24:21So it's picky about the type, but it will
-
24:21 - 24:24also share with you what it believes the
type is. -
24:25 - 24:28And there's several types of numbers.
-
24:28 - 24:32As I've already mentioned, there are
integers, which are the whole numbers. -
24:32 - 24:34They can be positive and negative and
zero. -
24:34 - 24:35And then there are the decimal numbers,
-
24:35 - 24:42the floating point numbers,
like 98.6 or negative 2.5 or 14.0. -
24:42 - 24:46Python knows these as well because it does
division different if it's presented with -
24:46 - 24:50two integers, or an integer and a float,
or a float and a float. -
24:54 - 24:58And so here we have x is 1, and we'll say
what is it? -
24:58 - 24:59It's an integer.
-
24:59 - 25:02And we say it's 98.6, and we'll say, well,
what's that? -
25:02 - 25:03It's a float.
-
25:03 - 25:05And you can ask for both variables and
constants. -
25:05 - 25:07So what's the type of 1? It's an integer.
-
25:07 - 25:10And what's type of up 1.0?
And it's a float. -
25:12 - 25:13You can also convert types.
-
25:13 - 25:16It has a bunch of type conversion
functions built into it. -
25:16 - 25:19So, there's implicit conversion going on
-
25:19 - 25:23when you're sort of saying, you know,
divide an integer by a floating point. -
25:23 - 25:26It says okay I see, I look to
the sides and -
25:26 - 25:29I will make the, I will make the
conversion for you. -
25:29 - 25:30But you can also be explicit.
-
25:30 - 25:33So in this case we're going to say,
take this -
25:33 - 25:3699 and convert to a floating point
version of itself. -
25:36 - 25:39Which is 99.0.
And then do the division. -
25:39 - 25:42So Python looks out here and goes oh,
after that, that's -
25:42 - 25:45a float, and that's an integer if I look
over here. -
25:45 - 25:48And then that means that the
result is a float. -
25:48 - 25:49And the division is done as a float.
-
25:49 - 25:55So we are force converting the 99 integer
into a 99.0 float. -
25:57 - 25:59And we can even do this like and just
stick it in the variable. -
25:59 - 26:03So we can just put 42 in i and that is an
integer. -
26:03 - 26:07Then we can say, hey, convert float that i
-
26:07 - 26:10into a float and stick it into the
variable f. -
26:10 - 26:14And so we can print it.
And now it's 42.0 instead of 42. -
26:14 - 26:16Right? They're not the same.
-
26:16 - 26:18They're both kind of 42, but one is a
-
26:18 - 26:21floating point 42 and the other is an
integer 42. -
26:21 - 26:24And we can ask, and that is a float.
-
26:24 - 26:26And you can also do the same thing in the
middle of -
26:26 - 26:31a calculation, where you have 1 plus 2
times a float of 3. -
26:31 - 26:35This float is done quickly.
So the first thing that happens -
26:35 - 26:39this is 1 plus 2 times 3.0 over 4
minus 5. -
26:39 - 26:40So
-
26:42 - 26:44the first thing that happens is these
floats -
26:44 - 26:46are done because they are parentheses so
they matter. -
26:46 - 26:50So this is a built-in function called
float that takes, as its -
26:50 - 26:55argument, a non-floating point number and
gives you back a floating point number. -
26:55 - 26:57We'll talk more about functions
in Chapter Four. -
27:01 - 27:06You can also convert between strings and
numbers, and if you -
27:06 - 27:10recall, I, we did the example where we
took a string. -
27:10 - 27:13In this case, I'm being a little
confusing, because -
27:13 - 27:16I'm making a string with the
characters 1, 2, 3. -
27:16 - 27:19Now, this is not the same as 123.
-
27:19 - 27:24This is a three-character string
with 1, 2, 3 in it. -
27:24 - 27:26And I can ask what kind of thing is in
there, and it says, -
27:26 - 27:29oh, there's a string in there.
I know about that. -
27:29 - 27:30And then I can try to add 1 to it, and
-
27:30 - 27:36it seems intuitive that quote 123 plus 1
would be somehow 124. -
27:36 - 27:38But it's not.
-
27:38 - 27:40Python takes a look at the plus and says,
oh there's -
27:40 - 27:43a string on that side, and an integer on
that side. -
27:43 - 27:46I am going to freak out and tell you
-
27:46 - 27:49that you cannot concatenate
a string and an integer. -
27:49 - 27:52Okay?
But there is an int function -
27:52 - 27:55that converts various things, including
strings, to an integer. -
27:55 - 28:01So we can give as its parameter, its input,
the string value, then it -
28:01 - 28:05converts it to an integer, and then we'll
put the result in the variable ival. -
28:05 - 28:10We can ask what the type of that is, it's
an i, it's a integer. -
28:10 - 28:13And now we can use it in an expression,
print ival plus 1, and -
28:13 - 28:17so now Python looks to both sides, sees an
integer, sees an integer, and -
28:17 - 28:20gets 124.
Voila. -
28:21 - 28:25Now, if I make a new variable and I stick
hello Bob in it, and I -
28:25 - 28:31say hey, let's convert hello Bob to an
integer, as you might expect, it blows up. -
28:31 - 28:34And it says, invalid literal
for int. -
28:36 - 28:42These, these tracebacks again, once you
kind of get used to the kind of harsh -
28:42 - 28:45wording of them, because they're not
saying, sorry, comma, -
28:45 - 28:48they're trying to tell you what's
going on. -
28:48 - 28:53So, cannot concatenate string and integer,
and invalid literal for int. -
28:53 - 28:55It's trying to be as helpful as it
possibly can -
28:55 - 28:58be to give you a clue as to what to fix.
-
28:58 - 29:00So, again, not scolded.
-
29:02 - 29:06Okay, so that's variables and types and
type conversion. -
29:06 - 29:10Now we'll talk a little bit about
user input. -
29:10 - 29:15And there's a function that's built into
Python called raw_input. -
29:15 - 29:21And what happens when raw_input runs is
it, it has as one of -
29:21 - 29:25its parameters, a prompt, which is
something that shows up on the screen. -
29:25 - 29:26Who are you?
-
29:26 - 29:28And then,
-
29:28 - 29:35it waits, tik, tik, tik, tik, tik.
Sits and waits, says, what next? -
29:35 - 29:37And then, you type a string,
dot, dot, dot, dot, dot, -
29:37 - 29:39and then you hit the Enter key.
-
29:41 - 29:42The Enter key.
-
29:42 - 29:47And then whatever you typed here goes
into a variable. -
29:49 - 29:53And it is a string.
And, then you, -
29:53 - 29:54then you can use it.
-
29:54 - 29:56So I'm going to print the string Welcome,
-
29:56 - 29:59comma. So that means I'm printing two
things now. -
29:59 - 30:02The comma adds a space between Welcome and
then nam, and so -
30:02 - 30:07Welcome is a literal, and then Chuck is
coming from this nam variable. -
30:07 - 30:09So this is a two-line program.
-
30:09 - 30:12I think this is one of your
assignments, actually, -
30:12 - 30:16to well, it's one of the exercises
in the book. -
30:16 - 30:18To a prompt for a user's name, and
-
30:18 - 30:20then welcome them, okay?
-
30:21 - 30:26So raw_input is a function that issues a
prompt, waits, and then takes whatever -
30:26 - 30:30string that's entered, and then returns it
and then puts it into that variable. -
30:33 - 30:38So, now we're going to create kind of the
first useful program. -
30:38 - 30:41It's not a powerful program.
-
30:41 - 30:49It is a, an interesting problem of the
fact that for some reason there -
30:49 - 30:50is a difference in the numbering scheme
-
30:50 - 30:53of United States elevators and
European elevators. -
30:54 - 30:58European elevators, the floor that you
walk out on is the -
30:58 - 31:00zero floor.
-
31:00 - 31:02The floor above that is the
one floor and the -
31:02 - 31:06floor below that, the basement,
is the minus one floor. -
31:06 - 31:11And so you walk in and you can go either
up the elevator or down the elevator. -
31:11 - 31:15Of course, in the United States, the floor
that you walk in is the -
31:15 - 31:20one and then there's the two floor above
that and then there's like, the basement. -
31:20 - 31:23So this is the
imagination that the Americans -
31:23 - 31:26have as to how to number floors, right?
-
31:26 - 31:29The Europeans go zero, one, minus one.
-
31:29 - 31:35So, children who go to hotels learn
instantly the notion of zero and the -
31:35 - 31:37notion of positive and negative
numbers and -
31:37 - 31:39the symmetry between positive and
negative numbers. -
31:39 - 31:45I mean, I just wish the United States
hotels would switch to this -
31:45 - 31:49to teach young people zero immediately and
-
31:49 - 31:50negative numbers.
-
31:50 - 31:54So we somehow think that numbers all in
the United States start at 1 -
31:54 - 31:57and then there are no
negative numbers, there's the -
31:57 - 31:58basement.
-
32:00 - 32:02I wonder why that is, but whatever.
-
32:04 - 32:07For people who travel a lot, they may be
confused by this. -
32:08 - 32:10They need a way to convert back and
-
32:10 - 32:14forth between the US and European
numbering system. -
32:16 - 32:18So this is a simple program that
demonstrates -
32:18 - 32:22a real classic pattern of input processing
and output. -
32:22 - 32:25It's just three lines, but it has the
-
32:25 - 32:29essential things that all programs that
are useful. -
32:29 - 32:33They generally read some data,
do some work with -
32:33 - 32:36the data, and then produce some
kind of results. -
32:36 - 32:41And so, so the first line is a raw_input
-
32:41 - 32:45that effectively, that puts out a prompt
and then it waits. -
32:45 - 32:49It says, please enter your Europe floor.
It sits there. -
32:49 - 32:51We type a zero,
-
32:51 - 32:54then zero goes into inp, but it is a
string. -
32:55 - 32:56It's not a number.
-
32:56 - 32:58It's a string.
-
32:58 - 33:01So we can't add to it. But we can take
-
33:02 - 33:05and convert it to an integer with
the int function. -
33:05 - 33:08Int of inp, thats a string being
converted to an integer -
33:08 - 33:10so now its a real numeric zero.
-
33:10 - 33:14And we can add 1 to that and we
sum that together. -
33:14 - 33:16And we put it into the
-
33:16 - 33:21variable usf and then we print US floor,
comma, and then -
33:21 - 33:25whatever the variable for usf is. And out
comes US floor 1. -
33:25 - 33:29So we've written a very simple elevator
floor conversion -
33:29 - 33:32from a European floor to a
United States floor. -
33:33 - 33:36Don't ask about negative numbers, it's not
really good at that. -
33:36 - 33:39But from zero and positive numbers it
works great. -
33:43 - 33:48So another thing to think about in any
programming language is comments. -
33:50 - 33:54Comments are like commentary, you know,
and basically it's a way for us to -
33:56 - 34:00add notations for ourselves and for other
humans interspersed in the code. -
34:01 - 34:07And so in Python anything after a pound
sign is ignored. -
34:07 - 34:08You can have a pound sign at the beginning
-
34:08 - 34:10of a line and then the
whole line is ignored. -
34:10 - 34:13There are two or three reasons why you
could do this. -
34:13 - 34:16One is sort of like paragraph headings,
where you can -
34:16 - 34:21say what's going to happen in English or,
or your language. -
34:21 - 34:23And you can write documentation says this
-
34:23 - 34:28code was written by Charles Severence,
December 2010. -
34:28 - 34:30And you can also just hide
a line of code to -
34:30 - 34:33test and, and turn it on and off without
actually deleting -
34:33 - 34:37the line of code.
It's a real common thing in debugging. -
34:37 - 34:43So for example, here is a, here is a, the
program that we've been playing with. -
34:43 - 34:45This is our word counting program that
-
34:45 - 34:47we've been talking about
from the beginning. -
34:47 - 34:52And here is an example of four comments,
one, two, three, four. -
34:52 - 34:56Four comments that basically tell us what
these paragraphs are going to do. -
34:56 - 34:59Now, they don't have any effect on the
program whatsoever. -
34:59 - 35:01But this one says get the name of the file
and open it. -
35:02 - 35:04Kind of helpful, right?
-
35:04 - 35:05Count the word frequency.
-
35:05 - 35:08That's what this little bit does.
Find the most common word. -
35:08 - 35:09That's what this little bit does.
-
35:09 - 35:12And all done, print this out.
-
35:12 - 35:16So it's really can be very helpful just to
add a little tiny bit of stuff. -
35:16 - 35:18And you don't want to overuse comments.
-
35:18 - 35:23You don't want to say like x equals 12,
take 12 and put it into x. -
35:23 - 35:25Sometimes people teach
-
35:25 - 35:28you and try to say, oh you need a one
comment every three lines. -
35:28 - 35:30I don't believe in any of those rules.
-
35:30 - 35:33I basically say if its useful to describe
it, then describe it. -
35:35 - 35:41So that's comments.
So some operators apply to strings. -
35:41 - 35:43We've already talked about plus.
-
35:43 - 35:46It's kind of silly, although
useful in places. -
35:46 - 35:50You can actually multiply strings.
The asterisk looks and -
35:50 - 35:54says I've got a string and an integer, and
it prints out the string five times. -
35:55 - 35:56Not a lot of use for that.
-
35:58 - 36:01Now, let's talk a little bit about
choosing variable names. -
36:01 - 36:04This is something that is
really confusing. -
36:04 - 36:08So I said like, x equals 1, x equals x
plus 1, what does x mean? -
36:08 - 36:12And the answer is, it doesn't
mean anything. -
36:12 - 36:15I chose it.
I wanted to make a variable, -
36:15 - 36:17and so I picked x.
-
36:17 - 36:19We pick x a lot, probably because
we learned -
36:19 - 36:23in algebra in sixth grade that
x was a variable. -
36:23 - 36:26So, and it's short, and so,
why not call it x? -
36:29 - 36:33But as your programs get larger this gets
kind of frustrating -
36:33 - 36:36to have all your variables like x
and y and z. -
36:36 - 36:39And so the notion of mnemonic, it means
memory aid. -
36:39 - 36:44We choose our variable names wisely, so
they remind us of what the variable's -
36:44 - 36:49going to do internally.
And so, as I go through this lecture -
36:52 - 36:55in the beginning if I choose a variable
that's too clever -
36:55 - 36:59you're going to think that it's
part of the language. -
36:59 - 37:03And so I sort of switch back and forth
between well-chosen variable names -
37:03 - 37:07and stupid variable names to kind of
reemphasize the notion that I can choose. -
37:07 - 37:10Mnemonic is a good practice, okay?
-
37:10 - 37:14So here we go.
Let's take a look at a bit of code. -
37:17 - 37:21So the question is, what is
this code doing? -
37:21 - 37:23What will it even print out?
-
37:23 - 37:25Is it syntactically correct?
-
37:27 - 37:33Now you could probably cut and paste this
into your brow, into Python and figure -
37:33 - 37:39out that it is syntactically correct.
There are three variables. -
37:41 - 37:47This one here and this one here match.
-
37:48 - 37:53This one here and that one there match.
And these two match. -
37:54 - 37:55So it's taking these two numbers and
-
37:55 - 37:58multiplying together, and then printing
out the product -
37:58 - 38:04of the two numbers, if you're real careful
and like look at every, every character. -
38:04 - 38:07Now, this would be called
non-mnemonic variables. -
38:07 - 38:09They're really messy.
-
38:09 - 38:13Now Python, it's happy, because all it
wants is to say, oh. -
38:13 - 38:14Here is then name that
-
38:14 - 38:17I, the programmer, decided I
wanted to call this -
38:17 - 38:20piece of memory and I'll refer to
it down here, okay? -
38:20 - 38:23And so Python is happy.
-
38:23 - 38:27Now if you hand this to another human
being they are going to be really unhappy. -
38:27 - 38:29Because they are going to be like, what
are you doing? -
38:30 - 38:36So one better way to write it would be to
make the variables very simple. -
38:36 - 38:39And then cognitively we humans can figure
out which is which, -
38:39 - 38:43because again it's still only
about matching. -
38:43 - 38:48The a has to match the a, the b matches
the b, and the c's match. -
38:48 - 38:50It's actually the exact same program.
-
38:50 - 38:54A equals 35.
B equals 12.5. -
38:54 - 38:55C equals A times B.
-
38:55 - 38:58And print C.
It is these. -
38:58 - 39:01Python sees these as the same program.
-
39:01 - 39:04It doesn't care what we name them.
Now, a human will -
39:04 - 39:09be much appreciative if you say, here you
can either have this one or this one. -
39:09 - 39:11This one will make them a lot happier.
-
39:13 - 39:14Okay?
-
39:14 - 39:19So that is certainly cognitively easier,
but it's not really -
39:19 - 39:23giving you any sense of what's going on
here, right? -
39:23 - 39:29So an even better way to write this exact
same program to do the exact same thing -
39:29 - 39:32would be to choose variables
named hours, rate, and pay, -
39:32 - 39:36if indeed that is what you're doing.
-
39:36 - 39:39Now you can look at this and you go,
oh well, shoot, -
39:39 - 39:4335 is the number of hours, and 12.5 is the
rate, and the pay is -
39:43 - 39:46the number of hours times the rate, and
then we are going to print out the pay. -
39:46 - 39:48And that makes a lot of sense.
-
39:48 - 39:54So this is really a awesome and wonderful
characterization. -
39:54 - 39:57And if that's what you're doing
and those are hours, -
39:57 - 40:00rate, and pay, it's a great thing
to name the variables. -
40:00 - 40:04But, this is where beginning students
get confused. -
40:04 - 40:07And so sometimes I'll write it this way
and sometimes I'll write it this way. -
40:07 - 40:10Because you'll look at this, until you get
a little -
40:10 - 40:13more sophisticated, a little more skilled,
and you'll say like -
40:15 - 40:19does Python know something about payroll?
Is hours a reserved word? -
40:19 - 40:22Is rate a reserved word and pay
a reserved word? -
40:22 - 40:26Are these things that Python knows about?
And the answer is, no. -
40:26 - 40:30Python sees these three programs as
exactly the same name. -
40:30 - 40:34It's just this person really made a very
bad choice of variable name. -
40:34 - 40:38This person made a less bad choice of
variable name, -
40:38 - 40:41and this person made a really awesome
choice of variable name. -
40:41 - 40:43So the only difference between those two
things is style. -
40:45 - 40:47They are the exact same program.
-
40:47 - 40:51And Python is equivalently happy with
these, but humans -
40:51 - 40:55are most happy when the variables are
easy to remember -
40:55 - 40:59and they are somewhat descriptive of what
their expected contents will be. -
41:00 - 41:02That's mnemonic.
-
41:02 - 41:06To help you remember what you were meaning
to do when you write the program. -
41:06 - 41:08This is still a bit cryptic, having these
-
41:08 - 41:10really short, one-character variable
names is still -
41:10 - 41:12a bit cryptic.
So, -
41:14 - 41:17You have a couple of assignments at the
end of the chapter. -
41:17 - 41:21One of the assignments is to write a
program to prompt -
41:21 - 41:25the user for hours and rate per hour and
compute pay. -
41:26 - 41:33So, I won't do this here, but just a
couple of sort of odd things. -
41:33 - 41:35You're going to be using raw_input.
-
41:35 - 41:40But remember that hands a string in so
you're going -
41:40 - 41:42to have to use float.
-
41:44 - 41:46The function to convert it to a floating
-
41:46 - 41:48point number so you can actually do a
calculation. -
41:48 - 41:51And then you're going to have to use
multiplication and print. -
41:51 - 41:53So then multiplication, and then print.
-
41:55 - 42:00So some combination of raw input, float,
multiplication, and print, -
42:01 - 42:04constructed to make your program do
exactly this. -
42:06 - 42:08So, this is the end of Chapter Two.
-
42:08 - 42:09We talked about types,
-
42:09 - 42:14reserved words, variables, the
mnemonic, how you choose variable names. -
42:14 - 42:16We'll hit this a couple more times
-
42:16 - 42:18because choosing variable names is always
problematic. -
42:18 - 42:22Operators, operator precedence, which just
means like does multiplication happen -
42:22 - 42:27before plus, parentheses.
Integer division is a little weird because -
42:27 - 42:34it truncates, oops, right, 9 over 10.
-
42:34 - 42:41Oops, 9 over 10 equals 0.
That's the integer division truncating. -
42:41 - 42:48Conversion, this is like the int()
float(). -
42:48 - 42:50And then user input, which is raw_input.
-
42:50 - 42:52And then comments, which are ways for you
-
42:52 - 42:56to add human-readable text to your
program. -
42:56 - 42:58Okay? See you next lecture.
- Title:
- Python for Informatics: Chapter 2 - Expressions
- Description:
-
This is the second chapter of Python for Informatics. We cover constants, variables, reserved words, assignment statements, operators, types, type conversion functions, input using raw_input(), and the basic structure of a sequential program. Material provided by www.pythonlearn.com.
All Lectures: http://www.youtube.com/playlist?list=PLlRFEj9H3Oj4JXIwMwN1_ss1Tk8wZShEJ - Video Language:
- English
- Team:
- Captions Requested
- Duration:
- 42:58
Claude Almansi edited English subtitles for Python for Informatics: Chapter 2 - Expressions |