【公开课】麻省理工学院:Python教程(全65讲)

計算機科學、編程思維與原語


Imperative knowledge is a methods or recipes for finding something.

Not perfect, but close enough.

Normally, the flow runs this way, but based on that test, we have a way of going back up and continuing the path.

- sequence of instructions

primitive instructions
→(construction)sequence of instructions
→(interpreter)executes each instructions in order
- A basic machine architecture

Turing showed that using six primitives, can compute anything, and modern programming languages have a more convenient set of primitives.
our goal: capture how to knowledge by breaking problems down into mechanical steps and then to figure out how to turn that into something the machine understands,
- primitive operation

- primitive constructs
what we need to pay attention:
- syntax
- semantics





編程知識




- Python programs


- Scalar object


運算優先級:乘方>乘法>整除>加法>減法

- comparison


- non-scalar objects



可不可以這樣理解:函數就是一個轉換步驟,把輸入表達的操作從編寫代碼的環節移到了程序啓動以後,相當於讓用戶來編寫表達代碼?
the name name to a particular string, and I can use it elsewhere.

In fact, a really good piece of code should be well-commented and therefore should be very readable by a user to understand where things are.

- 條件語句conditional

we use double equal sign for comparison, and use a single equal sign to reserved for assigning a name to a value.


循環——書寫簡單程序的能力,迭代,重複計算與多次執行





Use that iterative loop, and now that what we are doing is just walking through qenerating guesses for the cubed root of something until we find an answer that is close enough or beyond where I want it to be.
計算機的工作原理就是猜測,所以我們既需要用一個函數存放每次猜測的數值,也需要賦予一個初始值,表示我們從這個數字開始進行猜測.



- for循環


- float

整數的十進制數轉換爲二級制數,就是將每一位除以2,取餘數(1 or 0)作爲,取整再與2相除.
what about fraction?

python的設計者規定,0.1在計算機内部實際上并不代表0.1

we just cycling through this, cutting in half each time, the size of problem until I get to something that's close enough.

we saw when we wanted to move to floating point, we needed to be little more clever, and that led us to the idea of bisection as a smart way of reducing the computation at each stage, especially when we have functions or problems when there's a monotonic relationship between the value we're looking for and the guesses we're making.
- Newton - Raphson

What Newton showed was that for things like polynomials.
令f(x)=0,取泰勒多項式的前兩項作爲近似,也就是一階泰勒多項式,從而得到迭代公式:


循環計算機其抽象化
Looping computations and abstract them.
We'll capture the idea of a computational method inside a function, allowing us to separate the details of how we do the computation from the use of the computation.
儘管已經學習了如何使用計算機進行計算 ,我們的代碼仍然缺乏被稱之爲“抽象化”的東西.
idea of functions


但是我們不能重複使用x和y,因爲這樣有可能改變x和y的數值.

we'll see is that within that body, we're going to use the names of the formal parameters to identify the places, where we want to use the values associated with those parameters.

Inside of the parens here I've got 2 formal parameters, x and y.
and then the body is simply a description of the computations I want to do.

body can be any legal Python expressions.


ideal of environments

the form is so called environment, which gives us variable names and values as associations.
We are actually just keeping track in this environment of bindings of variables, looking them up when we need them, and re-changing them with assignment statements when we have to.
需要命名變量才能夠將其調用,這多少讓人想起給物品命就有用其使用權的異世界世界觀設定.


Python會自動使用局部變量,因此給參數使用何種命名,它們是否在其他地方綁定也並不重要.


use that loop, we'll print something up to tell us where we are, and we'll chage the value in result.


what happens inside of the binding:

we create the definition of f.
and in my global environment for Python, that says I've got a binding of f to the procedure.
the formal parameter is just there as text, it hasn't actually been evaluated yet.
The invocation of f says create a frame in which the formal parameter x is going to have a binding.
求求你説點我能聼懂的吧,我什麽都願意做......