MIT python course all class slide
WELCOME!
(download slides and .py files from
the class site to follow along)

6.100L Lecture 1

Ana Bell

1
TODAY
Course info
What is computation
Python basics
Mathematical operations
Python variables and types
NOTE: slides and code files up before each lecture
Highly encourage you to download them before class
Take notes and run code files when I do
Do the in-class “You try it” breaks
Class will not be recorded
Class will be live-Zoomed for those sick/quarantine
6.100L Lecture 1
2
WHY COME TO CLASS?
You get out of this course what you put into it
Lectures
Intuition for concept
Teach you the concept
Ask me questions!
Examples of concept
Opportunity to
practice practice practice

Repeat
6.100L Lecture 1
3
PRACTICE
February 3, 2016
6.0001 LECTURE 1
PROBLEM
SOLVING

PROGRAMMING
SKILL

PSETS

EXAMS

MANDATORY
FINGER
EXERCISES

OPTIONAL

(practice)

OFFICE
HOURS

PIAZZA

LECTURES

RECITATION

KNOWLEDGE

OF CONCEPTS

6.100L Lecture 1
4
TOPICS
Solving problems using computation
Python programming language
Organizing modular programs
Some simple but important algorithms
Algorithmic complexity
6.100L Lecture 1
5
LET’S GOOOOO!
6
TYPES of KNOWLEDGE
Declarative knowledge is statements of fact
Imperative knowledge is a recipe or “how-to”
Programming is about writing recipes to generate facts
6.100L Lecture 1
7
NUMERICAL EXAMPLE
Square root of a number x is y such that y*y = x
Start with a guess, g
1)
If g*g is close enough to x, stop and say g is the answer
2)
Otherwise make a new guess by averaging g and x/g
3)
Using the new guess, repeat process until close enough
Let’s try it for x = 16 and an initial guess of 3
g
g*g x/g (g+x/g)/2
3
9 16/3 4.17
6.100L Lecture 1
8
NUMERICAL EXAMPLE
Square root of a number x is y such that y*y = x
Start with a guess, g
1)
If g*g is close enough to x, stop and say g is the answer
2)
Otherwise make a new guess by averaging g and x/g
3)
Using the new guess, repeat process until close enough
Let’s try it for x = 16 and an initial guess of 3
g
g*g x/g (g+x/g)/2
3
9 16/3 4.17
4.17
17.36 3.837 4.0035
6.100L Lecture 1
9
NUMERICAL EXAMPLE
Square root of a number x is y such that y*y = x
Start with a guess, g
1)
If g*g is close enough to x, stop and say g is the answer
2)
Otherwise make a new guess by averaging g and x/g
3)
Using the new guess, repeat process until close enough
Let’s try it for x = 16 and an initial guess of 3
g
g*g x/g (g+x/g)/2
3
9 16/3 4.17
4.17
17.36 3.837 4.0035
4.0035
16.0277 3.997 4.000002
6.100L Lecture 1
10
WE HAVE an ALGORITHM
1) Sequence of simple steps

2) Flow of control process that specifies when each step is
executed

3) A means of determining when to stop

6.100L Lecture 1
11
ALGORITHMS are RECIPES /
RECIPES are ALGORITHMS

Bake cake from a box
1) Mix dry ingredients
2) Add eggs and milk
3) Pour mixture in a pan
4) Bake at 350F for 5 minutes
5) Stick a toothpick in the cake
6a) If toothpick does not come out clean, repeat step 4 and 5
6b) Otherwise, take pan out of the oven
7) Eat
6.100L Lecture 1
12
COMPUTERS are MACHINES that
EXECUTE ALGORITHMS

Two things computers do:
Performs simple operations
100s of billions per second!

Remembers results
100s of gigabytes of storage!

What kinds of calculations?
Built-in to the machine, e.g., +
Ones that you define as the programmer
The BIG IDEA here?
6.100L Lecture 1
13
A COMPUTER WILL ONLY DO
WHAT YOU TELL IT TO DO

6.100L Lecture 1
14
COMPUTERS are MACHINES that
EXECUTE ALGORITHMS

Fixed program computer
Fixed set of algorithms
What we had until 1940’s
Stored program computer
Machine stores and executes instructions
Key insight: Programs are no different from other kinds of data
6.100L Lecture 1
15
STORED PROGRAM COMPUTER
Sequence of instructions stored inside computer
Built from predefined set of primitive instructions
1)
Arithmetic and logical
2)
Simple tests
3)
Moving data
Special program (interpreter) executes each instruction in
order

Use tests to change flow of control through sequence
Stops when it runs out of instructions or executes a halt instruction
6.100L Lecture 1
16
6.100L Lecture 1
MEMORY

