MIT麻省理工学院公开课:计算机科学及编程导论 Python 笔记1-3

Lecture1:Goals of the course; what is computation; introduction to data types, operators, and variables

Python

  • High (√) VS. low
  • General (√) VS. targetted
  • Interpreted (√) VS. compile
    1. Syntax语法:what are legal expressions

      “cat dog boy “

    2. Static semantics 静态语义:which programs are meaningful

      “ My desk is Suson“

    3. Full semantics 完整语义:what does program mean

      what will happen when i run it

Operation

+ - * /

>>>‘a‘*3
‘aaa‘
>>>3/5
0
>>>3**3
27

Variables

>>> a = 3
>>> print a
3



Lecture2:Operators and operands; statements; branching, conditionals, and iteration

Operators and operands

>>> ‘ab‘ + ‘c‘
‘abc‘
>>> 3 + ‘c‘
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: ‘int‘ and ‘str‘
>>> str(3) + ‘c‘
‘3c‘
  • type conversion 类型转换 str(3)
  • type checking 类型检查 weak VS. strong typing

    TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

  • type discipline
  • operation precedence 算符优先

    * >> / >> + - when is doubt, use ()

>>> ‘a‘ < 3
False
>>> 4 + ‘3‘
True
>>> 9/5
1
>>> 9%5
4
>>> 3+4*5
23
  • Variables has a value--Assignment x = 3
  • type of Variables--get from value

    Dynamic types 动态类型

    x = ‘abc’

    don’t change types arbitrarily 不要反复无常的改变变量类型

  • Variables used any place it’s legal to use value

statements

  • statements = legal commands that Python can interpret
  • print, assignment

branching 分支

  • change the order of instructions based on some test (usually a value of a variable)
  • Syntax

    冒号colon : begin a sequence of instructions

    identifies a block of instructions.

冒号: start
carriage 回车 is end
x = 15
if(x/2)* 2 == x:
    print ‘Even‘
else: print ‘Odd‘


conditionals 条件

# if语句可嵌套
if <some test> :
    Block of instructions.
else:
    Block of instructions.
  • Boolean combination:AND, OR, or NOT


iteration 迭代 or loops 循环

# y = x的平方
y = 0
x = 3
itersLeft = x
while(itersLeft>0) :
    y = y + x
    itersLeft = itersLeft -1
print y



Lecture3:Common code patterns:iterative programs

iterative programs

  • choose a variable that’s going to “count“
  • initialize it outside of the loop 循环外初始化
  • set up the right end test (variable)正确结束循环
  • construct the block of code
    • changing the variable
  • what to do when done 循环结束后做什么

flow chart 流程图

# x开方
# find the square root of a perfect square
x = 16
ans = 0
while ans*ans <= x:
    ans = ans + 1
print ans

Created with Rapha?l 2.1.2Startans = 0ans * ans <= xans = ans + 1print ans Endyesno

x = 15
if(x/2)* 2 == x:
    print ‘Even‘
else: print ‘Odd‘

Created with Rapha?l 2.1.2Start(x/2)* 2 == xprint Evenprint Odd Endyesno



Simulate 模拟

ans x ans*ans
0 16 0
1 16 1
2 16 4
3 16 9
4 16 16
5 16 25


Defensive programming

  • A, if you’re getting inputs from a user, they won’t necessarily give you the input you’ve asked for 使用者可能没有按照你指定的输入。
  • B, if you’re using a piece of a program written by a programmer who is not perfect, perhaps yourself, there could be mistakes in that program, and so you write your program under the assumption that, not only might the user make a mistake, other parts of your program might make a mistake, and you just put in lots of different tests under the assumption that you’d rather catch that something has gone wrong, then have it go wrong and not know it. 程序中可能有错误。
# x开方进化版
# find the square root of a perfect square
# x = 1515361
ans = 0
if x > 0:
    while ans*ans < x:
        ans = ans + 1
        #print ‘ans =‘, ans
    if ans*ans != x:
        print x, ‘is not a perfect square‘
    else: print ans
else: print x, ‘is a negative number‘


Exhaustive enumeration 穷尽/枚举

  • try all “reasonable” values until you find the solution
# find x‘s divisor
# x = 10
# i = 1
    while i < x:
        if x%i == 0:
            print ‘divisor‘, i
        i = i+1
# find x‘s divisor
x = 10
for i in range(1, x):
    if x%i == 0:
        print ‘divisor‘, i


Tuple 元组

w3school Python元组

- ordered sequence of elements 有序的元素序列(immutable 不可变的)

