YouTube2Text

Lec 1 | MIT 6.01SC Introduction to Electrical Engineering and Computer Science I, Spring 2011 — Transcript

by MIT OpenCourseWare · 10,060 words · 1,367 segments · language en · Watch on YouTube

Full transcript

  1. 0:00The following content is provided under a Creative
  2. 0:02Commons license.
  3. 0:04Your support will help MIT OpenCourseWare continue to
  4. 0:07offer high quality educational resources for free.
  5. 0:11To make a donation or view additional materials from
  6. 0:13hundreds of MIT courses, visit MIT OpenCourseWare at
  7. 0:17ocw.mit.edu.
  8. 0:28PROFESSOR: Hello, and welcome to 6.01.
  9. 0:31I'm Denny Freeman.
  10. 0:32I'm the lecturer.
  11. 0:33One thing you should know about today is that there's a
  12. 0:35single hand-out.
  13. 0:37You should have picked it up on your way in.
  14. 0:38It's available at either of the two doors.
  15. 0:42What I want to do today in this first lecture is mostly
  16. 0:47focus on content.
  17. 0:50But before I do that, since 6.01 is a little bit of an
  18. 0:54unusual course, I want to give you a little bit of an
  19. 0:56overview and tell you a little bit about the administration
  20. 0:59of the course.
  21. 1:036.01 is mostly about modes of reasoning.
  22. 1:11What we would like you to get out of this course is ways to
  23. 1:14think about engineering.
  24. 1:17We want to talk about how do you design, how do you build,
  25. 1:21how do you construct, how do you debug complicated systems?
  26. 1:26That's what engineers do, and we're very good at it.
  27. 1:30And we want to make you very good at it.
  28. 1:33We're very good at it.
  29. 1:34And you know that from your common, everyday experience.
  30. 1:37Laptops are incredible.
  31. 1:41As we go through the course, you're going to see that
  32. 1:43laptops incorporate things from the tiniest, tiniest
  33. 1:47level, things so small that you can't see them.
  34. 1:51They're microscopic.
  35. 1:52The individual transistors are not things that you can see.
  36. 1:55We develop special tools for you even to be able to
  37. 1:58visualize them.
  38. 2:00And yet, we conglomerate billions of them into a system
  39. 2:04that works relatively reliably.
  40. 2:06Now, I realize I'm going out on a limb because when you say
  41. 2:09things like that, then things always fail.
  42. 2:11But I'll go out on a limb and say, for the most part, the
  43. 2:15systems we construct are very reliable.
  44. 2:19We'd like you to know how you think about making such a
  45. 2:22complicated system and making it reliable.
  46. 2:26We want to tell you about how you would model things.
  47. 2:29How do you gain insight?
  48. 2:30How do you get predictability?
  49. 2:32How do you figure out how something will work before
  50. 2:35you've built it?
  51. 2:38If you're limited to trying out how things work by
  52. 2:42actually constructing it, you spend a lot of time
  53. 2:45constructing things that never make it.
  54. 2:48We want to avoid that by -- where we can --
  55. 2:52making a model, analyzing the model, making a prediction
  56. 2:55from the model, and using that prediction to build a better
  57. 2:59system on the first try.
  58. 3:04We want to tell you about how to augment the physical
  59. 3:07behavior of a system by putting computation in it.
  60. 3:11That's a very powerful technique that is increasingly
  61. 3:15common in anything from a microwave to a refrigerator.
  62. 3:20We'd like you to know the principles
  63. 3:21by which to do that.
  64. 3:23And we'd like you to be able to build systems that are
  65. 3:26robust to failure.
  66. 3:29That's a newer idea.
  67. 3:31It's something that people are very good at.
  68. 3:35If we try to do something, and we make a mistake, we
  69. 3:38know how to fix it.
  70. 3:39And often, the fix works.
  71. 3:42We're less good at doing that in constructing artificial
  72. 3:46systems, in engineering systems.
  73. 3:48And we'd like to talk about principles by
  74. 3:49which we can do that.
  75. 3:52So the goal of 6.01 is, then, really to convey a distinct
  76. 3:56perspective about how we engineer systems.
  77. 4:02Now, having said that, this is not a philosophy course.
  78. 4:07We are not going to make lists of things to do if you want it
  79. 4:10to be robust.
  80. 4:13We're going to learn to do things by
  81. 4:15actually making systems.
  82. 4:17This is an introductory engineering course.
  83. 4:19And so you're going to build things.
  84. 4:22The idea is going to be that in constructing those things,
  85. 4:25we've written the exercises so that some of those important
  86. 4:28themes become transparent.
  87. 4:31So the idea is -- this is introductory engineering.
  88. 4:35You'll all make things.
  89. 4:36You'll all get things to work, and in the process of doing
  90. 4:40that, learn something about the bigger view of how quality
  91. 4:44engineering happens.
  92. 4:48So despite the fact that we're really about modes of
  93. 4:51reasoning, that will be grounded in content.
  94. 4:55We selected the content very broadly from across EECS.
  95. 5:00EECS is an enormous endeavor.
  96. 5:04We can't possibly introduce everything
  97. 5:06about EECS in one subject.
  98. 5:08That's ridiculous.
  99. 5:11However, we wanted to give you a variety.
  100. 5:13We wanted to give you a sense of the variety of tasks that
  101. 5:16you can use, that you can apply the same techniques to.
  102. 5:21So we want to introduce modes of reasoning, and then show
  103. 5:23you explicitly how you can use those modes of reasoning in a
  104. 5:27variety of contexts.
  105. 5:29So we've chosen four, and we've organized the course
  106. 5:31around four modules.
  107. 5:33First module is software engineering, then signals and
  108. 5:35systems, then circuits, then probability and planning.
  109. 5:39Even so, even having chosen just four out of the vast
  110. 5:43number of things we could have chosen, there's no way we can
  111. 5:46tell you adequately--
  112. 5:48we can't give you an adequate introduction to any of those
  113. 5:50things either.
  114. 5:52What we've chosen to do instead is focus on key
  115. 5:57concepts represented by the asterisks.
  116. 6:01The idea is going to be we choose one or two things and
  117. 6:06really focus on those deeply so you get a thorough
  118. 6:09understanding not only of how that fits within, for example,
  119. 6:14the context of software engineering, but also how that
  120. 6:17concept ramifies into other areas.
  121. 6:20Notice that I tried to choose the stars so they
  122. 6:23hit multiple circles.
  123. 6:25That's what we're trying to do.
  124. 6:27We're trying to not only introduce an idea to you, but
  125. 6:30also show you how it connects to other ideas.
  126. 6:36So the idea, then, is to focus on a few, we hope, very
  127. 6:40well-chosen applications that will demonstrate a variety of
  128. 6:45powerful techniques.
  129. 6:48Our mantra, the way we intend to go about teaching this
  130. 6:52stuff, is practice, theory, practice.
  131. 6:58There's an enormous educational
  132. 7:00literature that says--
  133. 7:02whether you like it or not--
  134. 7:04people learn better when they're doing things.
  135. 7:08You have a lot of experience with that.
  136. 7:10You have a lot of experience on the other side, too.
  137. 7:13I'll try to forget the other side, or at least try to wipe
  138. 7:15it from your brain momentarily to focus on your more
  139. 7:20fundamental modes of learning.
  140. 7:23When you were a kid and you were learning your first
  141. 7:25language, you didn't learn all the rules of grammar first.
  142. 7:30You didn't learn all the letters of the alphabet first.
  143. 7:32You didn't learn about conjugating verbs first.
  144. 7:37You learned a little bit about language.
  145. 7:38You started to use it.
  146. 7:39You ran into problems.
  147. 7:40You learned a little more about language.
  148. 7:43You learned to go from words like "feed me" to higher level
  149. 7:50concepts, like "Hey, what's for dinner?"
  150. 7:54So the idea is that you learned it in an iterative
  151. 7:58process where you learned some stuff, tried it out, learned
  152. 8:02some more stuff, tried it out.
  153. 8:03And it built up.
  154. 8:05There's an enormous literature in education that says that's
  155. 8:08exactly how we always learn everything.
  156. 8:11And so that's the way this course is focused.
  157. 8:13What we will do is, for example, for today, we'll
  158. 8:16learn a little bit about software engineering.
  159. 8:19Then, we'll do two lab sessions where you actually
  160. 8:21try to use the things we talk about.
  161. 8:25Then, we'll come back to lecture and we'll have some
  162. 8:26more theory about how you would do programming.
  163. 8:30And then, you go back to the lab and do some more stuff.
  164. 8:33And the hope is that by this tangible context, you'll have
  165. 8:39a deeper appreciation of the ideas that
  166. 8:40we're trying to convey.
  167. 8:45So let me tell you a little bit about the four modules
  168. 8:48that we've chosen.
  169. 8:49The course is going to be organized on four modules.
  170. 8:53Each module will take about one fourth of the course.
  171. 8:56First thing we'll look at is software engineering.
  172. 8:59As I said, we don't have time to focus on, or even survey,
  173. 9:04all of the big ideas in software engineering.
  174. 9:06It's far too big.
  175. 9:07So we're going to focus narrowly on one or two things.
  176. 9:12We'd like you to know about abstraction and modularity
  177. 9:16because that's such an important idea in the
  178. 9:20construction of big systems.
  179. 9:23So that's going to be our focus.
  180. 9:26In today's lecture, we'll begin talking about modularity
  181. 9:31and abstraction at the small scale.
  182. 9:35How does it affect the things you type as
  183. 9:38instructions to a computer?
  184. 9:40But by next week, we're going to be talking about a whole
  185. 9:43bigger scale.
  186. 9:45By next week, we're going to talk about constructing
  187. 9:48software modules at a much higher level.
  188. 9:52In particular, we'll talk about something that we'll
  189. 9:54call a state machine.
  190. 9:56A state machine is a thing that works in steps.
  191. 10:02On every step, the state machine gets a new input.
  192. 10:08Then, based on that input and its memory of what's come
  193. 10:11before, the state machine decides to do something.
  194. 10:14It generates an output.
  195. 10:16And then, the process repeats.
  196. 10:20We will see that that kind of an abstraction --
  197. 10:23state machines --
  198. 10:25there's a way to think about state machines that is
  199. 10:27compositional that you can think of as a hierarchy, just
  200. 10:33as you can think of low-level hierarchies within a language.
  201. 10:36I'll say a lot more about that today.
  202. 10:39So the idea will be that once you've composed a state
  203. 10:42machine, you'll be able to join two state machines and
  204. 10:46have its behavior look just like one state machine.
  205. 10:49That's a way to get a more complicated behavior by
  206. 10:52constructing two simpler behaviors.
  207. 10:54That's what we want.
  208. 10:55We want to learn tools that let us compose complex
  209. 10:58behaviors out of simple behaviors.
  210. 11:01And the tangible model of that will be the robot.
  211. 11:05We will see how to write a program that controls a robot
  212. 11:11as a state machine.
  213. 11:14That's certainly not the only way you could control a robot.
  214. 11:17And it's probably not the way you would first think of it if
  215. 11:20you took one course in programming and somebody said
  216. 11:23to you, go program the robot to do something.
  217. 11:27What we will see is that it's a very powerful way to think
  218. 11:30about it for exactly this reason of modularity.
  219. 11:35The bigger point that we will make in thinking about this
  220. 11:37first module is the idea of, how do you
  221. 11:41make systems modular?
  222. 11:43How do you use abstraction to simplify the design task?
  223. 11:46And in particular, we will focus on something
  224. 11:48that we'll call PCAP.
  225. 11:52When you think about a system, we will always think about it
  226. 11:54in terms of, what are the primitives?
  227. 11:57How do you combine them?
  228. 12:00How do you abstract a bigger behavior from
  229. 12:05those smaller behaviors?
  230. 12:07And what are the patterns that are important to capture?
  231. 12:11So the bigger point is this idea of PCAP, which we will
  232. 12:15then revisit in every subsequent module.
  233. 12:20OK, second module is on signals and systems.
  234. 12:24That's also an enormous area.
  235. 12:27So we only have time to do one thing.
  236. 12:29The thing that we will do is we will think about discrete
  237. 12:33time feedback.
  238. 12:36How do you make a system that's cognizant of what it's
  239. 12:40done so that it, in the future, can do things with
  240. 12:44awareness of how it got there?
  241. 12:48A good example is robotic steering.
  242. 12:51So the idea is going to be, OK, think about what you do
  243. 12:55when you're driving a car.
  244. 12:58And think about how you would tell a robot to
  245. 13:00do that same thing.
  246. 13:03Here's a naive driving algorithm.
  247. 13:06I don't recommend it, but it's widely used in Boston,
  248. 13:08apparently.
  249. 13:09[LAUGHTER]
  250. 13:11I find myself to the right of where I would like to be.
  251. 13:14So what should I do?
  252. 13:16Turn left.
  253. 13:18I'm still to the right of where I'd like to be.
  254. 13:20What should I do?
  255. 13:22Turn left.
  256. 13:24Oh!
  257. 13:25I'm exactly where I should be.
  258. 13:27What should I do?
  259. 13:30Go straight ahead.
  260. 13:32Oh, that's a bad idea.
  261. 13:34And what we'll see is that perfectly innocent looking
  262. 13:38algorithms can have horrendous performance.
  263. 13:42What we'll do is try to make an abstraction of that.
  264. 13:46We'll try to make a model.
  265. 13:48We'll try to capture that in math so that we don't need to
  266. 13:51build it to see the bad behavior.
  267. 13:55We'll make a model.
  268. 13:57We'll use the model to predict that that algorithm stinks.
  269. 14:01But more importantly, we'll use the model to figure out an
  270. 14:05algorithm that'll work better.
  271. 14:07In fact, we'll even be able to come up with bounds on how
  272. 14:11well such a controller could possibly work.
  273. 14:15So the focus in this module is going to be, how do you make a
  274. 14:19model to predict behavior?
  275. 14:20How do you analyze the model so that you can design a
  276. 14:24better system?
  277. 14:25And then, how do you use the model and the analysis to make
  278. 14:29a well-behaved system?
  279. 14:34The third module is on circuits.
  280. 14:37Again, circuits is huge.
  281. 14:39We don't have time to talk about all of circuits.
  282. 14:41We'll do very simple things.
  283. 14:43We'll focus our attention on how you would add a sensory
  284. 14:47capability to an already complicated system.
  285. 14:52The idea is going to be to start with a robot--
  286. 14:55I guess this is brighter--
  287. 14:56start with our robots and design a head for the robot.
  288. 15:03The robot comes from the factory with sonar sensors.
  289. 15:07The sonar sensors are these things.
  290. 15:09There's eight of them.
  291. 15:10They tell you how far away something that reflects the
  292. 15:13ultrasonic wave is.
  293. 15:17As they come from the factory, the robots can't sense light.
  294. 15:21What you'll do is add light sensors.
  295. 15:24The goal is to make a system to modify the robot so that
  296. 15:29the robot tracks light.
  297. 15:31That's a very simple goal.
  298. 15:33And the way we'll that is to augment the robot with a
  299. 15:37simple sensor here, showed a little more magnified here.
  300. 15:42The idea is that this is a LEGO motor.
  301. 15:46The LEGO motor will turn this relative to the attachment.
  302. 15:51That's the robot head's neck.
  303. 15:55So the robot will be able to do this.
  304. 16:00And the robot will have eyes.
  305. 16:02These are photosensors, photoresistors, actually.
  306. 16:06So the idea is going to be that there's information
  307. 16:08available in those sensors for figuring out where light is so
  308. 16:12that you can track it.
  309. 16:15Your job will be to build a circuit--
  310. 16:17that that's this thing--
  311. 16:20that connects via cables--
  312. 16:21these red cables and yellow cables--
  313. 16:23connects via cables over to this head.
  314. 16:25We'll give you the head.
  315. 16:28Your job will be to make the circuit that converts the
  316. 16:33signal from the photoresistor--
  317. 16:35which is in proportion to light--
  318. 16:38and figures out how to turn the motor to get the head to
  319. 16:43face the light and then ship that information down to the
  320. 16:47robot to let the robot turn its wheels to get the body.
  321. 16:51So it's kind of like the light comes on bright over here.
  322. 16:54The robot looks at it and says, oh, yeah, that's where I
  323. 16:55want to be.
  324. 16:57So that's the idea in the third module is to incorporate
  325. 17:00new sensing capabilities into the robot.
  326. 17:04The final module is on probability and planning.
  327. 17:08And the idea there is to learn about how you make systems
  328. 17:14that are robust to uncertainty and that can implement
  329. 17:22complicated plans, that they, too, are robust to
  330. 17:26uncertainty.
  331. 17:28So there's a number of things that we will do, including
  332. 17:31creating maps of spaces that the robot doesn't understand,
  333. 17:36telling the robot how to localize itself, how if it
  334. 17:41woke up suddenly in an environment, it could figure
  335. 17:43out where it is, how to make a plan.
  336. 17:47And as an example, I'll show you the kind of system that we
  337. 17:51will construct.
  338. 17:53Here, the idea is that we have a robot.
  339. 17:55The robot knows where it is.
  340. 17:58Imagine there's a GPS in it.
  341. 18:00There isn't, but imagine there is.
  342. 18:01So the robot knows where it is, and it knows where it
  343. 18:03wants to go.
  344. 18:04That's the star.
  345. 18:06But it has no idea what kind of obstacles are in the way.
  346. 18:10So if you were a robotic driver in Boston, you know
  347. 18:15that you started out at home and you want to end up in MIT.
  348. 18:18But there's these annoying obstacles, they're called
  349. 18:20people, that you should, in principle at least, miss.
  350. 18:26So that's kind of the idea.
  351. 18:30So I know where I am.
  352. 18:32I'm the robot.
  353. 18:32I know where I am.
  354. 18:33I know where I want to be.
  355. 18:34And I'm going to summarize that information here.
  356. 18:37Where I am is purple.
  357. 18:38Where I want to be is gold.
  358. 18:40And I have a plan.
  359. 18:43That's blue.
  360. 18:45My plan's very simple.
  361. 18:46I don't know anything about anything other than I'm in
  362. 18:49Waltham and I want to go to Cambridge.
  363. 18:51So blast east.
  364. 18:57So I imagine that the best way to do there
  365. 18:59is a straight line.
  366. 19:02OK, so now what I'm going to do is turn on the robot.
  367. 19:07The robot has now made one step.
  368. 19:09And I told you before about these sonar sensors.
  369. 19:13From the sonar sensors, the robot has learned now that
  370. 19:16there seems to be something reflecting at each of these
  371. 19:19black dots.
  372. 19:22It got a reflection from the black dots,
  373. 19:24from the sonar sensors.
  374. 19:26That means there's probably a wall there, or a person, or
  375. 19:28something that, in principle, I should avoid.
  376. 19:32And the red dots represent, OK, the obstacle is so close I
  377. 19:37really can't get there.
  378. 19:39So I'm excluded from the red spots because I'm too big.
  379. 19:43The black spots seem to be an obstacle.
  380. 19:47The red spots seem to be where I can't fit.
  381. 19:51I still want to go from the where I am, purple, to where I
  382. 19:54want to be, gold.
  383. 19:56So what I do is I compute the new plan.
  384. 20:00OK then, I start to take a step along that plan.
  385. 20:03And as I'm stepping along, OK, so now, I think that I can't
  386. 20:07go from where I started over to here.
  387. 20:09I have to go around this wall that I
  388. 20:11didn't know about initially.
  389. 20:14So now I just start driving.
  390. 20:15And it looks fine, right?
  391. 20:17I'm getting there, right?
  392. 20:20Now, I know I can go straight down here.
  393. 20:21Oh, wait a minute.
  394. 20:22There's another wall.
  395. 20:23OK, what do I do now?
  396. 20:26So as the robot goes along, it didn't know when it started
  397. 20:31what kinds of obstacles it would encounter.
  398. 20:33But as it's driving, it learned.
  399. 20:34Oh, that didn't work.
  400. 20:36Start over!
  401. 20:39So the idea is that this robot is executing a very
  402. 20:46complicated plan.
  403. 20:49The plan has, in fact, many sub-plans.
  404. 20:53And the sub-plans all involve uncertainty.
  405. 20:56It didn't know where the walls were when it started.
  406. 21:00And when it's all done, it's going to have figured out
  407. 21:03where the walls were and--
  408. 21:05provided there's a way--
  409. 21:06presumably find the way to negotiate the maze and get to
  410. 21:11the destination.
  411. 21:13So the idea, then, is that if you were asked to write a
  412. 21:19conventional kind of program for solving that, it might be
  413. 21:23kind of hard because of the number of
  414. 21:25contingencies involved.
  415. 21:27What we will do is break down the problem and figure out
  416. 21:31simple and elegant ways to deal not only with
  417. 21:34uncertainty, but how do you make complex plans.
  418. 21:41So as I said, our primary pedagogy is going to be
  419. 21:45practice, theory, practice.
  420. 21:49And so that ramifies in how the course is organized.
  421. 21:54So this is a quick map of some of the aspects of the course.
  422. 21:59So we'll have weekly lectures.
  423. 22:01It's lecture unintensive.
  424. 22:04In total, there's only 13 lectures.
  425. 22:06We'll meet once a week here for lecture.
  426. 22:10There's readings.
  427. 22:11There's voluminous readings.
  428. 22:13There's readings about every topic that we will talk about.
  429. 22:16And the readings were specifically
  430. 22:18designed for this course.
  431. 22:19I highly recommend that you become
  432. 22:21familiar with the readings.
  433. 22:23If you have a question after lecture, it's probably there.
  434. 22:26It's probably explained.
  435. 22:30We will do online tutor problems.
  436. 22:33We sent you an email if you pre-registered for the course.
  437. 22:36So you may already know about this.
  438. 22:37The idea is going to be that there's ways that you can
  439. 22:41prepare for the course by doing computer exercises.
  440. 22:45And we will also use those same kinds of exercises in all
  441. 22:48of the class sessions.
  442. 22:51We will have two kinds of lab experiences.
  443. 22:53Besides lecture, the other two events that you have to attend
  444. 22:58are a software lab and a design lab.
  445. 23:01That's the practice part.
  446. 23:04So after you learned a little bit about the theory by going
  447. 23:06to lecture, by doing the reading, then you go to the
  448. 23:09lab and try some things out.
  449. 23:12We call the first lab a software lab.
  450. 23:15It's a short lab.
  451. 23:16It's an hour and a half.
  452. 23:17You work individually.
  453. 23:18You try things out.
  454. 23:19You write little programs.
  455. 23:20The courseware can check the program to see if it's OK.
  456. 23:27And primarily, the exercises in the software lab are due
  457. 23:32during the software lab.
  458. 23:34But on occasion, there will be extra things due
  459. 23:36a day or two later.
  460. 23:38The due dates are very clearly written
  461. 23:40in the tutor exercises.
  462. 23:42Once a week, there's a design lab.
  463. 23:44That's a three hour session in which you work with a partner.
  464. 23:48The reason for the partner is that the intent--
  465. 23:51the difference between the design labs and the software
  466. 23:54labs is that the design labs ask you to solve slightly more
  467. 23:58open-ended questions, the kind of question that you might
  468. 24:04have no clue what we're asking.
  469. 24:06Open-ended, the kind of thing that you will be asked to do
  470. 24:09after you graduate.
  471. 24:10Design the system.
  472. 24:11What do you mean, design the system?
  473. 24:15So the idea is that working with a partner will give you a
  474. 24:20second, immediate source of help and a little more
  475. 24:24confidence if neither of you knows the solution so that you
  476. 24:26raise your hand and say, I don't have a clue
  477. 24:28what's going on here.
  478. 24:29So the idea is that once a week we do a software lab
  479. 24:32individually.
  480. 24:33Once a week, we do a design lab, a little more open-ended
  481. 24:36with partners.
  482. 24:38There's a little bit of written homework, four total.
  483. 24:40It's not much compared to other subjects.
  484. 24:43It's mostly practice.
  485. 24:46There's a nano-quiz, just to help you keep pace to make
  486. 24:50sure that you don't get too far behind.
  487. 24:53The first 15 minutes of every design lab
  488. 24:56starts with a nano-quiz.
  489. 24:57The nano-quizzes are intended to be simple if you've caught
  490. 25:02up, if you're up to date.
  491. 25:04So the idea is that you go to design lab, the first thing
  492. 25:08you do is a little, 15 minute nano-quiz.
  493. 25:11The nano-quiz uses a tutor much like the homework tutor,
  494. 25:14much like the Python tutor.
  495. 25:17And it's intended to be simple.
  496. 25:20But it does mean please get to the design lab on time.
  497. 25:25The nano-quizzes are administered by the software.
  498. 25:29It starts the hour when the design lab starts.
  499. 25:32It times out 15 minutes later.
  500. 25:35So if you come 10 minutes late, you will have 5 minutes
  501. 25:38to do something that we planned to give
  502. 25:39you 15 minutes for.
  503. 25:44We will also have exams and interviews.
  504. 25:46The interviews are intended to be a one-on-one conversation
  505. 25:51about how the labs went.
  506. 25:55And we will have two mid-terms and a final.
  507. 25:59So that's kind of the logistics.
  508. 26:01The idea behind the logistics is practice, theory, practice.
  509. 26:05Come to the labs.
  510. 26:06Try things out.
  511. 26:07Make sure you understand.
  512. 26:09Develop a little code.
  513. 26:10Type it in.
  514. 26:10See if it works.
  515. 26:12If it works, you're on top of things.
  516. 26:14You're ready to get the next batch of information from the
  517. 26:18lecture and readings.
  518. 26:21OK, let's go on, and let's talk about the technical
  519. 26:23material in the first module of the course, in
  520. 26:25the software module.
  521. 26:27We kick the course off talking about software engineering for
  522. 26:31two reasons.
  523. 26:32We'd like you to know about software engineering.
  524. 26:34It's an incredibly important part of our department.
  525. 26:39It's an incredibly important part of the engineering of
  526. 26:41absolutely any system, any modern system.
  527. 26:46But we'd also like you to know about it because it provides a
  528. 26:49very convenient way to think about-- it's a convenient
  529. 26:52language to think about the design issues, the engineering
  530. 26:59issues in all the other parts of the class.
  531. 27:01So it's a very good place to start.
  532. 27:06So what I will do today is talk about some of the very
  533. 27:10simplest ideas about abstraction and modularity in
  534. 27:15what I think of as the lowest level of granularity.
  535. 27:19How do you think about abstraction and modularity at
  536. 27:22the micro scale, at the individual
  537. 27:25lines of code scale?
  538. 27:28As I said earlier, we will, as we progress, look at
  539. 27:33modularity and abstraction at the higher scale.
  540. 27:37But we have to start somewhere.
  541. 27:38And we're going to start by thinking about, how do you
  542. 27:41think about abstraction and modularity at the micro scale?
  543. 27:46Special note about programming.
  544. 27:49So what we are trying to do is, in the first two weeks,
  545. 27:54ramp everybody up to some level of software security,
  546. 28:00where you feel comfortable.
  547. 28:03So the first two weeks of this course is intended to make you
  548. 28:07comfortable with programming.
  549. 28:09We don't assume you've done extensive programming before.
  550. 28:14We want you to become comfortable
  551. 28:16that you're not behind.
  552. 28:19And that's the focus of the first two weeks' exercises.
  553. 28:24If you have little or no previous background, if you
  554. 28:27are uncomfortable, please do the Python tutor exercises.
  555. 28:32If you have not --
  556. 28:34if you do not have a lot of experience programming, if
  557. 28:38you're uncomfortable with the expectation that you can do
  558. 28:41programming, do that first.
  559. 28:46That takes priority over all the other assignments during
  560. 28:49the first two weeks.
  561. 28:52In particular, if you're uncomfortable, we will run a
  562. 28:56special Python help session on Sunday.
  563. 29:00And if you attend that, you can get a free extension.
  564. 29:08The idea is completing the tutor exercises is intended to
  565. 29:12make you feel comfortable that you have the software
  566. 29:14background to finish the rest of the course.
  567. 29:18Do that first.
  568. 29:20We will forgive falling behind in other things so that you
  569. 29:26feel comfortable with programming.
  570. 29:29If, at the end of two weeks, you still feel uncomfortable,
  571. 29:34we have a deal with 6.00, the Python programming class, that
  572. 29:40they will allow you to switch your registration
  573. 29:43from 6.01 to 6.00.
  574. 29:46But that expires Valentine's Day.
  575. 29:49[LAUGHTER]
  576. 29:51So you have to make up your mind before Valentine's Day if
  577. 29:54you'd like to use that option.
  578. 29:57So the idea is we'd like you to be comfortable with
  579. 30:00programming.
  580. 30:01If you haven't programmed before, do the
  581. 30:03Python tutor exercises.
  582. 30:05Go to software lab.
  583. 30:06Go to design lab, but work on the tutor exercises.
  584. 30:10The staff will help you with them.
  585. 30:13You can go to office hours.
  586. 30:14There's office hours listed on the home page.
  587. 30:18You should try to become comfortable, and you should
  588. 30:20try to set as your goal --
  589. 30:22I'm going to be comfortable before Valentine's Day.
  590. 30:25And if you're not, talk to a staff member about that.
  591. 30:32OK, so what do I want you to know about programming?
  592. 30:36Well, we're going to use Python.
  593. 30:39We selected Python because it's very simple and because
  594. 30:46it lets us illustrate some very important ideas in
  595. 30:51software engineering in a very simple context.
  596. 30:55That's the reason.
  597. 30:57One of the reasons that it's simple is that it's an
  598. 31:01interpreter.
  599. 31:04After some initialization, the behavior of Python is to fall
  600. 31:10into an interpreter loop.
  601. 31:11The interpreter loop is, ask the user what he would like me
  602. 31:16to do, read what the user types, figure out what they're
  603. 31:24talking about, and print the result, repeat--
  604. 31:28very simple.
  605. 31:29What that means is that you can learn by doing.
  606. 31:35That's one of the points of today's software lab.
  607. 31:38You can simply walk up to a computer,
  608. 31:40type the word python--
  609. 31:43what you type is in red.
  610. 31:46Type the word "python." It will prompt you, so this
  611. 31:51chevron, that says, I'd like you to tell me
  612. 31:54something to do.
  613. 31:55I have nothing to do.
  614. 31:57If you type "2," Python tries to interpret that.
  615. 32:01In this particular case, Python says, oh, I see.
  616. 32:05That's a primitive data item.
  617. 32:06That's an integer.
  618. 32:08This person wants me to understand an integer.
  619. 32:10And so it will echo 2, indicating that it thinks you
  620. 32:17want it to understand a simple integer.
  621. 32:21Similarly, if you type 5.7, it says, oh, I got that.
  622. 32:25That's a float.
  623. 32:27The person wants me to remember a
  624. 32:28floating point number.
  625. 32:31And it will similarly echo the float.
  626. 32:35Now, of course, there's no exact representation for
  627. 32:40floats, right?
  628. 32:41There's too many of them, right?
  629. 32:43There's a lot of them.
  630. 32:44There's even more floats than there are ints, right?
  631. 32:48So it has an approximation.
  632. 32:50So it will print its approximation to the float
  633. 32:54that it thinks you are interested in.
  634. 32:59If you type a string, "Hello," it'll say, oh, primitive data
  635. 33:04structure, string.
  636. 33:07And it'll print out that string.
  637. 33:09So the idea is one of the features of Python that makes
  638. 33:12it easy to learn is the fact that it's interpreter based.
  639. 33:16You can play around.
  640. 33:17You can learn by doing.
  641. 33:19Now, of course, if the only thing it did was simple data
  642. 33:23structures, it would not be very useful.
  643. 33:27So the next more complex thing that it can do is think about
  644. 33:32combinations.
  645. 33:34If you type "2 + 3," it says, oh, I got it.
  646. 33:38This person's interested in a combination.
  647. 33:40I should combine by the plus operator two ints, 2 and 3.
  648. 33:46Oh, and if I do that, if I combine by the plus operator
  649. 33:48two and three, I'll get 5.
  650. 33:50So it prints 5.
  651. 33:52So that's a way you know that it interprets "2 + 3" as 5.
  652. 33:59Similarly here, except I've mixed types.
  653. 34:02"5.7 + 3," it says, oh, this user wants me to apply the
  654. 34:08plus operator on a float and an int.
  655. 34:12OK, well I'll upgrade the int to a float.
  656. 34:15I'll do the float version, and I'll get this, which is its
  657. 34:20representation of 8.7.
  658. 34:26So the idea is that it will first try to interpret what
  659. 34:29you're saying as a simple data type.
  660. 34:31If that works, it prints the result to tell you what it
  661. 34:34thinks is going on.
  662. 34:36It then will try to interpret it as an expression.
  663. 34:39And sometimes, the expressions won't makes sense.
  664. 34:43In particular, if you try to add an int to a string, it's
  665. 34:46going to say, huh?
  666. 34:48And over the course of the first two weeks, we hope that
  667. 34:50you get familiar with interpreting
  668. 34:52this kind of mess.
  669. 34:55That's Python's attempt to tell you what it was trying to
  670. 34:59do on your behalf and can't figure out what
  671. 35:03you're talking about.
  672. 35:07OK, so that was simple.
  673. 35:09But it already illustrates something that's very
  674. 35:11important, and that's the idea of a composition.
  675. 35:16So the way Python works, the fact that when you added 3 to
  676. 35:222 it came out 5, what we were doing was composing
  677. 35:27complicated--
  678. 35:29well, potentially complicated (that was pretty simple) --
  679. 35:32potentially complicated expressions and reducing them
  680. 35:36to a single data structure.
  681. 35:41And so that means that, in some sense, this operation, 3
  682. 35:46times 8, can be thought of as exactly the same as if the
  683. 35:53user had typed in 24.
  684. 35:56Whenever you can substitute for a complex expression a
  685. 36:02simpler thing, we say that the system is compositional.
  686. 36:07That's a very powerful idea.
  687. 36:10Even though it's simple, it's a very powerful idea.
  688. 36:12And it's an idea that you all know.
  689. 36:16You've seen it before in algebra, in arithmetic.
  690. 36:20So in arithmetic expressions, you can think about how the
  691. 36:24sum of two integers is an int.
  692. 36:29That's a closure.
  693. 36:30That's a kind of a combination that makes the system
  694. 36:33compositional and that provides a layer of
  695. 36:38hierarchical thinking so that, in your head, even though it
  696. 36:41says 3 times 8, you don't need to remember that anymore.
  697. 36:44You can say, oh, for any purposes that follow, I might
  698. 36:47just as well think of 3 times 8 as being a
  699. 36:49single integer, 24.
  700. 36:53It's part of many other kinds of systems, for example,
  701. 36:58natural language.
  702. 37:00The simplest example in natural language is that you
  703. 37:03can think about "Apples are good as snacks".
  704. 37:06"Apples" is a noun.
  705. 37:08It's a plural noun.
  706. 37:11Or you could substitute "Apples and oranges", and it
  707. 37:15makes complete sense within that same structure.
  708. 37:18So "Apples and oranges are good as snacks".
  709. 37:22The combination of "apples" and "oranges" works in every
  710. 37:27way from the point of view of the grammar in the same way
  711. 37:30that a simple noun, "apples," worked.
  712. 37:36What we would like to do is use that idea as the starting
  713. 37:40point for a more general compositional system.
  714. 37:46And a good way to think about that is by way of names.
  715. 37:52What if we had some sequence of operations that we think is
  716. 37:55particularly important so that we would like to somehow
  717. 38:00canonize that so that, subsequently, we can use that
  718. 38:04sequence of operations easily?
  719. 38:07Python provides a very simple way to do it.
  720. 38:10Every programming language does.
  721. 38:11It's not unique to Python.
  722. 38:14But the idea is --
  723. 38:16so here's an example.
  724. 38:17"2 times 2" --
  725. 38:19I'm squaring 2 and get 4. "3 times 3" --
  726. 38:22I'm squaring 3, and I'm getting 9.
  727. 38:24"8 plus 4 times 8 plus 4", I'm squaring "8 plus 4".
  728. 38:29"8 plus 4", well, I can think of that as 12.
  729. 38:31I'm squaring 12, I'm getting 144.
  730. 38:34The thing I'm trying to illustrate there is the notion
  731. 38:37of squaring.
  732. 38:38Squaring is a sequence of operations that I would like
  733. 38:43to be able to canonize as a single entity so that, in
  734. 38:49subsequent programs, I can think of the squaring
  735. 38:51operation as a single operation just
  736. 38:54like I think of times.
  737. 38:56The way we say that in Python is "define square of x to be
  738. 39:01return x squared".
  739. 39:07Then, having made that definition, I can say "square
  740. 39:10of 6", and the answer is 36.
  741. 39:14OK, this is a very small step.
  742. 39:15But it illustrates a very important point, the idea
  743. 39:18being that Python provides a compositional facility.
  744. 39:27And it's hierarchical.
  745. 39:31Having defined square, I can use square just as though it
  746. 39:35were a primitive operator.
  747. 39:38And I can use square to define higher level operations.
  748. 39:44So for example, what if I were interested in doing lots of
  749. 39:47sums of squares?
  750. 39:49Say I'm Pythagoreas, right?
  751. 39:51So I might want to add the square of 2 and the square of
  752. 39:554 to get 20, or the square of 3 with the
  753. 39:58square of 4 to get 25.
  754. 40:01Using that simple idea of composition, we can write a
  755. 40:06new program, sumOfSquares.
  756. 40:10sumOfSquares takes two arguments, x and y.
  757. 40:13And it returns the square of x and the square of y.
  758. 40:16SumOfSquares doesn't care about how
  759. 40:19you compute the square.
  760. 40:22It trusts that square knows how to do that.
  761. 40:25So the work is smaller.
  762. 40:28The idea is that square takes care of
  763. 40:32squaring single numbers.
  764. 40:35sumOfSquares doesn't have to know how to square numbers.
  765. 40:39It just needs to know how to make a sum of squares.
  766. 40:44So what we've done is we've broken a task, which was not
  767. 40:47very complicated, but the whole idea is hierarchical.
  768. 40:52We've taken a problem and broken it into two pieces.
  769. 40:56We factored the problem into how do you do a square, and
  770. 40:59how do you sum squares.
  771. 41:03And the idea, then, is that this hierarchical structure is
  772. 41:07a way of building complex systems out of simpler parts.
  773. 41:17So that's the idea of how you would build programs that are
  774. 41:23compositional.
  775. 41:27Python also provides a utility for making lists, for making
  776. 41:32data structures that are compositional.
  777. 41:39The most primitive is a list.
  778. 41:44So in Python, you can specify a list.
  779. 41:48Here's a list of integers.
  780. 41:51So the list says, beginning list, end of
  781. 41:53list, elements of list.
  782. 41:55So there's five elements in the list, the
  783. 41:57integers 1, 2, 3, 4, 5.
  784. 42:00Python doesn't care what the elements of a list are.
  785. 42:07We'll see in a minute that that's really important.
  786. 42:11But for the time being, the simplest thing that you can
  787. 42:13imagine is a heterogeneous list.
  788. 42:15It's not critical that the list contain just integers.
  789. 42:21Here's a list that contains an int, a string,
  790. 42:23an int, and a string.
  791. 42:25Python doesn't care.
  792. 42:26It's a list that has four elements.
  793. 42:28The first element's an int.
  794. 42:29The second element's a string, et cetera.
  795. 42:34Here's an even more complex example.
  796. 42:36Here's a list of lists.
  797. 42:41How many elements are in that list?
  798. 42:46Three.
  799. 42:49How many elements are in that list?
  800. 42:53So the idea is that you can build more complex data
  801. 42:57structures out of simple ones.
  802. 42:59That's the idea of compositional factoring
  803. 43:02applied to data.
  804. 43:07Just like it was important when we were thinking about
  805. 43:12procedures, to associate names with procedures--
  806. 43:16that's what "def" did--
  807. 43:19we can also think about associating names with data
  808. 43:22structures.
  809. 43:24And that's what we use something that Python calls a
  810. 43:27variable for.
  811. 43:29So I can say "b is 3".
  812. 43:32And that associates the data item, 3, with the label, b.
  813. 43:39I can say, "x is 5 times 2.2".
  814. 43:44Python will figure out what I mean by the
  815. 43:46expression on the right.
  816. 43:49It'll figure out that I'm composing by using the star
  817. 43:52operator, which is multiply, an integer and a float, which
  818. 43:55will give me a float.
  819. 43:57The answer to that's going to be a floating point number.
  820. 44:00And it will assign a label, x, to that floating point number.
  821. 44:06You can have a more complicated list, a data
  822. 44:10structure, and associate the name y with it.
  823. 44:13Then, having associated the name y, you get many of the
  824. 44:15same benefits of associating a name with a data structure
  825. 44:19that we got previously in associating a
  826. 44:22name with an operation.
  827. 44:25So we can say, y(0).
  828. 44:28And what that means is, what's the zero-th elements of the
  829. 44:32data structure, y?
  830. 44:35So the zero-th element of the data structure,
  831. 44:37y, is a list, [1, 2, 3].
  832. 44:42Python has some funky notations.
  833. 44:45The -1 element is the last one.
  834. 44:49So the -1th element of y is [7, 8, 9].
  835. 44:54And it's completely hierarchical.
  836. 44:57If I asked for the -1 element of y, I get [7, 8, 9].
  837. 45:01But then, if I asked for the first element of that
  838. 45:02result, I get 8.
  839. 45:06OK?
  840. 45:08Everything is clear?
  841. 45:12OK, just to make sure everything is clear, I want to
  842. 45:15ask you a question.
  843. 45:17But to kick off the idea of working together, I'd like you
  844. 45:19to think about this question with your neighbor.
  845. 45:21So before thinking about this question, everybody stand up.
  846. 45:27Introduce yourself to your neighbor.
  847. 45:29[AUDIENCE TALKS]
  848. 46:34So now, I'd like you to each discuss with your neighbor the
  849. 46:43list that is best represented by which of the following
  850. 46:46figures, 1, 2, 3, 4, or 5, or none of the above.
  851. 46:49And in 30 seconds, I'm going to ask everybody to raise a
  852. 46:52hand with a number of fingers indicating the right answer.
  853. 46:58You're allowed to talk.
  854. 47:00That's the whole point of having a partner.
  855. 47:02[AUDIENCE TALKS]
  856. 48:37OK.
  857. 48:41I'd like everybody now to raise their hand.
  858. 48:45Put up the number of fingers that show the answer.
  859. 48:47And I want to tally.
  860. 48:52Fantastic!
  861. 48:53Everybody gets it.
  862. 48:54OK, so which one do you like?
  863. 48:57AUDIENCE: 3.
  864. 48:58PROFESSOR: 3 --
  865. 48:59why do you like three.
  866. 49:01Somebody explain this to me?
  867. 49:04It just looks good?
  868. 49:06Its pattern recognition.
  869. 49:09What's good about 3?
  870. 49:12AUDIENCE: It shows the compositional
  871. 49:13element of the list.
  872. 49:16PROFESSOR: Compositional?
  873. 49:16What is the compositional element in the pictures?
  874. 49:22What represents what?
  875. 49:26OK, 'a' represents a.
  876. 49:28That's pretty easy, right?
  877. 49:30So that takes care of the bulk of the figures.
  878. 49:32What's the blue lines represent?
  879. 49:39Someone else?
  880. 49:40I didn't quite understand.
  881. 49:42AUDIENCE: The angles represent like a list.
  882. 49:45PROFESSOR: They represent a list.
  883. 49:47Where is the list on the figures?
  884. 49:51AUDIENCE: The vertex?
  885. 49:52PROFESSOR: The vertex.
  886. 49:53The vertices are lists.
  887. 49:57So in 3 --
  888. 49:59at the highest level, we have a list that's composed of how
  889. 50:03many elements?
  890. 50:052.
  891. 50:06The first element of that list is?
  892. 50:08AUDIENCE: a.
  893. 50:09PROFESSOR: And the second element of that list is?
  894. 50:11AUDIENCE: Another list.
  895. 50:11PROFESSOR: Another list.
  896. 50:12That's the hierarchical part, right?
  897. 50:15That second list has how many elements?
  898. 50:18AUDIENCE: 2.
  899. 50:19PROFESSOR: Fine, good, recurse.
  900. 50:21You got it.
  901. 50:22What is the list represented by number 2?
  902. 50:29A single list with five elements.
  903. 50:32Square bracket, a, comma, b, comma, c, comma, d, comma, e,
  904. 50:36square bracket, right?
  905. 50:38What is the list represented by that one?
  906. 50:42AUDIENCE: Not a list.
  907. 50:45PROFESSOR: Agh!
  908. 50:47It's not a list!
  909. 50:49What is it?
  910. 50:50Who knows?
  911. 50:52AUDIENCE: Looking at the variable
  912. 50:53names, it defines them.
  913. 50:55You have variables.
  914. 50:56You have a variable a, that defines a list that contains
  915. 50:58b, and the variable, c, that defines another list that
  916. 51:00contains d.
  917. 51:01PROFESSOR: So we could make that a variable.
  918. 51:05If we said a is a variable that comprises b and c, then
  919. 51:09we have the problem of how we're going to associate
  920. 51:11variables and elements into this list, right?
  921. 51:17So the weird thing about this one and, let's
  922. 51:19see, that one's weird.
  923. 51:20This one's also kind of weird.
  924. 51:22This one's weird because we're giving names to lists in a
  925. 51:27fashion that's not showed up here, right?
  926. 51:31That's not to say you couldn't invent a meaning.
  927. 51:34It's just that it doesn't map very well to that
  928. 51:36representation.
  929. 51:38Similarly over here, we seem to be giving the name b to the
  930. 51:43element a, and then the name c to the element b.
  931. 51:46What on earth are you talking about?
  932. 51:47It's not clear what we're doing their either.
  933. 51:50So the point is to get you thinking about the abstract
  934. 51:54representation of lists and how that maps into a complex
  935. 51:57data structure.
  936. 51:58That was the whole point.
  937. 52:00OK, so we've talked about, then, four things so far.
  938. 52:05How do you think about operations in a
  939. 52:08hierarchical fashion.
  940. 52:10And the idea was composition.
  941. 52:13We think about composing simple operations to make
  942. 52:18bigger, compound operations.
  943. 52:21That's a way of saying, there's this set of operations
  944. 52:24that I want to call foo.
  945. 52:27So every time I do this complicated thing that has
  946. 52:29three pages of code, that's one foo.
  947. 52:33And that's a way that we can then combined foos in some
  948. 52:37other horribly complicated way to make big foos.
  949. 52:42So the idea is composition.
  950. 52:44That's the first idea.
  951. 52:45The second is associating a name with that composition.
  952. 52:51That's what "def" does-- define name, name of a
  953. 52:54sub-routine.
  954. 52:55So we thought about composing operations,
  955. 52:58associating names with them.
  956. 52:59We composed data in terms of lists, and we associated names
  957. 53:03with those lists in terms of variables.
  958. 53:06The next thing we want to think about is a higher order
  959. 53:09construct where we would like to conglomerate into one data
  960. 53:13structure both data and procedures.
  961. 53:18Python has a concept called a class that lets us do that.
  962. 53:23In Python, you make a new class by saying to the Python
  963. 53:27prompt, I want a new class called Student.
  964. 53:31And then, under Student, there is this thing which we will
  965. 53:35call an attribute.
  966. 53:37An attribute to a class is simply a data item associated
  967. 53:42with the class.
  968. 53:44And a method--
  969. 53:46a method is just a procedure that is
  970. 53:50associated with the class.
  971. 53:52So there's this single item class called Student that has
  972. 53:58one piece of data, its attribute, school, and one
  973. 54:04procedure, which is the method calculateFinalGrade.
  974. 54:10So then, this is the kind of data structure you might
  975. 54:12imagine that a registrar would have.
  976. 54:15It's a way to associate.
  977. 54:18So the idea here is that everybody here is a student.
  978. 54:22They all have a school.
  979. 54:24And they all have a way of calculating their final grade.
  980. 54:28That's a very narrow view that maybe a registrar would have.
  981. 54:32So classes, having defined them, we can then use the
  982. 54:38class to define an instance.
  983. 54:42So an instance is a data structure that inherits all of
  984. 54:50the structure from the class but also provides a mechanism
  985. 54:54for having specific data associated with the instance.
  986. 54:58So in Python, I say Mary is a student.
  987. 55:02By mentioning the name of the class and putting parenthesis
  988. 55:05on it, I say, give me an instance of the student.
  989. 55:10So now, Mary is a name associated with an instance of
  990. 55:15the class, Student.
  991. 55:18John is similarly an instance of the class, Student.
  992. 55:23So both Mary and John have schools.
  993. 55:27In fact, they're both the same.
  994. 55:29The school of Mary and the school of John are both MIT.
  995. 55:34But I can extend the instance of Mary to include a new
  996. 55:42attribute, the section number, so that Mary's section number
  997. 55:48is 3 and John's section number is 4.
  998. 55:50So this provides a way--
  999. 55:52it's a higher-order concept.
  1000. 55:55We thought of a way to aggregate operations into
  1001. 55:57complicated operation, data into complicated data.
  1002. 56:01Classes aggregate data and operations.
  1003. 56:06Classes allow us to create a structure and
  1004. 56:09then generate instances.
  1005. 56:10And then the instances have access to those features that
  1006. 56:15were defined in the class, but also have the ability to
  1007. 56:18define their own unique attributes and methods.
  1008. 56:23You can also use a class to define a subclass.
  1009. 56:27So here, I'm defining the subclass, Student601.
  1010. 56:31All Student601s are members of the class, Student.
  1011. 56:35The reverse is not true.
  1012. 56:38So all Student601 entities inherit everything that a
  1013. 56:42Student has.
  1014. 56:44But all 601 students share some other things.
  1015. 56:48Besides having a school which all students have, 601
  1016. 56:52students also have a lecture day, a lecture time, and a
  1017. 56:54method for calculating tutor scores.
  1018. 56:59Not all students have a method for calculating tutor scores.
  1019. 57:03But members of the class Student601 do.
  1020. 57:09So this, again, represents a way of organizing data and
  1021. 57:14operations in a way that makes it easier to compose higher,
  1022. 57:20bigger, more complex structures.
  1023. 57:24The final thing that I want to talk about today is the
  1024. 57:27specific, gory details for how Python manages the association
  1025. 57:33between names and entities.
  1026. 57:38We've already seen two of those.
  1027. 57:40Naming operations is via "def." And it gives rise to
  1028. 57:45the name of a procedure.
  1029. 57:48Variables are ways of naming data structures.
  1030. 57:52Now, we've seen a way of naming classes.
  1031. 57:59And in fact, it's helpful if you understand.
  1032. 58:01So Python associates names and entities in a very simple,
  1033. 58:07straightforward fashion.
  1034. 58:08And if you know the ground rules, it makes it very easy
  1035. 58:11to deal with.
  1036. 58:11And if you don't know the ground rules, it makes it very
  1037. 58:13hard to deal with.
  1038. 58:14So what's the ground rules?
  1039. 58:15Here's the gory details.
  1040. 58:17So Python associates names with values in what Python
  1041. 58:22calls a binding environment.
  1042. 58:26An environment is just a list that associates
  1043. 58:30a name and an entity.
  1044. 58:33So if you were to type b equals 3 what Python is
  1045. 58:35actually doing is it's building this environment.
  1046. 58:41When you type b equals 3, it adds to the environment a
  1047. 58:46name, b, and associates that name with the integer, 3.
  1048. 58:54When you type x equals 2.2, it adds a name, x, and associates
  1049. 59:01it with the float, 2.2.
  1050. 59:04When you say foo is minus 506 times 2, it makes the name,
  1051. 59:09foo, and associates it with an int, minus 1012.
  1052. 59:16Then, if you ask Python about b, the rule is look it up in
  1053. 59:22the environment and type the thing that b refers to.
  1054. 59:29So when you type "b," what Python really does is it goes
  1055. 59:31to the environment.
  1056. 59:32It says, do I have some entity called "b?" Well, yes I do.
  1057. 59:36It happens to be an int, 3.
  1058. 59:38So it prints 3.
  1059. 59:43If you ask, what is "a?" Python says, OK, in my
  1060. 59:48environment, do I have some name, "a?" It doesn't find it.
  1061. 59:55So it prints out this cryptic message that basically says,
  1062. 59:59sorry, guys, I can't find something called "a" in the
  1063. 1:00:03current environment.
  1064. 1:00:05That's the key to the way Python does all name bindings.
  1065. 1:00:11So in general, there's a global environment.
  1066. 1:00:16You start typing to Python.
  1067. 1:00:18It just starts adding and modifying the bindings in the
  1068. 1:00:23binding environment.
  1069. 1:00:25So if you type a equals 3 and then type "a," it'll find 3.
  1070. 1:00:29If you then type "b=a+2," it evaluates the right-hand side
  1071. 1:00:36relative to the current environment.
  1072. 1:00:39So it first looks here.
  1073. 1:00:41And it says, do I have something called "a?" Ah, yes.
  1074. 1:00:47It's an integer, 3.
  1075. 1:00:49Substitute that.
  1076. 1:00:51Do I know what 2 is?
  1077. 1:00:52Oh yeah, that's just an int.
  1078. 1:00:53Do I know what plus is?
  1079. 1:00:54Oh yeah, that's the thing that combines two ints.
  1080. 1:00:56So it decides that a plus 2--
  1081. 1:00:59it evaluates a plus 2 in the current environment.
  1082. 1:01:01It gets 5.
  1083. 1:01:03And it says, oh, I'm trying to do a new equals, a new
  1084. 1:01:06association, a new variable.
  1085. 1:01:08Make the name, b, points to this evaluated in the current
  1086. 1:01:15environment.
  1087. 1:01:16So b gets associated with int 5.
  1088. 1:01:20Then, if I do this line, it evaluates b plus 1 in the
  1089. 1:01:25current environment.
  1090. 1:01:29b is 5 in the current environment.
  1091. 1:01:31It adds 1.
  1092. 1:01:32It gets 6.
  1093. 1:01:34And then, it says, associate this thing, 6, with b.
  1094. 1:01:40So it overwrites the b, which had been bound to 5, and b is
  1095. 1:01:45now bound to 6.
  1096. 1:01:48OK?
  1097. 1:01:48So the whole thing, the way it treats variables, the way
  1098. 1:01:54Python associates a name with a value in a variable, is
  1099. 1:01:57evaluate the right-hand side according to the current
  1100. 1:01:59environment.
  1101. 1:02:01Then, change the current environment to
  1102. 1:02:03reflect the new binding.
  1103. 1:02:07What it does in the case of sub-routines is very similar.
  1104. 1:02:13So here's an illustration of the local environment that is
  1105. 1:02:17generated by this piece of code.
  1106. 1:02:19When I say a equals 2, it generates a name in the local
  1107. 1:02:22environment, a.
  1108. 1:02:25It evaluates the right-hand side and finds 2.
  1109. 1:02:28So it makes a binding in the local environment where the
  1110. 1:02:32name, a, is associated with the integer, 2.
  1111. 1:02:36Then, I say define square of x to be return x squared.
  1112. 1:02:42That's more complicated.
  1113. 1:02:45Python says, oh, I'm defining a new operation.
  1114. 1:02:52It's a procedure.
  1115. 1:02:55The procedure has a formal argument, x.
  1116. 1:03:00It has a body, return x times x.
  1117. 1:03:05I'm going to have to remember all of that stuff.
  1118. 1:03:09So I'm trying to define a new procedure called square.
  1119. 1:03:13It's going to make a binding for square.
  1120. 1:03:17So in the future, if somebody says the word square, it'll
  1121. 1:03:20find out, oh, square I remember that one.
  1122. 1:03:23square, it's a procedure.
  1123. 1:03:28Just like the binding for a variable might be an int, the
  1124. 1:03:32binding for a procedure is the name of the procedure.
  1125. 1:03:39Then, in the procedure, which is some other data structure
  1126. 1:03:43outside the environment, it's got to remember the formal
  1127. 1:03:47parameters--
  1128. 1:03:48in this case, x--
  1129. 1:03:50and the body.
  1130. 1:03:54And for the purpose of resolving what do the
  1131. 1:03:57variables mean, it needs to remember what was the binding
  1132. 1:04:02environment in which this sub-routine was defined.
  1133. 1:04:07So that's this arrow.
  1134. 1:04:09So this sequence says, make a new binding square, points to
  1135. 1:04:14a procedure.
  1136. 1:04:14The procedure has the formal argument, x.
  1137. 1:04:18It has the body return x times x.
  1138. 1:04:23And it has the binding.
  1139. 1:04:25It came from the environment, E1, the current environment.
  1140. 1:04:31OK, is everybody clear?
  1141. 1:04:32So the idea is that the environment associates names
  1142. 1:04:36with things.
  1143. 1:04:37The thing could be a data item, or
  1144. 1:04:39it could be a procedure.
  1145. 1:04:43Then, when you call a procedure, it makes a new
  1146. 1:04:48environment.
  1147. 1:04:50So what happens, then, when I try to evaluate a form, square
  1148. 1:04:54of a plus 2?
  1149. 1:04:58What Python does is it says, OK, I need to figure
  1150. 1:05:01out what square is.
  1151. 1:05:04So it looks it up in the environment, and it finds out
  1152. 1:05:06that square is a procedure.
  1153. 1:05:09Fine, I know how to deal with procedures.
  1154. 1:05:12So then, it figures out this procedure has a formal
  1155. 1:05:16argument, x.
  1156. 1:05:17Oh, OK, if I'm going to run this procedure, I'm going to
  1157. 1:05:20have to know what x means.
  1158. 1:05:23So Python makes a new environment--
  1159. 1:05:25here, it's labelled E2, separate from the global
  1160. 1:05:28environment, E1.
  1161. 1:05:30It makes a new environment that will
  1162. 1:05:33associate x with something.
  1163. 1:05:35Doesn't know what it is yet, it just knows that this square
  1164. 1:05:39is a procedure that takes a formal argument, x.
  1165. 1:05:43So Python makes a new environment, E2, with x
  1166. 1:05:46pointing to something.
  1167. 1:05:47Then, Python evaluates the argument a plus 2 in the
  1168. 1:05:52environment E1.
  1169. 1:05:56You called square of a plus 2 in the environment of E1.
  1170. 1:05:59So it figures out what did you mean by a plus 3.
  1171. 1:06:05Well, you were in the environment E1.
  1172. 1:06:08So it means whatever a plus 3 would have meant if he had
  1173. 1:06:12just typed a plus 3 in that environment.
  1174. 1:06:15So you evaluate a plus 3 in the environment
  1175. 1:06:18E1, and you get 5.
  1176. 1:06:22So then, this new environment, E2, that is set up for this
  1177. 1:06:27procedure, square, associates 5 with x.
  1178. 1:06:33Now it's ready to run the body.
  1179. 1:06:36So now, it runs this procedure, return x times x.
  1180. 1:06:41But now, what it's trying to resolve its variables, it
  1181. 1:06:45looks it up in E2.
  1182. 1:06:48So it says, I want to do the procedure, the
  1183. 1:06:52body, x times x.
  1184. 1:06:53I need to know what x is, and I need to know it twice.
  1185. 1:06:56Look up what x means, but I will look it up in my E2
  1186. 1:07:00environment that was built
  1187. 1:07:01specifically for this procedure.
  1188. 1:07:03And fortunately, there's an x there.
  1189. 1:07:06So it finds out that x is 5.
  1190. 1:07:08It multiplies 5 times 5.
  1191. 1:07:10It gets the answer is 25.
  1192. 1:07:11It returns 25.
  1193. 1:07:13And then, it destroys this environment, E2, because it
  1194. 1:07:16was only necessary for the time when it was running the
  1195. 1:07:20procedure body.
  1196. 1:07:21Is that clear?
  1197. 1:07:28OK, so a slightly more difficult example illustrates
  1198. 1:07:33what happens whenever everything is not defined in
  1199. 1:07:37the current local environment.
  1200. 1:07:39What if I type define biz of a?
  1201. 1:07:43Well, I create a new name in the local environment that
  1202. 1:07:50points to a procedure.
  1203. 1:07:51The procedure has a formal parameter, a, and a body that
  1204. 1:07:55returns a plus b.
  1205. 1:07:58The procedure also was defined within the environment E1,
  1206. 1:08:02which I'll keep track of.
  1207. 1:08:07Then, if I say b equals 6, that makes a new binding in
  1208. 1:08:11the global environment, b equals 6.
  1209. 1:08:16Then, if I try to run biz of 2, look up biz.
  1210. 1:08:23Oh, that's a procedure, formal parameter, a.
  1211. 1:08:25Make an environment, has an a in it.
  1212. 1:08:27What should I put in a?
  1213. 1:08:28Evaluate the argument, 2.
  1214. 1:08:30OK, a is 2.
  1215. 1:08:31Put two here.
  1216. 1:08:34Now, I'm ready to run the body.
  1217. 1:08:37Run the body in the environment, E2.
  1218. 1:08:41When I run return a plus b in E2, I have to
  1219. 1:08:46first figure out a.
  1220. 1:08:48Well, that's easy. a is 2.
  1221. 1:08:52Then, I have to figure out b.
  1222. 1:08:53What's b?
  1223. 1:08:58AUDIENCE: 6?
  1224. 1:08:58PROFESSOR: 6.
  1225. 1:08:59So how did you get 6?
  1226. 1:09:00AUDIENCE: [INAUDIBLE].
  1227. 1:09:03PROFESSOR: So this local environment that was created
  1228. 1:09:06for the formal parameter has, as its parent, E1 because
  1229. 1:09:12that's where the procedure was defined.
  1230. 1:09:17So it doesn't find b in this local environment.
  1231. 1:09:22So it goes to the parent.
  1232. 1:09:23Do you have a "b?" And it could, in principal, propagate
  1233. 1:09:28up a chain of environments.
  1234. 1:09:29So you could construct this hierarchically.
  1235. 1:09:32So it will resolve bindings in the most recent environment
  1236. 1:09:37that has that binding.
  1237. 1:09:39So the answer, then, is that when you run biz of 2, this b
  1238. 1:09:45gets associated with that b, OK?
  1239. 1:09:51So that's how the environments work for simple procedures and
  1240. 1:09:55simple data structures.
  1241. 1:09:57It's very similar for the way it works with classes.
  1242. 1:10:01So imagine that I had this data, and I wanted to
  1243. 1:10:05represent that in Python.
  1244. 1:10:07What I might do is look at the common features.
  1245. 1:10:10The courses are all the same.
  1246. 1:10:11The rooms are all the same.
  1247. 1:10:12The buildings are all the same.
  1248. 1:10:13The ages are highly variable.
  1249. 1:10:16So I might want to create a class that
  1250. 1:10:19has the common data.
  1251. 1:10:21So I might do this--
  1252. 1:10:23class Staff601.
  1253. 1:10:26The course is 601.
  1254. 1:10:27The building's 34.
  1255. 1:10:28The room is this.
  1256. 1:10:30The way Python implements a class is as an environment.
  1257. 1:10:38Executing this set of statements builds the class
  1258. 1:10:43environment.
  1259. 1:10:44This is it.
  1260. 1:10:45It's a list of bindings.
  1261. 1:10:48Here, I'm binding the name, course, to the
  1262. 1:10:50string, 601, et cetera.
  1263. 1:10:53If there were a method, I would do the same thing,
  1264. 1:10:55except it would look like a procedure then.
  1265. 1:10:59So this creates the Staff601 environment.
  1266. 1:11:06Staff601, because I executed this class statement, that
  1267. 1:11:12creates a binding in the local environment, Staff601, which
  1268. 1:11:18points to the new environment.
  1269. 1:11:22So now, in the future, when Python encounters the name
  1270. 1:11:26Staff601, it will discover that that's an environment.
  1271. 1:11:33Python implements classes as environments.
  1272. 1:11:39So now, when I want to access elements within a class, I use
  1273. 1:11:46a special notation.
  1274. 1:11:47It's a dot notation.
  1275. 1:11:49Python regards dots as ways of navigating an environment.
  1276. 1:11:56When Python parses staff.room, it looks up Staff601 in the
  1277. 1:12:02current environment.
  1278. 1:12:04If it finds an environment, it then says, oh, I know about
  1279. 1:12:08this .room thing.
  1280. 1:12:09All I do is I look up the room name in
  1281. 1:12:14the environment Staff601.
  1282. 1:12:18And when it does that, it gets the answer 501.
  1283. 1:12:21And the same sort of thing happens here.
  1284. 1:12:23It looks up Staff601.
  1285. 1:12:25It finds an environment.
  1286. 1:12:26It looks up coolness.
  1287. 1:12:28It finds out there is no such thing.
  1288. 1:12:29Well, no, that's not true.
  1289. 1:12:32So it creates coolness within 601 and assigns an
  1290. 1:12:40integer, 11, to it.
  1291. 1:12:43So then, the way Python treats methods is completely
  1292. 1:12:51analogous--
  1293. 1:12:54oh, excuse me, instances.
  1294. 1:12:56I'm doing instances first.
  1295. 1:12:58If I make pat be an instance of Staff601, pat is an
  1296. 1:13:04instance of the class Staff601.
  1297. 1:13:08pat is implemented as an environment.
  1298. 1:13:14So when I make pat, pat points to a new environment--
  1299. 1:13:19here, E3.
  1300. 1:13:20The parent of E3 is the class that pat belongs to,
  1301. 1:13:24which is, here, E2.
  1302. 1:13:27And when I make the instance, it's empty.
  1303. 1:13:31But now, if I ask what is pat.course, well, pat points
  1304. 1:13:36to this environment.
  1305. 1:13:37Does this environment have something called a course?
  1306. 1:13:39No.
  1307. 1:13:39Does the parent?
  1308. 1:13:40Yes.
  1309. 1:13:43Course is bound to the string 601.
  1310. 1:13:45So pat.course is 601 just the same as
  1311. 1:13:50Staff601.course had been 601.
  1312. 1:13:53pat is an instance.
  1313. 1:13:56It's a new environment with the class
  1314. 1:14:00environment as its parent.
  1315. 1:14:06You can add attributes to instances.
  1316. 1:14:12And all that does is populate the environment associated
  1317. 1:14:17with the instance.
  1318. 1:14:21You can add methods to classes.
  1319. 1:14:26And that does the same thing.
  1320. 1:14:28So here, I've got the class, Staff601, which has a method,
  1321. 1:14:32salutation, instance variables, course,
  1322. 1:14:35building, and room.
  1323. 1:14:38So when I build that structure, Staff601 points to
  1324. 1:14:42an environment that contains salutation, which is a
  1325. 1:14:45procedure, in addition to a bunch of instance variables.
  1326. 1:14:53So now, all of the rules that we've talked about with regard
  1327. 1:14:56to environments apply now to this class.
  1328. 1:15:00So in particular, I can say Staff601 salutation of pat.
  1329. 1:15:07When Python parses Staff601, it finds an environment.
  1330. 1:15:15It says dot salutation.
  1331. 1:15:17Oh, I know how to do that.
  1332. 1:15:19Within the environment, Staff601, look for a binding
  1333. 1:15:25for the name salutation.
  1334. 1:15:28Do I find one?
  1335. 1:15:29Well, yeah, there it is.
  1336. 1:15:31It points to a procedure.
  1337. 1:15:32So staff dot salutation is a procedure.
  1338. 1:15:35Do just the same things that we would have done with a
  1339. 1:15:38simple procedure.
  1340. 1:15:39The only difference here is that the
  1341. 1:15:41procedure came from a class.
  1342. 1:15:45In this particular case, the sub-routine that I define has
  1343. 1:15:54a formal parameter, self.
  1344. 1:15:57So then, that's going to have to build when I try to
  1345. 1:16:01evaluate it.
  1346. 1:16:01That has to build a binding for self, which is set to pat.
  1347. 1:16:10pat was an environment.
  1348. 1:16:12So self gets pointed to pat.
  1349. 1:16:16So now, when I run Staff601.salutation on pat, it
  1350. 1:16:21behaves as though that generic method was applied to the
  1351. 1:16:25instance pat.
  1352. 1:16:29We'll do that a lot.
  1353. 1:16:30It's a little bit of redundancy.
  1354. 1:16:32We know that pat is a member of Staff601.
  1355. 1:16:38So we will define a special form-- or I should say, Python
  1356. 1:16:41defines a special form-- that makes that easy to say.
  1357. 1:16:44This is the way we will usually say, the instance pat
  1358. 1:16:49should run the class method salutation on itself.
  1359. 1:16:56This is simply a simplified notation that means
  1360. 1:17:00precisely that, OK?
  1361. 1:17:06So what we covered today, then, was supposed to be the
  1362. 1:17:10most elementary ideas in how you construct modular
  1363. 1:17:15programs, Modularity at the small scale.
  1364. 1:17:20How do you make operations that are hierarchical, data
  1365. 1:17:24structures, and classes?
  1366. 1:17:26What we will do for the rest of the week is practice those
  1367. 1:17:30activities.

About this transcript

This page contains the full transcript of Lec 1 | MIT 6.01SC Introduction to Electrical Engineering and Computer Science I, Spring 2011 by MIT OpenCourseWare, generated from the public captions YouTube serves with the video. The transcript has 10,060 words across 1,367 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.