Return to Video

Python for Informatics: Chapter 2 - Expressions

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

more » « less
Video Language:
English
Team:
Captions Requested
Duration:
42:58

English subtitles

Revisions