MIT 6.00 Notes From Lessons 1,2 and 3.
MIT 6.00 Notes From Lessons 1,2 and 3.
00 Lecture 1
Computation Knowledge - declerative & imperative Declerative knowledge- statements of fact Imperative knowledge- a description on how to do something Earliest computers were fixed program computers, they had a piece of circutry designed to do a certain operation (calculator, Atanasoff- solved linear equasions, Turing bombe- used during WW2 to break German Enigma codes). Let's image we could build a circuit. The input in that circuit would be any other circuit diagram for some specific task. The first circuit could reconfigure itself to do that specific task. That is what an interpretor does. That is the start of a stored program computer. Such a computer has to have memory, control, ALU(input and output), program counter. In memory you have a sequence of instructions. We have acess to it, we can change it and modify it. The program counter points to a instruction in the memory, runs it through an operation with ALU, and proceeds to the next instruction and keeps processing. Eventualy, a value gets put out. You can think of a program as a recepie- a sequence of instructions. We build it out of primitives. Given a fixed set of primitives, a programer can program anything. Anything you can program in one language, you can do with another, thou some are better at something then others. A language has 3 dimensions (Python): - High level or low level language - General or a targeted language - Interpreted or compiled language
Syntax and semantics Syntax- what are the legal expressions in this language cat dog boy a series of words, not syntaticly correct Semantics- static semantics and full semantics Static semantics- which expressions make sense My desk is Susan- syntaticly correct, but not semanticly Full semantics- what does the program mean, what's gonna happen when I run it.
With Python, it will fully check syntax, we get some help in static semantics, and we dont have any help with full sematics. To deal with full semantics, we have to develop good programing style.
Data elements: Values: -numbers (3, 3.14) - 3- integer - 3.14- floating point or float -strings (ok,3) strings are a type of their own Strings also support slicing, selection and a set of other parameters. Set of operations: -arithmetics for numbers -commands(statements) (example- print) -variables (myString = Eric) Variables have their own value, that can be any expression. Their value is defined by a assigment statement (example. x = 5). A variable inherits its type from its value, so their type is dynamic, it changes with its value. The variable can be used anywhere its value can be used. That is why it is advised not to change variable type arbitrarily.
Commands: -assignment -input/output -conditionals -loop mechanisms Variable names can't be defined by anything, especialy keywords used by Python. Statements: They are legal commands that Python can interpret. (example: print, assignmet(x=3))
In Python: Red are comments. Black are assignment statements. Purple are functions. Orange are keywords. Green are strings. Blue are special values.
Brancing programs- programs that can change the order of instructions based on a test, usually the value of a variable. Example: If <some test> : Block of instructions Else : Other bloc of instructions Boolean combination we can take logical expressions, combine them with and,or, not to make a complex expression and use that value further on. Boolean combination type can be true and false.
Iteration(repeat) example:
While <some test> : Block of code Iterative programs 1) Choose a variable that's going to count it changes during the process 2) Initializing it outside the loop 3) Set up the right end test 4) Constructing the block of code -the variable has to be changing 5) What do I do when I'm done a) Make sure it terminates b) Make sure it gives a reasonable answer despite the input, aka. Make it fool proof Float chart a helpful tool that helps with the perception of the problem and its answer inside programing. Example: float chart for finding a intingers square root Start | Ans=0 | Ans*Ans<X ---------------| | Ans=yes-------------------ans=ans+1 | Print Ans This float chart shows a linear proces, it depends on the size of x.
______________________________________________________________________ Example: float chart for distinguishing even and odd numbers Start | (x/2)*2==X --yes--> print Even
| No | Print Odd This float chart doesn't depend on the size of x. Simulating code pick a simple set of values and simulate them. Example for float chart 1. ans 0 1 2 3 4 x 16 16 16 16 16 Ans*Ans 0 1 4 9 16
Defensive programing: Make sure there is a path through the code. Assume that if you ask of input, they could give you wrong input, and/or the program could have a mistake in itself because of programer mistakes. Exhaustive numerations trying all reasonable values untill the solution is found.
For loop for <some variable> in <some collection> Block of code A tuple an ordered sequence of elements, it is immutable. Foo = (1,2,3,4) Example: >>>Test = (1,2,3) >>>Test[0] 1 >>>Test (1,2,3) >>>Test[10] Error: going out of range >>>Test[-1]
3 >>>Test[-2] 2 From a tuple, we can pick up isolated elements, or groups of elements (slicing). Example: >>>Test[1:2] 2,3 >>>Test[:1] 1 >>>Test[1:] 2,3 Python is a turing complete program, it allows us to write anything that can be described algorithmicly, but it doesn't mean it's going to be simple. 10**6 lines is almost impossible to write alone because we don't have decomposition or abstraction. Decomposition is a way of putting structure on code, breaking it into modules that make sense on their own and that can be re-used. Abstraction will allow us to supress details, the specifics of something and treat that computation like black box, meaning that if you put it the right input it's going give us the right output without us worrying about how.
Functions are a way for us to define small modules so we can later on refer to them just by their name. - break up into modules - suppress detail - create new primitives Example: def. <keyword> (x<-defines the formal parameters) Return when you get to a point in the computations, stop, return the control from this function and take the value of the next expression and return that as the value of the whole computation. None is a special value, it says that there is no value comming back from this computation.
A function is invoked by passing valuies for the parameters: Example: >>>sqrt(16) binds x to value 16, but it binds it only localy, as are all values under this funcion. These binding do not affect global bindings. >>>4 Bindings Global bindings are bindings seen by the interpretor. Local binding are bindings seen by a function that has been called out.
Example: Farmyard problem 20 heads, 56 legs. How many pigs and chickens? numP + numC = 20 4numP + 2numC = 56 ... This will be solved by a brute force algorithm, enumerate&check.