English subtitles

← Variables Can Vary - Intro to Computer Science

Get Embed Code
5 Languages

Showing Revision 6 created 05/24/2016 by Udacity Robot.

  1. So the speed of light is a constant. But
  2. the important thing about variables in Python is that
  3. they can vary. That's why they're called variables. Once
  4. we define the variable, we can change the value.
  5. And then when we use that name again it
  6. refers to the new value. So let's see that
  7. in an interpreter. We could change the value of
  8. cycles_per_second. Suppose we have a faster processor. Now we've upgraded,
  9. we've got a 2.8 Ghz processor. Now, that doesn't
  10. affect the value of cycle distance. This was already computed
  11. with the old cycles_per_second. We still get the result, 0.111.
  12. We'll compute it again. This time the value of cycles
  13. per second is the new value since we changed
  14. what cycles per second refers to. And now when we
  15. print cycle_distance we see that the values changed. So we
  16. have a faster processor the second time we print cycle_distance
  17. now we only have 0.107 meters, less than 0.11
  18. centimeters per cycle. So what happens now that we
  19. have assignment, the same expression can have different meanings
  20. at different times we evaluate it. The value of the
  21. speed of light divided by cycles per second depends
  22. on what the current value of cycles per second is.
  23. When we evaluate the first one, the value was
  24. 2 billion 700 million, so we got a different result,
  25. which was the 0.111 result we got the first time.
  26. When we evaluate the same expression the second time, the
  27. result's different, because the value that cycles_per_second refers to is
  28. different. And that's why we get the smaller cycle_distance the
  29. second time. So, let's see what's going on there and
  30. make sure we understand assignment. So, suppose we have a
  31. variable, days. And we'll initialize it to the value 7
  32. times 7. So what that does is introduce a name days.
  33. And it refers to a value, which is the result
  34. of that expression. So it refers to the value 49, and
  35. that means when we look at the name days, we
  36. see what it refers to and we get the result, 49.
  37. If we do another assignment. Let's say we have one
  38. less day. And in this case we'll assign 48 to days.
  39. Well, that's a new assignment. We already have a name days.
  40. It used to refer to 49. But after the new assignment,
  41. it's going to refer to this new value. Now
  42. it's going to refer to the value 48. The
  43. number 49 still exists, but days no longer refers
  44. to it. Now days refers to 48. Where things
  45. get more interesting is we can use variables in
  46. their own assignment statements. So here we have an
  47. assignment statement where we have the value days minus
  48. 1 and we assign that to days. So what
  49. happens with that assignment? Well, we evaluate the right
  50. side first. We look for the value of days
  51. and we see that it refers to 48. We
  52. compute days minus 1 and we get the value 47.
  53. Then we do the assignment that will assign to
  54. the variable days. So now the value days refers to
  55. the value 47, no longer refers to 48. So
  56. we could keep doing that, if we did another statement,
  57. same exact one, that's going to change the value again.
  58. This time, the first time, the value days is 47,
  59. we'll subtract 1, we'll get the value 46. And then
  60. we do the assignment, that'll change the value, so now
  61. days refers to the value 46. So the important thing
  62. to notice, this is not an equal symbol. This looks
  63. like an equal symbol. If you studied algebra you would
  64. think an equation like this looks like an equality, and
  65. there is no way to solve an equation like that.
  66. In Python and in most programming language, equal does not mean
  67. equal. What equal means is assignment. You should really think of
  68. it as an arrow. It's an arrow saying put whatever value
  69. of the right side evaluates to, into the name on the
  70. left side. We don't write it as an arrow in most
  71. programming languages. There's some that do, just because an arrow is
  72. harder to type, and lots of programs have lots of assignments.
  73. But you should think of the equal
  74. sign as not meaning equal, it means assignment.