< Return to Video

Intro to tracing program execution | Intro to computer science - Python | Khan Academy

  • 0:00 - 0:03
    - Let's trace a program step by step.
  • 0:03 - 0:04
    This is a common pattern we'll use
  • 0:04 - 0:07
    to understand what the computer
    is doing under the hood
  • 0:07 - 0:08
    when we press the Run button.
  • 0:08 - 0:11
    Tracing program execution like this
  • 0:11 - 0:13
    helps us better read and write programs
  • 0:13 - 0:14
    because we can start to predict
  • 0:14 - 0:17
    what the computer's going
    to do with each instruction
  • 0:17 - 0:18
    without having to go through
  • 0:18 - 0:20
    a long process of trial and error.
  • 0:20 - 0:22
    For now, we'll write this all out together
  • 0:22 - 0:23
    so we can make sense of
    what's happening here,
  • 0:23 - 0:26
    but eventually, we'll get
    familiar enough with Python
  • 0:26 - 0:28
    where we can trace small
    blocks of code like this
  • 0:28 - 0:29
    in our heads.
  • 0:30 - 0:31
    When you press Run,
  • 0:31 - 0:34
    the computer looks at
    your program line by line.
  • 0:34 - 0:36
    It isn't smart enough to zoom out
  • 0:36 - 0:38
    and look at the big picture
    and try and understand
  • 0:38 - 0:41
    what the program is
    trying to do as a whole.
  • 0:41 - 0:43
    When I say line by line, I literally mean
  • 0:43 - 0:47
    that the computer is going to
    load each line of your program
  • 0:47 - 0:49
    into its working memory one by one.
  • 0:49 - 0:51
    And then with one instruction
  • 0:51 - 0:52
    loaded into its working memory,
  • 0:52 - 0:55
    it's going to interpret that
    instruction in isolation,
  • 0:55 - 0:57
    in total isolation.
  • 0:57 - 0:59
    Now, remember that the
    computer is not a mind reader.
  • 0:59 - 1:02
    So it doesn't understand any nuance
  • 1:02 - 1:04
    or intention behind your instruction.
  • 1:04 - 1:07
    It's only going to do literally
    what that instruction says
  • 1:07 - 1:09
    according to whatever rules of Python.
  • 1:09 - 1:12
    Let's see that in action
    by tracing this program.
  • 1:12 - 1:14
    The computer takes the
    first line of the program
  • 1:14 - 1:16
    and loads that into working memory.
  • 1:16 - 1:18
    Then the first thing it's gonna do
  • 1:18 - 1:20
    is look for any expressions to evaluate.
  • 1:20 - 1:22
    Remember that evaluating an expression
  • 1:22 - 1:25
    just simplifies it down to a single value.
  • 1:25 - 1:27
    Here we have the expression,
  • 1:27 - 1:30
    the integer four plus the float, 20.55.
  • 1:30 - 1:34
    And that evaluates to the float 24.55.
  • 1:34 - 1:37
    Now, all our expressions
    have been simplified,
  • 1:37 - 1:39
    so the computer's gonna
    peek outside the parentheses
  • 1:39 - 1:42
    and ask, "Well, what did you
    want me to do with this value?"
  • 1:42 - 1:44
    The instruction print
    tells it to take the value
  • 1:44 - 1:48
    inside the parentheses and go
    display that in the console.
  • 1:48 - 1:52
    So it goes over here and it prints 24.55.
  • 1:52 - 1:53
    This instruction is complete,
  • 1:53 - 1:55
    so the computer's ready
    to move to the next step,
  • 1:55 - 1:58
    but first it wants to
    optimize its brain space.
  • 1:58 - 2:01
    It doesn't really need to
    remember this instruction anymore,
  • 2:01 - 2:03
    it doesn't need this information.
  • 2:03 - 2:06
    So it just clears out its
    working memory and forgets,
  • 2:06 - 2:09
    and that makes room for
    the next instruction.
  • 2:09 - 2:11
    Now, the computer loads the second line
  • 2:11 - 2:12
    into its working memory.
  • 2:12 - 2:15
    And again, it looks for any
    expressions to evaluate.
  • 2:15 - 2:18
    It sees the expression three plus two,
  • 2:18 - 2:21
    and it simplifies that
    down to the integer five.
  • 2:21 - 2:24
    Notice that there's no
    print instruction here.
  • 2:24 - 2:26
    We didn't actually ask the computer
  • 2:26 - 2:28
    to do anything with that value.
  • 2:28 - 2:29
    So the computer's thinking,
  • 2:29 - 2:31
    "Well, hey, I just did all this work.
  • 2:31 - 2:33
    I figured out the answer's five,
  • 2:33 - 2:35
    but I guess you don't want me to tell you.
  • 2:35 - 2:40
    So it shrugs whatever, and it
    clears its working memory out,
  • 2:40 - 2:43
    forgets that five, and just moves on.
  • 2:43 - 2:44
    Third line, the computer loads,
  • 2:44 - 2:48
    print the string learn
    plus the string space.
  • 2:48 - 2:50
    Careful, this is not the empty string.
  • 2:50 - 2:51
    There's one little space character
  • 2:51 - 2:53
    in between these quotation marks.
  • 2:53 - 2:55
    Plus the string more.
  • 2:56 - 2:58
    But there are two operators
    in this expression.
  • 2:58 - 3:00
    There's two plus signs.
  • 3:00 - 3:03
    So the computer's actually going
    to evaluate this expression
  • 3:03 - 3:05
    in two steps, reading left to right.
  • 3:05 - 3:09
    First, it evaluates the
    expression, learn plus space.
  • 3:09 - 3:10
    Now, when we add strings,
  • 3:10 - 3:12
    remember that we are concatenating.
  • 3:12 - 3:13
    We are smushing together.
  • 3:13 - 3:16
    So we get the string learn space.
  • 3:16 - 3:18
    Then we add the string more.
  • 3:18 - 3:21
    We concatenate and we
    get learn space more.
  • 3:22 - 3:23
    We're down to a single value,
  • 3:23 - 3:26
    so the computer peaks
    outside the parentheses,
  • 3:26 - 3:28
    sees that we wanted to print that value,
  • 3:28 - 3:32
    and it prints learn space
    more in the console.
  • 3:32 - 3:33
    Finally, it clears its working memory
  • 3:33 - 3:36
    and it moves to the next line.
  • 3:36 - 3:39
    What do you think the last
    two lines of this program do?
  • 3:39 - 3:41
    Take a second and try
    and trace it yourself.
  • 3:43 - 3:46
    Okay, this instruction has the expression,
  • 3:46 - 3:50
    the string 81 plus the string 19.42.
  • 3:50 - 3:52
    Now, these may look like
    integers and floats,
  • 3:52 - 3:55
    but because there are
    quotation marks around them,
  • 3:55 - 3:57
    the computer's going to
    treat them like strings.
  • 3:57 - 3:59
    So when we evaluate this expression,
  • 3:59 - 4:01
    we're concatenating strings
  • 4:01 - 4:06
    and we get the string 8119.42.
  • 4:06 - 4:07
    Nothing left to simplify here.
  • 4:07 - 4:09
    So the computer pops
    outside the parentheses,
  • 4:09 - 4:14
    sees the print, and then
    prints 8119.42 to the console.
  • 4:14 - 4:15
    Then it clears out working memory
  • 4:15 - 4:17
    and moves to the last line.
  • 4:17 - 4:19
    The computer loads the
    last line of the program
  • 4:19 - 4:21
    into working memory.
  • 4:21 - 4:23
    Notice that this whole thing
    inside the parentheses here
  • 4:23 - 4:25
    is surrounded by quotation marks.
  • 4:25 - 4:28
    That means this is already a single value.
  • 4:28 - 4:30
    It's the string, the two character,
  • 4:30 - 4:33
    space character, plus
    character, space character,
  • 4:33 - 4:34
    two character.
  • 4:34 - 4:37
    It's not the expression
    the integer two plus two.
  • 4:37 - 4:39
    Because we already have a single value,
  • 4:39 - 4:41
    there's nothing to simplify here.
  • 4:41 - 4:43
    So the computer pops out the parentheses,
  • 4:43 - 4:45
    sees the instruction print,
  • 4:45 - 4:47
    prints two space plus
    space two to the console,
  • 4:47 - 4:49
    and clears working memory.
  • 4:49 - 4:51
    Then it jumps to the
    next line of the program,
  • 4:51 - 4:52
    and oh, there is no next line.
  • 4:52 - 4:55
    We are at the end, we did it.
  • 4:55 - 4:57
    So the computer terminates
    the program execution,
  • 4:57 - 5:01
    it exits, and we have a final
    result here in the console.
  • 5:01 - 5:02
    Wanna check my work?
  • 5:02 - 5:04
    Copy this program into a code editor
  • 5:04 - 5:06
    and run it for yourself.
  • 5:06 - 5:07
    Is the result the same?
Title:
Intro to tracing program execution | Intro to computer science - Python | Khan Academy
Description:

more » « less
Video Language:
English
Team:
Khan Academy
Duration:
05:08

English subtitles

Revisions