CONTROL
UNIT

ARITHMETIC
LOGIC UNIT

INPUT
OUTPUT
program counter
do primitive ops
17
6.100L Lecture 1
MEMORY

CONTROL
UNIT

ARITHMETIC
LOGIC UNIT

INPUT
OUTPUT
3456
3
3457
4
3458

3459
True
3460

3461
False
7889
5
7890
2
7891

7892

7893

7894

Add
3456 3457
Add
7889 7890
Store
3458
Store
7891
Compare
3458 7891
Print

18
6.100L Lecture 1
MEMORY

CONTROL
UNIT

ARITHMETIC
LOGIC UNIT

INPUT
OUTPUT
3456
3
3457
4
3458

3459
True
3460

3461
False
7889
5
7890
2
7891

7892

7893

7894

Add
3456 3457
Add
7889 7890
Store
3458
Store
7891
Compare
3458 7891
Print

19
6.100L Lecture 1
MEMORY

CONTROL
UNIT

ARITHMETIC
LOGIC UNIT

INPUT
OUTPUT
3456
3
3457
4
3458
7
3459
True
3460

3461
False
7889
5
7890
2
7891

7892

7893

7894

Add
3456 3457
Add
7889 7890
Store
3458
Store
7891
Compare
3458 7891
Print

20
6.100L Lecture 1
MEMORY

CONTROL
UNIT

ARITHMETIC
LOGIC UNIT

INPUT
OUTPUT
3456
3
3457
4
3458
7
3459
True
3460

3461
False
7889
5
7890
2
7891

7892

7893

7894

Add
3456 3457
Add
7889 7890
Store
3458
Store
7891
Compare
3458 7891
Print

21
6.100L Lecture 1
MEMORY

CONTROL
UNIT

ARITHMETIC
LOGIC UNIT

INPUT
OUTPUT
3456
3
3457
4
3458
7
3459
True
3460

3461
False
7889
5
7890
2
7891
7
7892

7893

7894

Add
3456 3457
Add
7889 7890
Store
3458
Store
7891
Compare
3458 7891
Print

22
6.100L Lecture 1
MEMORY

CONTROL
UNIT

ARITHMETIC
LOGIC UNIT

INPUT
OUTPUT
3456
3
3457
4
3458
7
3459
True
3460

3461
False
7889
5
7890
2
7891
7
7892

7893

7894

Add
3456 3457
Add
7889 7890
Store
3458
Store
7891
Compare
3458 7891
Print

23
6.100L Lecture 1
MEMORY

CONTROL
UNIT

ARITHMETIC
LOGIC UNIT

INPUT
OUTPUT
3456
3
3457
4
3458
7
3459
True
3460

3461
False
7889
5
7890
2
7891
7
7892

7893

7894

Add
3456 3457
Add
7889 7890
Store
3458
Store
7891
Compare
3458 7891
Print

True

24
BASIC PRIMITIVES
Turing showed that you can compute anything with a very
simple machine with only 6 primitives: left, right, print, scan,
erase, no op

Real programming languages have
More convenient set of primitives
Ways to combine primitives to create new primitives
Anything computable in one language is computable in any
other programming language

6.100L Lecture 1
© source unknown. All rights reserved. This
content is excluded from our Creative Commons
license. For more information, see
https://ocw.mit.edu/help/faq-fair-use/
25
ASPECTS of LANGUAGES
Primitive constructs
English: words
Programming language: numbers, strings, simple operators
6.100L Lecture 1
26
ASPECTS of LANGUAGES
Syntax
English: "cat dog boy" not syntactically valid
"cat hugs boy" syntactically valid

Programming language: "hi"5 not syntactically valid
"hi"*5 syntactically valid

6.100L Lecture 1
27
ASPECTS of LANGUAGES
Static semantics: which syntactically valid strings have meaning
English: "I are hungry" syntactically valid
but static semantic error

PL: "hi"+5 syntactically valid
but static semantic error

6.100L Lecture 1
28
ASPECTS of LANGUAGES
Semantics: the meaning associated with a syntactically correct
string of symbols with no static semantic errors

English: can have many meanings "The chicken is
ready to eat."

Programs have only one meaning
But the meaning may not be what programmer intended
6.100L Lecture 1
29
WHERE THINGS GO WRONG
Syntactic errors
Common and easily caught
Static semantic errors
Some languages check for these before running
program

Can cause unpredictable behavior
No linguistic errors, but different meaning
than what programmer intended

Program crashes, stops running
Program runs forever
Program gives an answer, but it’s wrong!
6.100L Lecture 1
30