>>>  foo = (1, 2, 3)
>>>> foo
(1, 2, 3)
>>> foo[0]
1
>>> foo[1]
2
>>> foo[10]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> foo[-1]
3
>>> foo[-2]
2
>>> foo[1:3]
(2, 3)
>>> foo[1:2]
(2,)
>>> foo[:2]
(1, 2)
>>> foo[1:]
(2, 3)
# find x‘s divisor
x = 100
divisors = ()
for i in range(1, x):
    if x%i == 0:
        divisors = divisors + (i,)
print divisors


String

w3school Python字符串

- support selection, slicing, and a set of other parameters, other properties

sumDigits = 100
for c in str(1952):
    sumDigits += int(c)
print sumDigits
时间: 2024-10-11 23:29:17

MIT麻省理工学院公开课:计算机科学及编程导论 Python 笔记1-3的相关文章

MIT公开课:计算机科学及编程导论 Python 笔记5 浮点数,逐次逼近法和二分法

Lecture5: Floating point number , successive refinement, finding roots 浮点数和二分法 3wschool 数字 >>> a = 2 ** 1000 >>> a 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251

MIT公开课:计算机科学及编程导论 Python 笔记4 函数分解抽象与递归

Lecture4:Decomposition and abstraction through functions:introduction to recursion 函数分解抽象与递归 Functions 函数 block up into modules 分解为模块 suppress detail 忽略细节 create "new primitives" 创建原语的思考方式 w3school Python函数 #example code for finding square roots

麻省理工学院公开课-第一讲:算法分析

http://www.cnblogs.com/banli/archive/2013/05/19/3087486.html http://www.cnblogs.com/diliwang/p/3352946.html 自己再梳理一下,便于记忆: 1.插入排序(Insertion Sort) 问题描述:输入一个数组A[1,2....n],输出一个按升序排列的数组A'. 算法分析理论: 通常,我们寻找算法运行的最大时间(上界)T(n),因为最坏的情况是一个承诺,一个保证. 因为,算法的运行时间依赖于我

麻省理工学院公开课-第二讲:渐进符号、递归及解法

http://blog.csdn.net/julius819/article/details/8267060 http://www.cnblogs.com/banli/archive/2013/05/21/3089900.html

计算机科学及编程导论学习笔记

不要频繁变换变量的类型 多用有意义的通俗易懂的变量名 python的raw_input的值都默认为字符串 “=”是用来赋值的,将左边的名字绑定到右边的值上去.因此比较的时候要用“==” if <测试语句> :

麻省理工公开课:线性代数 第6课 列空间和零空间

参考资料: 网易公开课:http://open.163.com/special/opencourse/daishu.html 麻省理工公开课:线性代数 一.向量空间和子空间(加法封闭.数乘封闭) 向量空间$R^3$的子空间:$R^3$.任意经过原点$(0, 0, 0)$的平面$P$和直线$L$.只包含零向量的空间$Z$ 并集:$P\bigcup L$是子空间吗? //否!对加法运算不封闭 交集:$P\bigcap L$是子空间吗? //是! 结论:任意子空间的交集仍然是子空间 二.矩阵列空间 $

麻省理工公开课:线性代数 第7课 求解Ax=0:主变量、特解

参考资料: 网易公开课:http://open.163.com/special/opencourse/daishu.html 麻省理工公开课:线性代数 教材:Introduction to Linear Algebra, 4th edition  by Gilbert Strang 链接:https://pan.baidu.com/s/1bvC85jbtOVdVdw8gYMpPZg 提取码:s9bl 假设:$A$为$3\times 4$长方形矩阵(线性相关),求解$A\mathbf{x}=0$

麻省理工公开课:线性代数 第8课 求解Ax=b:可解性和解的结构

参考资料: 网易公开课:http://open.163.com/special/opencourse/daishu.html 麻省理工公开课:线性代数 教材:Introduction to Linear Algebra, 4th edition  by Gilbert Strang 链接:https://pan.baidu.com/s/1bvC85jbtOVdVdw8gYMpPZg 提取码:s9bl 假设:$A$为$3\times 4$长方形矩阵(线性相关),求解$A\mathbf{x}=\ma

麻省理工公开课:线性代数 第10课 四个基本子空间

参考资料: 网易公开课:http://open.163.com/special/opencourse/daishu.html 麻省理工公开课:线性代数 教材:Introduction to Linear Algebra, 4th edition  by Gilbert Strang 链接:https://pan.baidu.com/s/1bvC85jbtOVdVdw8gYMpPZg 提取码:s9bl 假设:$m\times n$矩阵$A$ 一.矩阵$A$的列空间:$C(A)$ (1)是$R^m$