-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_chunks.csv
We can't make this file beautiful and searchable because it's too large.
7007 lines (7003 loc) · 573 KB
/
text_chunks.csv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Index,Text
Lecture1_Chunk1,"MITOCW | watch?v=ytpJdnlu9ug
The following content is provided under a Creative Commons license. Your support will help
MIT OpenCourseWare continue to offer high-quality, educational resources for free. To make
a donation, or view additional materials from hundreds of MIT courses, visit MIT
OpenCourseWare at ocw.mit.edu.
ANA BELL:
All right. Let's begin. As I mentioned before, this lecture will be recorded for OCW. Again, in"
Lecture1_Chunk2,"All right. Let's begin. As I mentioned before, this lecture will be recorded for OCW. Again, in
future lectures, if you don't want to have the back of your head show up, just don't sit in this
front area here.
First of all, wow, what a crowd, you guys. We're finally in 26-100. 6.0001 made it big, huh?
Good afternoon and welcome to the very first class of 6.0001, and also 600, this semester.
My name is Ana Bell. First name, Ana. Last name, Bell. I'm a lecturer in the EECS Department."
Lecture1_Chunk3,"My name is Ana Bell. First name, Ana. Last name, Bell. I'm a lecturer in the EECS Department.
And I'll be giving some of the lectures for today, along with later on in the term, Professor Eric
Grimson, who's sitting right down there, will be giving some of the lectures, as well.
Today we're going to go over some basic administrivia, a little bit of course information. And
then, we're going to talk a little bit about what is computation? We'll discuss at a very high level"
Lecture1_Chunk4,"then, we're going to talk a little bit about what is computation? We'll discuss at a very high level
what computers do just to make sure we're all on the same page.
And then, we're going to dive right into Python basics. We're going to talk a little bit about
mathematical operations you can do with Python. And then, we're going to talk about Python
variables and types.
As I mentioned in my introductory email, all the slides and code that I'll talk about during"
Lecture1_Chunk5,"As I mentioned in my introductory email, all the slides and code that I'll talk about during
lectures will be up before lecture, so I highly encourage you to download them and to have
them open. We're going to go through some in-class exercises which will be available on those
slides. And it's fun to do.
And it's also great if could take notes about the code just for future reference. It's true. This is
a really fast-paced course, and we ramp up really quickly. We do want to position you to"
Lecture1_Chunk6,"a really fast-paced course, and we ramp up really quickly. We do want to position you to
succeed in this course.
As I was writing this, I was trying to think about when I was first starting to program what
helped me get through my very first programming course. And this is really a good list. The
first thing was I just read the psets as soon as they came out, made sure that the terminology
first thing was I just read the psets as soon as they came out, made sure that the terminology"
Lecture1_Chunk7,"first thing was I just read the psets as soon as they came out, made sure that the terminology
just sunk in.
And then, during lectures, if the lecturer was talking about something that suddenly I
remembered, oh, I saw that word in the pset and I didn't know what it was. Well, hey, now I
know what it is. Right? So just give it a read. You don't need to start it.
If you're new to programming, I think the key word is practice. It's like math or reading. The"
Lecture1_Chunk8,"If you're new to programming, I think the key word is practice. It's like math or reading. The
more you practice, the better you get at it. You're not going to absorb programming by
watching me write programs because I already know how to program. You guys need to
practice.
Download the code before lecture. Follow along. Whatever I type, you guys can type. And I
think, also, one of the big things is if you're new to programming, you're kind of afraid that"
Lecture1_Chunk9,"think, also, one of the big things is if you're new to programming, you're kind of afraid that
you're going to break your computer. And you can't really do that just by running Anaconda
and typing in some commands.
So don't be afraid to just type some stuff in and see what it does. Worst case, you just restart
the computer. Yeah. That's probably the big thing right there. I should have probably
highlighted it, but don't be afraid."
Lecture1_Chunk10,"highlighted it, but don't be afraid.
Great. So this is pretty much a roadmap of all of 6.0001 or 600 as I've just explained it.
There's three big things we want to get out of this course. The first thing is the knowledge of
concepts, which is pretty much true of any class that you'll take.
The class will teach you something through lectures. Exams will test how much you know. This
is a class in programming. The other thing we want you to get out of it is programming skills."
Lecture1_Chunk11,"is a class in programming. The other thing we want you to get out of it is programming skills.
And the last thing, and I think this is what makes this class really great, is we teach you how to
solve problems. And we do that through the psets. That's really how I feel the roadmap of this
course looks like.
And underlying all of these is just practice. You have to just type some stuff away and code a
lot. And you'll succeed in this course, I think."
Lecture1_Chunk12,"lot. And you'll succeed in this course, I think.
OK. So what are the things we're going to learn in this class? I feel like the things we're going
learn in this class can be divided into basically three different sections. The first one is related
to these first two items here. It's really about learning how to program.
Learning how to program, part of it is figuring out what objects to create. You'll learn about"
Lecture1_Chunk13,"Learning how to program, part of it is figuring out what objects to create. You'll learn about
these later. How do you represent knowledge with data structures? That's sort of the broad
term for that.
And then, as you're writing programs, you need to-- programs aren't just linear. Sometimes
programs jump around. They make decisions. There's some control flow to programs. That's
what the second line is going to be about."
Lecture1_Chunk14,"what the second line is going to be about.
The second big part of this course is a little bit more abstract, and it deals with how do you
write good code, good style, code that's readable. When you write code, you want to write it
such that-- you're in big company, other people will read it, other people will use it, so it has to
be readable and understandable by others.
To that end, you need to write code that's well organized, modular, easy to understand. And"
Lecture1_Chunk15,"To that end, you need to write code that's well organized, modular, easy to understand. And
not only that, not only will your code be read by other people, but next year, maybe, you'll take
another course, and you'll want to look back at some of the problems that you wrote in this
class.
You want to be able to reread your code. If it's a big mess, you might not be able to
understand-- or reunderstand-- what you were doing. So writing readable code and organizing
code is also a big part."
Lecture1_Chunk16,"code is also a big part.
And the last section is going to deal with-- the first two are actually part of the programming in
Introduction to Programming and Computer Science in Python. And the last one deals mostly
with the computer science part in Introduction to Programming and Computer Science in
Python.
We're going to talk about, once you have learned how to write programs in Python, how do
you compare programs in Python? How do you know that one program is better than the
other?"
Lecture1_Chunk17,"you compare programs in Python? How do you know that one program is better than the
other?
How do you know that one program is more efficient than the other? How do you know that
one algorithm is better than the other? That's what we're going to talk about in the last part of
the course.
OK. That's all for the administrative part of the course. Let's start by talking at a high level what
does a computer do."
Lecture1_Chunk18,"does a computer do.
Fundamentally, it does two things. One, performs calculations. It performs a lot of calculations.
Computers these days are really, really fast, a billion calculations per second is probably not
far off. It performs these calculations and it has to store them somewhere. Right? Stores them
in computer memory.
So a computer also has to remember results. And these days, it's not uncommon to find"
Lecture1_Chunk19,"So a computer also has to remember results. And these days, it's not uncommon to find
computers with hundreds of gigabytes of storage. The kinds of calculations that computers do,
there are two kinds.
One are calculations that are built into the language. These are the very low level types of
calculations, things like addition, subtraction, multiplication, and so on.
And once you have a language that has these primitive calculation types, you, as a"
Lecture1_Chunk20,"And once you have a language that has these primitive calculation types, you, as a
programmer, can put these types together and then define your own calculations. You can
create new types of calculations. And the computer will be able to perform those, as well.
I think, one thing I want to stress-- and we're going to come back to this again during this
entire lecture, actually-- is computers only know what you tell them. Computers only do what"
Lecture1_Chunk21,"entire lecture, actually-- is computers only know what you tell them. Computers only do what
you tell them to do. They're not magical. They don't have a mind.
They just know how to perform calculations really, really quickly. But you have to tell them
what calculations to do. Computers don't know anything. All right. We've come to that.
Let's go into the types of knowledge. The first type of knowledge is declarative knowledge. And"
Lecture1_Chunk22,"Let's go into the types of knowledge. The first type of knowledge is declarative knowledge. And
those are things like statements of fact. And this is where my email came into play. If you read
it all the way to the bottom, you would have entered a raffle.
So a statement of fact for today's lecture is, someone will win a prize before class ends. And
the prize was a Google Cardboard. Google state-of-the-art virtual reality glasses. And I have
them right here. Yea. I delivered on my promise."
Lecture1_Chunk23,"them right here. Yea. I delivered on my promise.
That's a statement of fact. So pretend I'm a machine. OK? I don't know anything except what
you tell me. I don't know. I know that you tell me this statement. I'm like, OK. But how is
someone going to win a Google Cardboard before class ends, right?
That's where imperative knowledge comes in. Imperative knowledge is the recipe, or the how-
to, or the sequence of steps. Sorry. That's just my funny for that one. So the sequence of"
Lecture1_Chunk24,"to, or the sequence of steps. Sorry. That's just my funny for that one. So the sequence of
steps is imperative knowledge.
If I'm a machine, you need to tell me how someone will win a Google Cardboard before class.
If I follow these steps, then technically, I should reach a conclusion.
Step one, I think we've already done that. Whoever wanted to sign up has signed up. Now I'm
going to open my IDE. I'm just basically being a machine and following the steps that you've
told me."
Lecture1_Chunk25,"told me.
The IDE that we're using in this class is called Anaconda. I'm just scrolling down to the bottom.
Hopefully, you've installed it in problem set zero. I've opened my IDE. I'm going to follow the
next set of instructions. I'm going to choose a random number between the first and the nth
responder.
Now, I'm going to actually use Python to do this . And this is also an example of how just a
really simple task in your life, you can use computers or programming to do that. Because if I"
Lecture1_Chunk26,"really simple task in your life, you can use computers or programming to do that. Because if I
chose a random number, I might be biased because, for example, I might like the number 8.
To choose a random number, I'm going to go and say, OK, where's the list of responders? It
starts at 15. Actually, it starts at 16 because that's me. We're going to choose a random
number between 16 and the end person 266. Oh, we just got-- oh. OK."
Lecture1_Chunk27,"number between 16 and the end person 266. Oh, we just got-- oh. OK.
OK. I'm going to cut it off right here. 271. OK. 16 and 271. Perfect. OK. I'm going to choose a
random number. I'm going to go to my IDE. And you don't need to know how to do this yet, but
by the end of this class, you will. I'm just going to use Python.
I'm just going to get the random number package that's going to give me a random number.
I'm going to say random.randint. And I'm going to choose a random number between 16 and"
Lecture1_Chunk28,"I'm going to say random.randint. And I'm going to choose a random number between 16 and
272,
OK. 75. OK. Great. I chose a random number. And I'm going to find the number in the
responder's sheet. What was the number again? Sorry. 75. OK. Up we go. There we go.
Lauren Z-O-V. Yeah. Nice. You're here.
Awesome. All right. That's an example of me being a machine and also, at the same time,
using Python in my everyday life, just lecturing, to find a random number. Try to use Python"
Lecture1_Chunk29,"using Python in my everyday life, just lecturing, to find a random number. Try to use Python
wherever you can. And that just gives you practice.
That was fun. But we're at MIT. We're MIT students. And we love numbers here at MIT. Here's
a numerical example that shows the difference between declarative and imperative
a numerical example that shows the difference between declarative and imperative
knowledge."
Lecture1_Chunk30,"a numerical example that shows the difference between declarative and imperative
knowledge.
An example of declarative knowledge is the square root of a number x is y such that y times y
is equal to x. That's just a statement of fact It's true. Computers don't know what to do with
that. They don't know what to do with that statement. But computers do know how to follow a
recipe.
Here's a well-known algorithm. To find the square root of a number x, let's say x is originally"
Lecture1_Chunk31,"Here's a well-known algorithm. To find the square root of a number x, let's say x is originally
16, if a computer follows this algorithm, it's going to start with a guess, g, let's say, 3. We're
trying to find the square root of 16.
We're going to calculate g times g is 9. And we're going to ask is if g times g is close enough to
x, then stop and say, g is the answer.
I'm not really happy with 9 being really close to 16. So I'm going to say, I'm not stopping here.
I'm going to keep going."
Lecture1_Chunk32,"I'm going to keep going.
If it's not close enough, then I'm going to make a new guess by averaging g and x over g.
That's x over g here. And that's the average over there.
And the new average is going to be my new guess. And that's what it says. And then, the last
step is using the new guess, repeat the process. Then we go back to the beginning and repeat
the whole process over and over again.
And that's what the rest of the rows do. And you keep doing this until you decide that you're"
Lecture1_Chunk33,"And that's what the rest of the rows do. And you keep doing this until you decide that you're
close enough. What we saw for the imperative knowledge in the previous numerical example
was the recipe for how to find the square root of x. What were the three parts of the recipe?
One was a simple sequence of steps. There were four steps. The other was a flow of control,
so there were parts where we made decisions. Are we close enough? There were parts where"
Lecture1_Chunk34,"so there were parts where we made decisions. Are we close enough? There were parts where
we repeated some steps. At the end, we said, repeat steps 1, 2, 3. That's the flow of control.
And the last part of the recipe was a way to stop. You don't want a program that keeps going
and going. Or for a recipe, you don't want to keep baking bread forever. You want to stop at
some point. Like 10 breads is enough, right? So you have to have a way of stopping."
Lecture1_Chunk35,"some point. Like 10 breads is enough, right? So you have to have a way of stopping.
In the previous example, the way of stopping was that we decided we were close enough.
Close enough was maybe being within .01, .001, whatever you pick. This recipe is there for an
algorithm. In computer science speak, it's going to be an algorithm. And that's what we're
going to learn about in this class.
We're dealing with computers. And we actually want to capture a recipe inside a computer, a"
Lecture1_Chunk36,"We're dealing with computers. And we actually want to capture a recipe inside a computer, a
computer being a mechanical process. Historically, there were two different types of
computers. Originally, there were these things called fixed-program computers.
And I'm old enough to have used something like this, where there's just numbers and plus,
minus, multiplication, divide, and equal. But calculators these days are a lot more complicated."
Lecture1_Chunk37,"minus, multiplication, divide, and equal. But calculators these days are a lot more complicated.
But way back then, an example of a fixed-program computer is this calculator. It only knows
how to do addition, multiplication, subtraction, division. If you want to plot something, you can't.
If you want to go on the internet, send email with it, you can't.
It can only do this one thing. And if you wanted to create a machine that did another thing,"
Lecture1_Chunk38,"It can only do this one thing. And if you wanted to create a machine that did another thing,
then you'd have to create another fixed-program computer that did a completely separate test.
That's not very great.
That's when stored-program computers came into play. And these were machines that could
store a sequence of instructions. And these machines could execute the sequence of
instructions. And you could change the sequence of instructions and execute this different
sequence of instructions."
Lecture1_Chunk39,"sequence of instructions.
You could do different tasks in the same machine. And that's the computer as we know it
these days. The central processing unit is where all of these decisions get made. And these
are all the peripherals.
The basic machine architecture-- at the heart of every computer there's just this basic
architecture-- and it contains, I guess, four main parts. The first is the memory. Input and
output is the other one."
Lecture1_Chunk40,"output is the other one.
The ALU is where all of the operations are done. And the operations that the ALU can do are
really primitive operations, addition, subtraction, and so on.
What the memory contains is a bunch of data and your sequence of instructions. Interacting
with the Arithmetic Logic Unit is the Control Unit. And the Control Unit contains one program
counter.
When you load a sequence of instructions, the program counter starts at the first sequence. It"
Lecture1_Chunk41,"When you load a sequence of instructions, the program counter starts at the first sequence. It
starts at the sequence, at the first instruction. It gets what the instruction is, and it sends it to
the ALU.
The ALU asks, what are we doing operations on here? What's happening? It might get some
data. If you're adding two numbers, it might get two numbers from memory. It might do some
operations. And it might store data back into memory."
Lecture1_Chunk42,"operations. And it might store data back into memory.
And after it's done, the ALU is going to go back, and the program counter is going to increase
by 1, which means that we're going to go to the next sequence in the instruction set. And it just
goes linearly, instruction by instruction.
There might be one particular instruction that does some sort of test. It's going to say, is this
particular value greater or equal to or the same as this other particular value? That's a test, an"
Lecture1_Chunk43,"particular value greater or equal to or the same as this other particular value? That's a test, an
example of a test. And the test is going to either return true or false.
And depending on the result of that test, you might either go to the next instruction, or you
might set the program counter to go all the way back to the beginning, and so on. You're not
just linearly stepping through all the instructions. There might be some control flow involved,"
Lecture1_Chunk44,"just linearly stepping through all the instructions. There might be some control flow involved,
where you might skip an instruction, or start from the beginning, or so on.
And after you're done, when you finished executing the last instruction, then you might output
something. That's really the basic way that a computer works. Just to recap, you have the
stored program computer that contains these sequences of instructions."
Lecture1_Chunk45,"stored program computer that contains these sequences of instructions.
The primitive operations that it can do are addition, subtraction, logic operations, tests-- which
are something equal to something else, something less than, and so on-- and moving data, so
storing data, moving data around, and things like that.
And the interpreter goes through every instruction and decides whether you're going to go to
the next instruction, skip instructions, or repeat instructions, and so on."
Lecture1_Chunk46,"the next instruction, skip instructions, or repeat instructions, and so on.
So we've talked about primitives. And in fact, Alan Turing, who was a really great computer
scientist, he showed that you can compute anything using the six primitives. And the six
primitives are move left, move right, read, write, scan, and do nothing.
Using those six instructions and the piece of tape, he showed that you can compute anything."
Lecture1_Chunk47,"Using those six instructions and the piece of tape, he showed that you can compute anything.
And using those six instructions, programming languages came about that created a more
convenient set of primitives. You don't have to program in only these six commands.
And one interesting thing, or one really important thing, that came about from these six
primitives is that if you can compute something in Python, let's say-- if you write a program that"
Lecture1_Chunk48,"primitives is that if you can compute something in Python, let's say-- if you write a program that
computes something in Python, then, in theory, you can write a program that computes the
exact same thing in any other language. And that's a really powerful statement.
Think about that today when you review your slides. Think about that again. That's really
powerful. Once you have your set of primitives for a particular language, you can start creating"
Lecture1_Chunk49,"powerful. Once you have your set of primitives for a particular language, you can start creating
expressions. And these expressions are going to be combinations of the primitives in the
programming language.
And the expressions are going to have some value. And they're going up some meaning in the
programming language. Let's do a little bit of a parallel with English just so you see what I
mean. In English, the primitive constructs are going to be words. There's a lot of words in the"
Lecture1_Chunk50,"mean. In English, the primitive constructs are going to be words. There's a lot of words in the
English language.
Programming languages-- in Python, there are primitives, but there aren't as many of them.
There are floats, Booleans, these are numbers, strings, and simple operators, like addition,
subtraction, and so on. So we have primitive constructs.
Using these primitive constructs, we can start creating, in English, phrases, sentences, and the"
Lecture1_Chunk51,"Using these primitive constructs, we can start creating, in English, phrases, sentences, and the
same in programming languages. In English, we can say something like, ""cat, dog, boy. That,
we say, is not syntactically valid. That's bad syntax. That's noun, noun, noun. That doesn't
make sense.
What does have good syntax in English is noun, verb, noun. So, ""cat, hugs boy"" is syntactically
valid. Similarly, in a programming language, something like this-- in Python, in this case-- a"
Lecture1_Chunk52,"valid. Similarly, in a programming language, something like this-- in Python, in this case-- a
word and then the number five doesn't really make sense. It's not syntactically valid. But
something like operator, operand, operator is OK.
So once you've created these phrases, or these expressions, that are syntactically valid, you
have to think about the static semantics of your phrase, or of your expression. For example, in
English, ""I are hungry"" is good syntax."
Lecture1_Chunk53,"English, ""I are hungry"" is good syntax.
But it's weird to say. We have a pronoun, a verb, and an adjective, which doesn't really make
sense. ""I am hungry"" is better. This does not have good static semantics.
Similarly, in programming languages-- and you'll get the hang of this the more you do it--
something like this, ""3.2 times 5, is OK. But what does it mean? What's the meaning to have a
word added to a number? There's no meaning behind that."
Lecture1_Chunk54,"word added to a number? There's no meaning behind that.
Its syntax is OK, because you have operator, operand, operator. But it doesn't really make
sense to add a number to a word, for example.
Once you have created these expressions that are syntactically correct and static,
semantically correct, in English, for example, you think about the semantics. What's the
meaning of the phrase? In English, you can actually have more than one meaning to an entire
phrase."
Lecture1_Chunk55,"meaning of the phrase? In English, you can actually have more than one meaning to an entire
phrase.
In this case, ""flying planes can be dangerous"" can have two meanings. It's the act of flying a
plane is dangerous, or the plane that is in the air is dangerous.
And this might be a cuter example. ""This reading lamp hasn't uttered a word since I bought it.
What's going on?"" So that has two meanings. It's playing on the word ""reading lamp."""
Lecture1_Chunk56,"What's going on?"" So that has two meanings. It's playing on the word ""reading lamp.""
That's in English. In English, you can have a sentence that has more than one meaning, that's
syntactically correct and static, semantically correct. But in programming languages, the
program that you write, the set of instructions that you write, only has one meaning.
Remember, we're coming back to the fact that the computer only does what you tell it to do."
Lecture1_Chunk57,"Remember, we're coming back to the fact that the computer only does what you tell it to do.
It's not going to suddenly decide to add another variable for some reason. It's just going to
execute whatever statements you've put up. In programming languages, there's only one
meaning.
But the problem that comes into play in programming languages is it's not the meaning that
you might have intended, as the programmer. That's where things can go wrong."
Lecture1_Chunk58,"you might have intended, as the programmer. That's where things can go wrong.
And there's going to be a lecture on debugging a little bit later in the course. But this is here
just to tell you that if you see an error pop up in your program, it's just some text that says,
error. For example, if we do something like this, this is syntactically correct. Incorrect.
Syntactically incorrect. See? There's some angry text right here. What is going on?"
Lecture1_Chunk59,"Syntactically incorrect. See? There's some angry text right here. What is going on?
The more you program, the more you'll get the hang of reading these errors. But this is
basically telling me the line that I wrote is syntactically incorrect. And it's pointing to the exact
line and says, this is wrong, so I can go back and fix it as a programmer.
Syntax errors are actually really easily caught by Python. That was an example of a syntax"
Lecture1_Chunk60,"Syntax errors are actually really easily caught by Python. That was an example of a syntax
error. Static semantic errors can also be caught by Python as long as, if your program has
some decisions to make, as long as you've gone down the branch where the static semantic
error happens.
And this is probably going to be the most frustrating one, especially as you're starting out. The
program might do something different than what you expected it to do. And that's not because"
Lecture1_Chunk61,"program might do something different than what you expected it to do. And that's not because
the program suddenly-- for example, you expected the program to give you an output of 0 for
a certain test case, and the output that you got was 10.
Well, the program didn't suddenly decide to change its answer to 10. It just executed the
program that you wrote. That's the case where the program gave you a different answer than
expected."
Lecture1_Chunk62,"program that you wrote. That's the case where the program gave you a different answer than
expected.
Programs might crash, which means they stop running. That's OK. Just go back to your code
and figure out what was wrong. And another example of a different meaning than what you
intended was maybe the program won't stop. It's also OK. There are ways to stop it besides
restarting the computer.
So then Python programs are going to be sequences of definitions and commands. We're"
Lecture1_Chunk63,"So then Python programs are going to be sequences of definitions and commands. We're
going to have expressions that are going to be evaluated and commands that tell the
interpreter to do something.
If you've done problem set 0, you'll see that you can type commands directly in the shell here,
which is the part on the right where I did some really simple things, 2 plus 4. Or you can type
commands up in here, on the left-hand side, and then run your program."
Lecture1_Chunk64,"commands up in here, on the left-hand side, and then run your program.
Notice that, well, we'll talk about this-- I won't talk about this now. But these are-- on the right-
hand side, typically, you write very simple commands just if you're testing something out. And
on the left-hand side here in the editor, you write more lines and more complicated programs.
Now we're going to start talking about Python. And in Python, we're going to come back to this,"
Lecture1_Chunk65,"Now we're going to start talking about Python. And in Python, we're going to come back to this,
everything is an object. And Python programs manipulate these data objects. All objects in
Python are going to have a type.
And the type is going to tell Python the kinds of operations that you can do on these objects. If
an object is the number five, for example, you can add the number to another number,
subtract the number, take it to the power of something, and so on."
Lecture1_Chunk66,"subtract the number, take it to the power of something, and so on.
As a more general example, for example, I am a human. So that's my type. And I can walk,
speak English, et cetera. Chewbacca is going to be a type Wookie. He can walk, do that sound
that I can't do. He can do that, but I can't. I'm not even going to try, and so on.
Once you have these Python objects, everything is an object in Python. There are actually two"
Lecture1_Chunk67,"Once you have these Python objects, everything is an object in Python. There are actually two
types of objects. One are scalar objects. That means these are very basic objects in Python
from which everything can be made. These are scalar objects. That can't be subdivided.
The other type of object is a non-scalar object. And these are objects that have some internal
structure. For example, the number five is a scalar object because it can't be subdivided."
Lecture1_Chunk68,"structure. For example, the number five is a scalar object because it can't be subdivided.
But a list of numbers, for example, 5, 6, 7,8, is going to be a non-scalar object because you
can subdivide it. You can subdivide it into-- you can find parts to it. It's made up of a sequence
of numbers.
Here's the list of all of the scalar objects in Python. We have integers, for example, all of the
whole numbers. Floats, which are all of the real numbers, anything with a decimal."
Lecture1_Chunk69,"whole numbers. Floats, which are all of the real numbers, anything with a decimal.
Bools are Booleans. There's only two values to Booleans. That's True and False. Note the
capitalization, capital T and capital F. And this other thing called NoneType. It's special. It has
only one value called None. And it represents the absence of a type. And it sometimes comes
in handy for some programs.
If you want to find the type of an object, you can use this special command called type. And"
Lecture1_Chunk70,"If you want to find the type of an object, you can use this special command called type. And
then in the parentheses, you put down what you want to find the type of. You can write into the
shell ""type of 5,"" and the shell will tell you, that's an integer.
If you happen to want to convert between two different types, Python allows you to do that.
And to do that, you put the type that you want to convert to right before the object that you"
Lecture1_Chunk71,"And to do that, you put the type that you want to convert to right before the object that you
want to convert to. So float(3) will convert the integer 3 to the float 3.0.
And similarly, you can convert any float into an integer. And converting to an integer just
truncates. It just takes away the decimal and whatever's after it-- it does not round-- and keeps
just the integer part.
For this slide, I'm going to talk about it. But if you'd like if you have the slides up, go to go to"
Lecture1_Chunk72,"For this slide, I'm going to talk about it. But if you'd like if you have the slides up, go to go to
this exercise. And after I'm done talking about the slide, we'll see what people think for that
exercise.
One of the most important things that you can do in basically any programming, in Python
also, is to print things out. Printing out is how you interact with the user.
To print things out, you use the print command. If you're in the shell, if you simply type ""3 plus"
Lecture1_Chunk73,"To print things out, you use the print command. If you're in the shell, if you simply type ""3 plus
2,"" you do see a value here. Five, right? But that's not actually printing something out.
And that becomes apparent when you actually type things into the editor. If you just do ""3 plus
2,"" and you run the program-- that's the green button here-- you see on the right-hand side
here, it ran my program. But it didn't actually print anything."
Lecture1_Chunk74,"here, it ran my program. But it didn't actually print anything.
If you type this into the console, it does show you this value, but that's just like peeking into the
value for you as a programmer. It's not actually printing it out to anyone. If you want to print
something out, you have to use the print statement like that. In this case, this is actually going
to print this number five to the console."
Lecture1_Chunk75,"to print this number five to the console.
That's basically what it says. It just tells you it's an interaction within the shell only. It's not
interacting with anyone else. And if you don't have any ""Out,"" that means it got printed out to
the console.
All right. We talked a little bit about objects. Once you have objects, you can combine objects
and operators to form these expressions. And each expression is going to have a value. So an"
Lecture1_Chunk76,"and operators to form these expressions. And each expression is going to have a value. So an
expression evaluates to a value. The syntax for an expression is going to be object, operator,
object, like that.
And these are some operators you can do on ints and floats. There's the typical ones,
addition, subtraction, multiplication, and division. If, for the first three, the answer that you get--
the type of the answer that you get-- is going to depend on the type of your variables. If both"
Lecture1_Chunk77,"the type of the answer that you get-- is going to depend on the type of your variables. If both
of the variables of the operands are integers, then the result you're going to get is of type
integer.
But if at least one of them is a float, then the result you're going to get is a float. Division is a
little bit special in that no matter what the operands are, the result is always going to be a float.
The other operations you can do, and these are also useful, are the remainder, so the percent"
Lecture1_Chunk78,"The other operations you can do, and these are also useful, are the remainder, so the percent
sign. If you use the percent sign between two operands, that's going to give you the remainder
when you divide i by j.
And raising something to the power of something else is using the star star operator. And i star
stars j is going to take i to the power of j.
These operations have the typical precedence that you might expect in math, for example."
Lecture1_Chunk79,"These operations have the typical precedence that you might expect in math, for example.
And if you'd like to put precedence toward some other operations, you can use parentheses to
do that.
All right. So we have ways of creating expressions. And we have operations we can do on
objects. But what's going to be useful is to be able to save values to some name. And the
name is going to be something that you pick."
Lecture1_Chunk80,"name is going to be something that you pick.
And it should be a descriptive name. And when you save the value to a name, you're going to
be able to access that value later on in your program. And that's very useful.
To save a value to a variable name, you use the equal sign. And the equal sign is an
assignment. It assigns the right-hand side, which is a value, to the left-hand side, which is
going to be a variable name. In this case, I assigned the float 3.14159 to the variable pi."
Lecture1_Chunk81,"going to be a variable name. In this case, I assigned the float 3.14159 to the variable pi.
And in the second line, I'm going to take this expression, 22 divided by 7, I'm going to evaluate
it. It's going to come up with some decimal number. And I'm going to save it into the variable
pi_approx. values are stored in memory. And this assignment in Python, we say the
assignment binds the name to the value.
When you use that name later on in your program, you're going to be referring to the value in"
Lecture1_Chunk82,"When you use that name later on in your program, you're going to be referring to the value in
memory. And if you ever want to refer to the value later on in your code, you just simply type
the name of the variable that you've assigned it to.
So why do we want to give names to expressions? Well, you want to reuse the names instead
of the values. And it makes your code look a lot nicer. This is a piece of code that calculates"
Lecture1_Chunk83,"of the values. And it makes your code look a lot nicer. This is a piece of code that calculates
the area of a circle. And notice, I've assigned a variable pi to 3.14159. I've assigned another
variable called radius to be 2.2.
And then, later on in my code, I have another line that says area-- this is another variable-- is
equal to-- this is an assignment-- to this expression. And this expression is referring to these
variable names, pi and radius."
Lecture1_Chunk84,"variable names, pi and radius.
And it's going look up their values in memory. And it's going to replace these variable names
with those values. And it's going to do the calculation for me. And in the end, this whole
expression is going to be replaced by one number. And it's going to be the float.
Here's another exercise, while I'm talking about the slide. I do want to make a note about
programming versus math."
Lecture1_Chunk85,"programming versus math.
In math, you're often presented with a problem that says, solve for x. x plus y is equal to
something something. Solve for x, for example. That's coming back to the fact that computers
don't know what to do with that. Computers need to be told what to do.
In programming, if you want to solve for x, you need to tell the computer exactly how to solve
for x. You need to figure out what formula you need to give the computer in order to be able to
solve for x."
Lecture1_Chunk86,"solve for x.
That means always in programming the right-hand side is going to be an expression. It's
something that's going to be evaluated to a value. And the left-hand side is always a variable.
It's going to be an assignment.
The equal sign is not like in math where you can have a lot of things to the left and a lot of
things to the right of the equal sign. There's only one thing to the left of the equal sign. And
that's going to be a variable. An equal sign stands for an assignment."
Lecture1_Chunk87,"that's going to be a variable. An equal sign stands for an assignment.
Once we've created expressions, and we have these assignments, you can rebind variable
names using new assignment statements. Let's look at an example for that. Let's say this is
our memory. Let's type back in the example with finding the radius.
Let's say, pi is equal to 3.14. In memory, we're going to create this value 3.14. We're going to"
Lecture1_Chunk88,"Let's say, pi is equal to 3.14. In memory, we're going to create this value 3.14. We're going to
bind it to the variable named pi. Next line, radius is equal to 2.2. In memory, we're creating this
value 2.2. And we're going to bind it to the variable named radius.
Then we have this expression here. It's going to substitute the values for pi from memory and
the value for radius from memory. It's going to calculate the value that this expression
evaluates to."
Lecture1_Chunk89,"evaluates to.
It's going to pop that into the memory. And it's going to assign-- because we're using the equal
sign-- it's going to assign that value to that variable area.
Now, let's say we rebind radius to be something else. Radius i is bound to the value 2.2. But
when we do this line, radius is equal to radius plus 1, we're going to take away the binding to
2.2. We're going to do this calculation. The new value is 3.2."
Lecture1_Chunk90,"2.2. We're going to do this calculation. The new value is 3.2.
And we're going to rebind that value to that same variable. In memory, notice we're still going
to have this value, 2.2, floating around. But we've lost the handle for it. There's no way to get it
back. It's just in memory sitting there. At some point, it might get collected by what we call the
garbage collector. In Python, And it'll retrieve these lost values, and it'll reuse them for new
values, and things like that."
Lecture1_Chunk91,"values, and things like that.
But radius now points to the new value. We can never get back 2.2. And that's it. The value of
area-- notice, this is very important. The value of area did not change. And it did not change
because these are all the instructions we told the computer to do.
We just told it to change radius to be radius plus 1. We never told it to recalculate the value of
area. If I copied that line down here, then the value of area would change. But we never told it"
Lecture1_Chunk92,"area. If I copied that line down here, then the value of area would change. But we never told it
to do that. The computer only does what we tell it to do.
That's the last thing. Next lecture, we're going to talk about adding control flow to our
programs, so how do you tell the computer to do one thing or another? All right."
Lecture2_Chunk1,"MITOCW | watch?v=0jljZRnHwOI
The following content is provided under a Creative Commons license. Your support will help
MIT OpenCourseWare continue to offer high quality educational resources for free. To make a
donation or view additional materials from hundreds of MIT courses, visit MIT
OpenCourseWare at ocw.mit.edu.
PROFESSOR:
All right. Let's get started, everyone. So, good afternoon. Welcome to the second lecture of"
Lecture2_Chunk2,"All right. Let's get started, everyone. So, good afternoon. Welcome to the second lecture of
60001 and also of 600. So as always, if you'd like to follow along with the lectures, please go
ahead and download the slides and the code that I'll provide at least an hour before class
every day. All right.
So a quick recap of what we did last time. So last time, we talked a little bit about what a
computer is. And I think the main takeaway from the last lecture is really that a computer only"
Lecture2_Chunk3,"computer is. And I think the main takeaway from the last lecture is really that a computer only
does what it is told, right? So it's not going to spontaneously make decisions on its own. You,
as the programmer, have to tell it what you want it to do by writing programs. OK. So we talked
about simple objects. And these objects were of different types. So we saw integers, floats,
and Booleans. And then we did a couple of simple operations with them."
Lecture2_Chunk4,"and Booleans. And then we did a couple of simple operations with them.
Today, we're going to look at a different-- a new type of object called a string. And then we're
going to introduce some more powerful things in our programming toolbox. So we're going to
look at how to branch within a program, and how to make things-- how to make the computer
repeat certain tasks within our program.
All right. So let's begin by looking at strings. So strings are a new object type. We've seen so"
Lecture2_Chunk5,"All right. So let's begin by looking at strings. So strings are a new object type. We've seen so
far integers, which were whole numbers, floats, which were decimal numbers, and we have
seen Booleans, which were true and false. So strings are going to be sequences of
characters. And these characters can be anything. They can be letters, digits, special
characters, and also spaces. And you tell Python that you're talking about a string object by"
Lecture2_Chunk6,"characters, and also spaces. And you tell Python that you're talking about a string object by
enclosing it in quotation marks. So in this case, I'm creating an object whose value is h-e-l-l-o
space t-h-e-r-e.
And Python knows it's a string object, because we're enclosing it in quotations. They can be
either double quotes or single quotes, but as long as you're consistent, it doesn't matter. And
this object, we're binding it to this variable named hi. And we're using that using the equals"
Lecture2_Chunk7,"this object, we're binding it to this variable named hi. And we're using that using the equals
sign, which is the assignment operator. So from now on, whenever we refer to this variable hi,
Python is going to say, oh, I know what the value is, and it's that string of characters.
So we're going to learn about two things that you can do on strings today, two operations. One
is to concatenate them. And concatenation is really just a fancy word for using this plus"
Lecture2_Chunk8,"is to concatenate them. And concatenation is really just a fancy word for using this plus
operator, which means put the strings together. So I have this original variable named hi, and I
create a new variable called name. And in it, I'm going to assign the string a-n-a to the variable
name. And when I use the plus operator in between hi and name, those two variables, Python
is going to look at the values of those two, and it's going to just put them together."
Lecture2_Chunk9,"is going to look at the values of those two, and it's going to just put them together.
OK. I'm going to switch to Spider. And this is just that example from the slides. So let's see
what happens. So I have the variable hi, the variable name, and I'm just concatenating those
two together. And then I'm going to print that out. So if I run the code, notice it prints out ""hello
thereana."" There's no space. And there's no space because the concatenation operator, the"
Lecture2_Chunk10,"thereana."" There's no space. And there's no space because the concatenation operator, the
plus, doesn't add any spaces implicitly. So again, another example of just computer just doing
what it's told. If we want to add a space, we'd have to actually insert the space manually. So
that's this line here, line 8. And in this line, we're concatenating the value of the variable hi with
a space. Notice we're putting it in quotation marks. Just a space. And then with name."
Lecture2_Chunk11,"a space. Notice we're putting it in quotation marks. Just a space. And then with name.
So if we'll go ahead and print that value, notice this was that garbage greeting there. And now
we have a proper greeting, right? So that's the concatenation between strings.
And then the other thing we're going to look at related to strings is the star operator. So that's
this one here on line 10. So Python allows you to use the star operator, which stands for"
Lecture2_Chunk12,"this one here on line 10. So Python allows you to use the star operator, which stands for
multiplication, between a string and a number. And when you do that, Python interprets it as
repeat that string that many number of times. So in this case, I'm creating a silly greeting, and
I'm concatenating the value of hi, which is ""hello there"" with the space plus the name. So
notice here, I'm using parentheses to tell Python, do this operation first, and then multiply"
Lecture2_Chunk13,"notice here, I'm using parentheses to tell Python, do this operation first, and then multiply
whatever the result of this is by 3. So if I print that out, it's going to multiply the space with my
name three times, and it's going to concatenate that with ""hello there."" So that's exactly what it
printed out there.
Last lecture, we talked a little bit about print. Today, I'm going to talk about some nuances"
Lecture2_Chunk14,"Last lecture, we talked a little bit about print. Today, I'm going to talk about some nuances
related to print. So you use print to interact with the user. It's cool to write programs that print
things out to the user. So the key word here being print. And then you put parentheses after
print. And in the parentheses, you put in whatever you want to show the user.
So in this little program, I have-- I created a variable named x. I assigned it the value 1, and"
Lecture2_Chunk15,"So in this little program, I have-- I created a variable named x. I assigned it the value 1, and
then I print 1. Here, I'm casting. So I'm taking the number one, the integer 1, and I'm casting it
to a string. And you'll see why in a moment.
So I want to bring to your attention a couple of things here. So in the first print, I'm using
commas everywhere here. And in the second print, I'm using plus. So by definition, if you-- you"
Lecture2_Chunk16,"commas everywhere here. And in the second print, I'm using plus. So by definition, if you-- you
can use commas inside a print-- inside the parentheses of print. And if you use a comma,
Python is going to automatically add a space in between the two things that the comma is in
between, the values. So ""my fav num is"" is the first thing. And the second thing is whatever's
after the comma. Let's take x. So if you use a comma, Python is going to automatically insert a
space for you."
Lecture2_Chunk17,"space for you.
Sometimes, you might want that, sometimes you might not. If you don't want that, you can use
the concatenation operation, the plus. And you can add all of your little bits together to create
one big string. If you're using commas, the items, the objects in between the commas, do not
all have to be strings. That's the plus side of using commas. But the downside is you get
spaces everywhere. If you use plus operator, the plus side is Python does exactly what you tell"
Lecture2_Chunk18,"spaces everywhere. If you use plus operator, the plus side is Python does exactly what you tell
it to do, but everything has to be a string object. So ""my fav num is"" is a string object. You
have to convert all of your numbers to string objects, and so on.
So if we look at Spider-- This is the same-- almost the same code. So here, I don't have
spaces anywhere. So you can see that the first line here has commas everywhere. So I'm"
Lecture2_Chunk19,"spaces anywhere. So you can see that the first line here has commas everywhere. So I'm
going to have spaces in between every one of the things that I'm printing out. This line here is
sort of a combination between commas and concatenation. So depending on where I used the
comma, I'm going to have an extra space. And this line here just has concatenation
everywhere. So if I run this, notice this very first line added spaces everywhere in between all"
Lecture2_Chunk20,"everywhere. So if I run this, notice this very first line added spaces everywhere in between all
my objects. The second one added spaces somewhere. And you can sort of trace through and
see exactly where the spaces were added. And the last line here didn't add spaces anywhere.
So printing things out to the console is nice, but the second part of sort of writing an interactive
program is getting input from the user. And that's the more interesting part. So if you've done"
Lecture2_Chunk21,"program is getting input from the user. And that's the more interesting part. So if you've done
problem set 0, you might have sort of already tried to understand this on your own. But here
we are. So the way you get input from the user is using this command function called input.
And inside the parentheses, you type in whatever you'd like to prompt the user with. So in this
case, in my example here, I have input, and then here I said ""type anything."" So the user is"
Lecture2_Chunk22,"case, in my example here, I have input, and then here I said ""type anything."" So the user is
going to see this text here, and then the program is just going to stop. And it's going to wait for
the user to type in something and hit Enter. As soon as the user types in Enter, whatever the
user types in becomes a string. If a user types in a number, for example, that becomes the
string of that number. So everything the user types in is going to be made as a string."
Lecture2_Chunk23,"string of that number. So everything the user types in is going to be made as a string.
In this line right here, whatever these the user types in becomes a string. And we're going to
bind that string object to this variable named text. So now, further in my program, I could do
whatever I want with this variable text. In this case, I'm going to print 5*text. OK. So if the user,
for example, gave me ""ha,"" I'm going to print ""ha"" 5 times. If the user gave me 5, what do you"
Lecture2_Chunk24,"for example, gave me ""ha,"" I'm going to print ""ha"" 5 times. If the user gave me 5, what do you
think the user is-- what do you think is going to be printed out? 25 or 5 five times? Great. Yes.
Exactly. 5 five times.
Oftentimes, you don't want to work with numbers as strings, right? You want to work with
numbers as numbers, right? So you have to cast. And we learned that last lecture. You cast by
just putting in this little bit right in front of the input. And you can cast it to whatever type you"
Lecture2_Chunk25,"want. Here I cast it to an int, but you can also cast to a float if you want to work with floats. And
that converts whatever the user typed in, as long as it's some number that Python knows how
to convert, into the number itself. So in this case, if the user gives me 5, I'm going to print out 5
times 5 instead of 5 five times. So that's the code here.
So the first bit is I'm going to get the user to type in anything, and I'm going to put 555. And"
Lecture2_Chunk26,"So the first bit is I'm going to get the user to type in anything, and I'm going to put 555. And
then when I type in the number, since I'm casting it, I'm going to do operations with the
number. Yeah, question.
AUDIENCE:
[INAUDIBLE]
PROFESSOR:
Why do you want to cast to-- oh. The question is why do you want to cast to a string? Why do
you want to cast a string to a number?
AUDIENCE:
[INAUDIBLE]
PROFESSOR:
Oh, so Python always-- whatever you type in, just by default, by definition of the input"
Lecture2_Chunk27,"PROFESSOR:
Oh, so Python always-- whatever you type in, just by default, by definition of the input
command, Python always makes it a string. So if you want to work with numbers, you have to
explicitly tell it, I'm going to work with a number. So even if you give it the number 5, it's going
to think it's the string 5. Yeah. That's just how input works.
The next thing we're going to look at is ways that you can start adding tests in your code. And"
Lecture2_Chunk28,"The next thing we're going to look at is ways that you can start adding tests in your code. And
before you can start adding tests in your code, you need to be able to do the actual tests. So
this is where comparison operators come in. So here, let's assume that i and j are variables.
The following comparisons are going to give you a Boolean. So it's either going to say, this is
true or this is false. So that's going to be your test."
Lecture2_Chunk29,"true or this is false. So that's going to be your test.
So if i and j are variables, you're allowed to compare ints with ints, floats with floats, strings
with strings. And you're allowed to compare ints and floats between themselves, but you're not
allowed to compare a string with a number. In fact, if you even try to do that in Python-- in
Spider here, if I try to say, is the letter a greater than 5? I get some angry text right here. And"
Lecture2_Chunk30,"Spider here, if I try to say, is the letter a greater than 5? I get some angry text right here. And
this just tells me Python doesn't understand the meaning of-- how do I compare a string with a
number?
OK. So just like in math, we can do these usual comparisons. We can say if something is
greater than something, greater or equal to, less than, less than or equal to. I'd like to bring to
your attention the equality. So the single equals sign is an assignment. So you're taking a"
Lecture2_Chunk31,"your attention the equality. So the single equals sign is an assignment. So you're taking a
value, and you're assigning it to a variable. But when you're doing the double equals sign, this
is the test for equality. Is the value of variable i the same as the value of the variable j? And
that's, again, also going to give you a Boolean either true or false. And you can also test for
inequality with the exclamation equal. So that means, is the value of the variable i not equal to"
Lecture2_Chunk32,"inequality with the exclamation equal. So that means, is the value of the variable i not equal to
the value of the variable j? True if yes, false if no.
OK. So those are comparison operators on integer, floats, and strings. On Booleans, you can
do some logic operators. And the simplest is just inverting. So if a is a variable that has a
Boolean value, not a is just going to invert it. So if a is true, then not a is false, and vice versa."
Lecture2_Chunk33,"This is a table that sort of represents what I've said here. So you can do-- you can use and
and or. These are key words in Python. You can use those two key words on variables, on
Boolean variables. And you get the result a and b is only true if both a and b are true. And a or
b is only false if a and b are false. And this is the complete table just in case you need to
reference it.
All right. So now that we have ways to do logical-- question right there.
AUDIENCE:
[INAUDIBLE]
PROFESSOR:"
Lecture2_Chunk34,"AUDIENCE:
[INAUDIBLE]
PROFESSOR:
Yeah, great question. So what does it mean to compare a string with a string with the greater
than? So that's just going to compare them, lexicographically. So does it come first in the
alphabet? So we can even test that out. We can say, is a greater than b? And it's false. So b
comes later in the alphabet than a.
OK. So now we have ways to do the tests. So we can add some branching to our"
Lecture2_Chunk35,"OK. So now we have ways to do the tests. So we can add some branching to our
programming toolbox now that we have ways to do tests. This is a map of MIT. I'm going to go
through sort of a little example to motivate why we would want to do branching in our code.
And I think after this lecture, you'll be able to sort of code up this algorithm that I'm going to
explain. So most of us see MIT as a maze. I first did when I came here. When I first came"
Lecture2_Chunk36,"explain. So most of us see MIT as a maze. I first did when I came here. When I first came
here, obviously, I signed up for the free food mailing list. And MIT, being a maze, I had no idea
where to go, what the shortest path was to free food. So one way to think about it is all I
wanted to do was get to the free food.
A very simple algorithm to get there would be to say, OK, I'm going take my right hand, and
I'm going to make sure that my right hand is always on a wall. And I'm going to go around"
Lecture2_Chunk37,"I'm going to make sure that my right hand is always on a wall. And I'm going to go around
campus with my right hand always being at a wall. And eventually, I'll get to where the free
food is. There might not be any left, right? But I'll be there. So the algorithm is as follows. If my
right hand always has to be on a wall, then I'm going to say, if there's no wall to my right side,
then I'm going to go right until I get to a wall. Then if there's a wall to my right, and I can go"
Lecture2_Chunk38,"then I'm going to go right until I get to a wall. Then if there's a wall to my right, and I can go
forward, I'm just going to keep going forward. If I keep going forward, and there's a wall to my
right and in front of me, I'm going to turn around and go left. And then if there's a wall to my
right, in front of me, and to the left, then I'm going to turn around and go back.
So with this fairly simple algorithm, I just follow the path always keeping the wall to my right."
Lecture2_Chunk39,"So with this fairly simple algorithm, I just follow the path always keeping the wall to my right.
And eventually, I would end up where I need to be. So notice, I used, just in plain English, a
few key words. If, otherwise, things like that. So in programming, we have those same
constructs. And those same sort of intuitive words can be used to tell Python to do something
or to do something else or to choose from a different set of possibilities. And this way, we can"
Lecture2_Chunk40,"or to do something else or to choose from a different set of possibilities. And this way, we can
get the computer to make decisions for us. And you might be thinking, well, you said that
computers can't make decisions on their own. It's not. You, as programmers, are going to
build these decisions into the program, and all the computer is going to do is going to reach
the decision point and say, OK, this is a decision point, should I go left or should I go right? Or"
Lecture2_Chunk41,"the decision point and say, OK, this is a decision point, should I go left or should I go right? Or
which one do I pick? And these sort of decisions are created by you as a programmer. And the
computer just has to make the decision and choose a path.
OK. So in programming, there's three sort of simple ways that you can add control flow to your
programs. And that's making one decision and choosing whether to execute something or"
Lecture2_Chunk42,"programs. And that's making one decision and choosing whether to execute something or
execute something else. The first is a simple if. And given a program that just linearly has
statements that get executed, whenever I reach an if statement, you're going to check the
condition. The condition is going to be something that's going to get evaluated to either true or
false.
So I've reached the condition here. And if the condition is true, then I'm going to additionally"
Lecture2_Chunk43,"So I've reached the condition here. And if the condition is true, then I'm going to additionally
execute this extra set of expressions. But if the condition is false, then I'm just going to keep
going through the program and not execute that extra set of instructions. How does Python
know which instructions to execute? They're going to be inside this what we call code block.
And the code block is denoted by indentation. So it's going to be everything that's indented is"
Lecture2_Chunk44,"And the code block is denoted by indentation. So it's going to be everything that's indented is
part of that if code block. Typically, four spaces is indentation.
OK. So that's how you write code that decides whether to execute this extra thing or not. Now
let's say I don't just want to execute an extra thing, I want to reach a point where I say, I'll
either go down this path or I'll do something else. That's this right here. So this if else construct"
Lecture2_Chunk45,"says this is my code, I've reached my decision point here, if the condition inside the if is true,
then I'm going to execute maybe this set of statements here. But if the condition is not true,
then I'm not going to execute that set of statements, and instead I'm going to execute under
whatever else is. So using this construct, I'm either going to do one set of expressions or the
other, but never both. And after I've executed one or the other, I'm going to continue on with"
Lecture2_Chunk46,"other, but never both. And after I've executed one or the other, I'm going to continue on with
just the regular execution of the program.
OK. So we're able to either choose one thing, choose one thing or another, but what if we
want to have more than one choice? So if some number is equal to zero, I want to do this. If
it's equal to 1, I want to do this. If it's equal to 2, I want to do this, and so on. That's where this"
Lecture2_Chunk47,"last one comes in. And we introduced this other key word here called elif. So that stands for
short form for else if. So first we check if this condition is true. So we're going through our
program, we've reached our decision point, if the condition is true, we're going to execute
maybe this set of instructions. If the condition is not true, maybe we'll check-- if the condition is
not true, we will check this next condition. That's part of the elif right here. And if that one's"
Lecture2_Chunk48,"not true, we will check this next condition. That's part of the elif right here. And if that one's
true, we're going to execute a different set of instructions. You can have more than one elif.
And depending on which one's true, you're going to execute a different set of instructions. And
then this last else is sort of a catch all where if none of the previous conditions were true, then
just do this last set of expressions."
Lecture2_Chunk49,"just do this last set of expressions.
So in this case, you're going to choose between one of these three-- one of these four roots,
or however many you have. And then when you're done making your choice, you're going to
execute the remaining set of instructions. So the way this works is if more than one condition is
true, you're actually just going to enter one of them. And you're going to enter the very first"
Lecture2_Chunk50,"true, you're actually just going to enter one of them. And you're going to enter the very first
one that's true. So you're never going to enter more than one of these code blocks. You
always enter one, and you enter the first one that evaluates to true.
So notice that we denoted code blocks using indentation. And that's actually one of the things
that I really like about Python. It sort of forces you to write pretty code and nice looking code"
Lecture2_Chunk51,"that I really like about Python. It sort of forces you to write pretty code and nice looking code
and just code that's very readable. And that forces you to indent everything that's a code
block. So you can easily see sort of where the flow of control is and where decision making
points are and things like that. So in this particular example, we have one if statement here,
and it checks if two variables are equal. And we have an if, elif, else. And in this example,"
Lecture2_Chunk52,"and it checks if two variables are equal. And we have an if, elif, else. And in this example,
we're going to enter either this code block or this one or this one, depending on the variables
of x and y. And we're only going into one code block. And we'll enter the first one that's true.
Notice you can have nested conditionals. So inside this first if, we have another if here. And
this inner if is only going to be checked when we enter the first-- this outter if. I do want to"
Lecture2_Chunk53,"this inner if is only going to be checked when we enter the first-- this outter if. I do want to
make one point, though. So sometimes, you might forget to do the double equals sign when
you are checking for equality, and that's OK. If you just use one equals sign, Python's going to
give you an error. And it's going to say syntax error, and it's going to highlight this line. And
then you're going to know that there's a mistake there. And you should be using equality,"
Lecture2_Chunk54,"then you're going to know that there's a mistake there. And you should be using equality,
because it doesn't make sense to be using-- to assign-- to be making an assignment inside
the if.
So we've learned about branching. And we know about conditionals. Let's try to apply this to a
little game. And spoiler, we won't be able to. We'll have to learn about a new thing. But back in
the 1980s, there was the Legend of Zelda-- cool graphics-- where there was a scene with the"
Lecture2_Chunk55,"the 1980s, there was the Legend of Zelda-- cool graphics-- where there was a scene with the
lost woods. Oversimplification if anyone's a Zelda die hard fan. But the basic idea was if you
entered the woods, you entered from the left to the right. And then as long as you kept going
right, it would show you the same screen over and over again. And the trick was you just had
to go backward, and then you'd exit the woods. So very simple."
Lecture2_Chunk56,"to go backward, and then you'd exit the woods. So very simple.
Using what we know so far, we could sort of code this up. And we'd say something like this. If
the user exits right, then set the background to the woods background. Otherwise, set the
background to the exit background. Now let's say the user-- and then in the else, we're done.
Let's say the user went right. Well, you'd show them the woods background, and now ask"
Lecture2_Chunk57,"Let's say the user went right. Well, you'd show them the woods background, and now ask
them again, where do they want to go? If they exit right, set the background to the woods
background. Otherwise, set the background to the exit background, and so on.
So you notice that there's sort of no end to this, right? How many times-- do you know how
many times the user might keep going right? They might be really persistent, right? And they'll"
Lecture2_Chunk58,"many times the user might keep going right? They might be really persistent, right? And they'll
be like maybe if I go 1,000 times, I'll get out of the woods. Maybe 1,001? Maybe. So this would
probably be-- who knows how deep? These nested ifs. So we don't know. So with what we
know so far, we can't really code this cute little game.
But enter loops. And specifically, a while loop. So this code here that could be infinitely number"
Lecture2_Chunk59,"But enter loops. And specifically, a while loop. So this code here that could be infinitely number
of nested if statements deep can be rewritten using these three lines. So we say while the user
exits right, set the background to the woods background. And with a while loop, it's going to do
what we tell it to do inside the loop, and then it's going to check the condition again, and then
it's going to do what we say it should do inside the code block, and it's going to check the"
Lecture2_Chunk60,"it's going to do what we say it should do inside the code block, and it's going to check the
condition again. And then when the condition-- as long as a condition is true, it's going to keep
doing that little loop there. And as soon as the condition becomes false, it's going to stop doing
the loop and do whatever's right after the while.
OK. So that's basically how a while loop works. We have while. That's the key word. The"
Lecture2_Chunk61,"OK. So that's basically how a while loop works. We have while. That's the key word. The
condition is something that gets evaluated to true or false. And once again, we have a code
block that's indented, and it tells Python, these are the expressions I want to do as long as the
condition is true. So the condition is true, you evaluate every expression in the code block.
When you reach the end of the expression-- end of the code block, you check the condition"
Lecture2_Chunk62,"When you reach the end of the expression-- end of the code block, you check the condition
again. If it's true still, you keep doing the expressions. Check it again, and so on.
So here's a little game. And with these lines of code, we were able-- we can code up the lost
woods of Zelda. Even worse graphics, by the way than the original Zelda is this one that I
coded up here. So I print out the following things. ""You're in the Lost Forest. Go left or right."""
Lecture2_Chunk63,"coded up here. So I print out the following things. ""You're in the Lost Forest. Go left or right.""
And my program's going to say, ""You're in the Lost Forest. Go left or right."" It's going to get
user input. It's going to say while the user keeps typing in right, show them this text, and ask
them again. So I'm asking them again by just saying input here again. And that's it. That's
going to just keep getting input from the user. And if the user doesn't type in right, and maybe"
Lecture2_Chunk64,"going to just keep getting input from the user. And if the user doesn't type in right, and maybe
types in left, you're going to exit out of this loop, and print out, ""You've got out of the Lost
Forest.""
So I have to show you this, because I spent too much time on it. But I decided to improve on
the code that's in the slides. And I've written here ways that you guys can also improve it. So if
I run my code-- ""You're in the Lost Forest. Go left or right."" So if I say left, then yay, I got out of"
Lecture2_Chunk65,"the Lost Forest. But if I go right, then I'm stuck, right? I took down some trees. You can see
there's no more trees here. I made a table, and then I flipped it over.
So the expansion to this if you want to try it out-- I put this in the comments here-- is try to use
a counter. If the user types in right the first two times, just make that a sad face. But if the user
types in more than two times, make them cut down some trees and build a table and flip it."
Lecture2_Chunk66,"types in more than two times, make them cut down some trees and build a table and flip it.
That's a cute little expansion if you want to test yourself to make sure you are getting loops.
OK. So so far, we've used while loops to ask for user input. And that's actually somewhere
where it makes sense to use while loops, because you don't actually know how many times
the user is going to type in something. You can use while loops to keep sort of a counter and"
Lecture2_Chunk67,"the user is going to type in something. You can use while loops to keep sort of a counter and
to write code that counts something. If you do that, though, there's two things you need to take
care of. The first is the first line here, which is sort of an initialization of this loop counter. And
the second is this line here, which is incrementing your loop counter.
The reason why the second one is important is because-- let's look at our condition here. So"
Lecture2_Chunk68,"The reason why the second one is important is because-- let's look at our condition here. So
while n is less than five. If you didn't have this line here, you would never increment n. So
every time through the loop, you just keep printing zeros. And you would have an infinite loop.
I do want to show, though, what-- if you do have an infinite loop, it's not the end of the world.
So I can say something like-- so while true, print zero. So this is going to give me an infinite"
Lecture2_Chunk69,"So I can say something like-- so while true, print zero. So this is going to give me an infinite
loop in my program. And-- whoop. OK. So notice it's just printing the letter p over and over
again. And if I let it go any longer, it's going to slow down the computer. So I'm going to hit
Control-C or Command-C maybe. And it's going to stop the program from printing. So just in
case you ever enter infinite loops in your programs, just go to the console and hit Control-C,"
Lecture2_Chunk70,"case you ever enter infinite loops in your programs, just go to the console and hit Control-C,
and that's going to stop it from sort of slowing down the computer.
OK. So going back to this example, I was saying that if you're using counters-- variables in
order to sort of count up inside the while loop, you have to take care to initialize a counter
variable first. And then to increment it, otherwise you'll enter an infinite loop. That feels a little"
Lecture2_Chunk71,"bit tedious. And so there's a shortcut for doing that exact same thing.
So these four lines, you can rewrite those into these two lines right here using this new type of
loop called a for loop. So the for loop says, for some loop variable-- in this case, I named it n.
You can name it whatever you want. In range 5-- we're going to come back to what range
means in a little bit-- print n. So every time through the loop, you're going to print out what the"
Lecture2_Chunk72,"means in a little bit-- print n. So every time through the loop, you're going to print out what the
value of n is. Range 5 actually creates internally a sequence of numbers starting from 0 and
going to that number 5 minus 1.
So the sequence is going to be 0, 1, 2, 3, and 4. The first time through the loop, you're going
to say n is equal to 0. Or internally, this is what happens. N gets the value of 0. You're going to"
Lecture2_Chunk73,"to say n is equal to 0. Or internally, this is what happens. N gets the value of 0. You're going to
print n. Then you're going to go back to the top. N gets the value 1. Then you're going to go
execute whatever is inside. So you're going to print 1. Then you're going to increment that to
the next value in the sequence. You're going to print out 2, and so on.
So this is the general look of a for loop. So we have for some loop variable-- again, can be"
Lecture2_Chunk74,"So this is the general look of a for loop. So we have for some loop variable-- again, can be
named whatever you want-- in range some number. Do a bunch of stuff. And again, these are
part of this for loop code block. So you should indent them to tell Python that these are the
things that you should do. So when you're using range some number, you start out with
variable getting the value 0. With variable having value 0, you're going to execute all of these"
Lecture2_Chunk75,"variable getting the value 0. With variable having value 0, you're going to execute all of these
expressions. After all the expressions in the code block are done, you're going to go on to the
next value. So 1. You're going to execute all these expressions with the variable being value 1,
and then so on and so on until you go to some num minus 1.
That-- so using range in that way is a little bit constraining, because you're always going to get"
Lecture2_Chunk76,"That-- so using range in that way is a little bit constraining, because you're always going to get
values starting from 0 and ending at some num minus 1, whatever is in the parentheses in
range. Sometimes you might want to write programs that maybe start at a custom value. Don't
start at 0. Maybe they start at 5. Maybe they start at minus 10. And sometimes you might want
to write programs that don't go with-- don't expect the numbers by 1, but maybe skip every"
Lecture2_Chunk77,"to write programs that don't go with-- don't expect the numbers by 1, but maybe skip every
other number, go every two numbers, or every three numbers, and so on.
So you can customize range to your needs. The one thing you do need to give it is the stop.
So if you give it only one value in the parentheses that stands for stop. And by default, start is
going to have the value 0, and step is going to have the value 1. If you give it two things in the"
Lecture2_Chunk78,"going to have the value 0, and step is going to have the value 1. If you give it two things in the
parentheses, you're giving it start and stop. So the first being start, the second being stop. And
step gets this value of 1 by default. And if you give it three things in the parentheses, you're
giving it start, stop, and step in that order. And you're always going to start at the start value
and stop at-- or so you're going to start at the start value, and you're going to go until stop"
Lecture2_Chunk79,"and stop at-- or so you're going to start at the start value, and you're going to go until stop
minus 1. So those are the sequences of numbers.
So in this first code right here, my sum is going to get the value 0. And you're going to have a
for loop. We're going to start from 7, because we're giving it two numbers. And when you give
it two numbers, it represents start and stop with step being 1. So we're starting at 7. If step is"
Lecture2_Chunk80,"it two numbers, it represents start and stop with step being 1. So we're starting at 7. If step is
1, the next value is 8. What's the value after that? If we're incrementing by 1? 9. And since
we're going until stop minus 1, we're not actually going to pick up on 10. So this loop variable,
i, the very first time through the loop is going to have the value 7. So my sum is going to be 0
plus 7. That's everything that's inside the code block."
Lecture2_Chunk81,"plus 7. That's everything that's inside the code block.
The next time through the loop, i gets the value 8. So inside the for loop, my sum gets
whatever the previous value was, which was 7, plus 8. OK. The next time through the loop, my
sum get the value 7 plus 8 plus 9. Obviously, replacing that with the previous value. So 15.
Since we're not going through 10, that's where we stop. And we're going to print out my sum,
which is going to be the value of 7 plus 8 plus 9. Yeah? OK. Yeah.
AUDIENCE:"
Lecture2_Chunk82,"which is going to be the value of 7 plus 8 plus 9. Yeah? OK. Yeah.
AUDIENCE:
[INAUDIBLE]
PROFESSOR:
Do they have to be integers? That's a great question. We can try that out. I'm not actually sure
right off the top of my head. So you can go on Spider and say-- let's say in this example here.
So we can say 7.1, 10.3-- yeah. So they have to be integers. OK. So that's that example. And
let's erase that. In this particular example, we have start, stop, and step. And here, we're going"
Lecture2_Chunk83,"let's erase that. In this particular example, we have start, stop, and step. And here, we're going
every other value. So we're starting at 5. Tell me what the next value is supposed to be. If
we're taking every other one. 7, and then 9, and then-- are we doing 11 or not? Excellent.
Nice. Yeah. So we're going to the end minus 1. OK.
So it's possible that sometimes you write code where you might want to exit out of the loop"
Lecture2_Chunk84,"So it's possible that sometimes you write code where you might want to exit out of the loop
early. You don't want to go through all of the sequences of your numbers. Maybe there's a
condition inside there where you just want to exit the loop early. Inside the while loop, maybe
you want to exit the loop before the condition becomes false. So that's where the break
statement comes in. So the break works like this. It's going to-- as soon as Python sees this"
Lecture2_Chunk85,"statement comes in. So the break works like this. It's going to-- as soon as Python sees this
break statement, it's going to say, OK, I'm going to look at whatever loop I'm currently in. I'm
not evaluating any expression after it that comes within my loop. And I'm going to immediately