Lecture 1 Cs
Lecture 1 Cs
Lecture 1 Cs
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
WHY COME TO CLASS?
6.100L Lecture 1
OFFICE
PSETS
HOURS
OPTIONAL
PIAZZA (practice)
PROBLEM
SOLVING MANDATORY
FINGER
PRACTICE
EXERCISES
LECTURES
KNOWLEDGE PROGRAMMING
OF CONCEPTS SKILL
RECITATION
EXAMS
6.100L Lecture 1
LET’S GOOOOO!
6
TYPES of KNOWLEDGE
6.100L Lecture 1
NUMERICAL EXAMPLE
6.100L Lecture 1
NUMERICAL EXAMPLE
6.100L Lecture 1
NUMERICAL EXAMPLE
10
6.100L Lecture 1
WE HAVE an ALGORITHM
11
6.100L Lecture 1
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
12
6.100L Lecture 1
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?
13
6.100L Lecture 1
A COMPUTER WILL ONLY DO
WHAT YOU TELL IT TO DO
14
6.100L Lecture 1
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
15
6.100L Lecture 1
STORED PROGRAM COMPUTER
16
6.100L Lecture 1
MEMORY
CONTROL ARITHMETIC
UNIT LOGIC UNIT
program counter do primitive ops
INPUT OUTPUT
17
6.100L Lecture 1
3456 3 7889 5
3457 4 7890 2
3458
3459 True
7891
7892 MEMORY
3460 7893
3461 False 7894
INPUT OUTPUT
18
6.100L Lecture 1
3456 3 7889 5
3457 4 7890 2
3458
3459 True
7891
7892 MEMORY
3460 7893
3461 False 7894
INPUT OUTPUT
19
6.100L Lecture 1
3456 3 7889 5
3457 4 7890 2
3458
3459
7
True
7891
7892 MEMORY
3460 7893
3461 False 7894
INPUT OUTPUT
20
6.100L Lecture 1
3456 3 7889 5
3457 4 7890 2
3458
3459
7
True
7891
7892 MEMORY
3460 7893
3461 False 7894
INPUT OUTPUT
21
6.100L Lecture 1
3456 3 7889 5
3457 4 7890 2
3458
3459
7
True
7891
7892
7
MEMORY
3460 7893
3461 False 7894
INPUT OUTPUT
22
6.100L Lecture 1
3456 3 7889 5
3457 4 7890 2
3458
3459
7
True
7891
7892
7
MEMORY
3460 7893
3461 False 7894
INPUT OUTPUT
23
6.100L Lecture 1
3456 3 7889 5
3457 4 7890 2
3458
3459
7
True
7891
7892
7
MEMORY
3460 7893
3461 False 7894
INPUT OUTPUT
True
24
6.100L Lecture 1
BASIC PRIMITIVES
6.100L Lecture 1
ASPECTS of LANGUAGES
Primitive constructs
English: words
Programming language: numbers, strings, simple operators
26
6.100L Lecture 1
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
27
6.100L Lecture 1
ASPECTS of LANGUAGES
28
6.100L Lecture 1
ASPECTS of LANGUAGES
29
6.100L Lecture 1
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!
30
6.100L Lecture 1
PYTHON PROGRAMS
31
6.100L Lecture 1
PROGRAMMING ENVIRONMENT:
ANACONDA
Code Editor
Shell / Console
32
6.100L Lecture 1
OBJECTS
33
6.100L Lecture 1
OBJECTS
34
6.100L Lecture 1
SCALAR OBJECTS
>>> type(5)
int
>>> type(3.0)
float
35
6.100L Lecture 1
int float
0, 1, 2, …
0.0, …, 0.21, …
300, 301 …
1.0, …, 3.14, …
-1, -2, -3, …
-1.22, …, -500.0 , …
-400, -401, …
bool NoneType
True
False None
36
6.100L Lecture 1
YOU TRY IT!
In your console, find the type of:
1234
8.99
9.0
True
False
37
6.100L Lecture 1
TYPE CONVERSIONS (CASTING)
38
6.100L Lecture 1
YOU TRY IT!
In your console, find the type of:
float(123)
round(7.9)
float(round(7.2))
int(7.2)
int(7.9)
39
6.100L Lecture 1
EXPRESSIONS
40
6.100L Lecture 1
BIG IDEA
Replace complex
expressions by ONE value
Work systematically to evaluate the expression.
41
6.100L Lecture 1
EXAMPLES
>>> 3+2
5
>>> (4+2)*6-1
35
>>> type((4+2)*6-1)
int
>>> float((4+2)*6-1)
35.0
42
6.100L Lecture 1
YOU TRY IT!
In your console, find the values of the following expressions:
(13-4) / (12*12)
type(4*3)
type(4.0*3)
int(1/2)
43
6.100L Lecture 1
OPERATORS on int and float
44
6.100L Lecture 1
SIMPLE OPERATIONS
**
45
6.100L Lecture 1
SO MANY OBJECTS, what to do
with them?!
temp = 100.4
a= 2
go = True
b= -0.3
x= 123 flag = False
n= 17
small = 0.001
46
6.100L Lecture 1
VARIABLES
CS variables
Is bound to one single value at a given time a = b + 1
Can be bound to an expression
(but expressions evaluate to one value!) m = 10
F = m*9.98
47
6.100L Lecture 1
BINDING VARIABLES to VALUES
pi = 355/113
Step 1: Compute the value on the right hand side (the VALUE)
Value stored in computer memory
Step 2: Store it (bind it) to the left hand side (the VARIABLE)
Retrieve value associated with name by invoking the name
(typing it out)
48
6.100L Lecture 1
YOU TRY IT!
Which of these are allowed in Python? Type them in the
console to check.
x = 6
6 = x
x*y = 3+4
xy = 3+4
49
6.100L Lecture 1
ABSTRACTING EXPRESSIONS
6.100L Lecture 1
WHAT IS BEST CODE STYLE?
#do calculations
a = 355/113 *(2.2**2)
c = 355/113 *(2.2*2)
p = 355/113
r = 2.2
#multiply p with r squared
a = p*(r**2)
#multiply p with r times 2
c = p*(r*2)
6.100L Lecture 1
CHANGE BINDINGS
52
6.100L Lecture 1
BIG IDEA
Lines are evaluated one
after the other
No skipping around, yet.
We’ll see how lines can be skipped/repeated later.
53
6.100L Lecture 1
YOU TRY IT!
These 3 lines are executed in order. What are the values of
meters and feet variables at each line in the code?
meters = 100
feet = 3.2808 * meters
meters = 200
ANSWER:
Let’s use PythonTutor to figure out what is going on
Follow along with this Python Tutor LINK
Where did we tell Python to (re)calculate feet?
54
6.100L Lecture 1
YOU TRY IT!
Swap values of x and y without binding the numbers directly.
Debug (aka fix) this code.
x = 1 1
y = 2 x
2
y
y = x
x = y
Python Tutor to the rescue?
ANSWER:
1
x
2
y
temp
55
6.100L Lecture 1
SUMMARY
Objects
Objects in memory have types.
Types tell Python what operations you can do with the objects.
Expressions evaluate to one value and involve objects and operations.
Variables bind names to objects.
= sign is an assignment, for ex. var = type(5*4)
Programs
Programs only do what you tell them to do.
Lines of code are executed in order.
Good variable names and comments help you read code later.
56
6.100L Lecture 1
MITOpenCourseWare
https://ocw.mit.edu
For information about citing these materials or our Terms ofUse,visit: https://ocw.mit.edu/terms.
57