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

Syntax语法:what are legal expressions

“cat dog boy “

Static semantics 静态语义:which programs are meaningful

“ My desk is Suson“

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 "", line 1, in

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 :

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.2

Start

ans = 0

ans * ans <= x

ans = ans + 1

print ans

End

yes

no

x = 15

if(x/2)* 2 == x:

print 'Even'

else: print 'Odd'

Created with Raphaël 2.1.2

Start

(x/2)* 2 == x

print Even

print Odd

End

yes

no

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 "", line 1, in

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

麻省理工python公开课 pdf_MIT麻省理工学院公开课:计算机科学及编程导论 Python 笔记1-3...相关推荐

  1. 计算机科学与python编程导论_【基于Python】MIT OCW 计算机科学与编程导论

    [基于Python]MIT OCW 计算机科学与编程导论 (MIT Course Number 6.0001)Introduction to Computer Science and Programm ...

  2. 计算机编程导论python程序设计答案-学堂云_计算机科学与Python编程导论_作业课后答案...

    学堂云_计算机科学与Python编程导论_作业课后答案 答案: 更多相关问题 保本基金参与股指期货交易,应当根据风险管理的原则,以套期保值为目的.() 基金经理主要依据股票投资价值报告来决定实际的投资 ...

  3. 计算机编程导论python程序设计答案-学堂在线_计算机科学与Python编程导论_作业课后答案...

    学堂在线_计算机科学与Python编程导论_作业课后答案 答案: 更多相关问题 近代中国完全沦为半殖民地半封建社会的标志是:A.<马关条约>B.<辛丑条约>C.<凡尔赛和 ...

  4. python莱布尼茨法计算π_酷叮猫少儿编程讲堂——Python 用莱布尼茨等式求π

    原标题:酷叮猫少儿编程讲堂--Python 用莱布尼茨等式求π Python 用莱布尼茨等式求π 2018-08-01 德国大数学家莱布尼茨Leibniz在研究圆周率π的过程中发现一个数学公式是这样的 ...

  5. 麻省理工学院公开课:计算机科学及编程导论习题2

    习题1: 已知6a + 9b + 20c = n,当n = 50, 51, 52,53, 54, 55时,a.b.c有自然数解(我不知道现在是怎么定义的,但我以前学的时候自然数包括0), 如何求出n ...

  6. 麻省理工计算机都学啥,麻省理工学院计算机科学专业排名第1(2020年USNEWS美国排名)...

    麻省理工学院电气工程与计算机科学系研究生阶段开设有以下学位项目,分别是: 电气工程与计算机科学硕士:为期1年,仅面向该校电气工程与计算机科学系本科毕业生招生 电气工程与计算机科学哲学博士:为期4-7年 ...

  7. 【公开课】印度理工学院 - CMOS射频集成电路(L4)课堂笔记

    意味着如果我增加Q,我的电路响应变得越来越尖锐 Q=4000很难实现 解决方法:利用混频器将其中心频率转换到频率更低的基带上,这样就仅仅需要Q比较低的滤波器 瞬态响应时, 通过计算循环个数可以估算电路 ...

  8. 【公开课】印度理工学院 - CMOS射频集成电路(L3)课堂笔记

    低噪声放大器必须对极低的功率冲击敏感Q品质因数的定义:欧米伽*(存储在电路中的峰值能量/损耗的平均能量)只在谐振频率下有效检查方法:法一:在无限大频率下,阻抗为0,观察是否没有输出: 法二:将电容开路 ...

  9. 【公开课】印度理工学院 - CMOS射频集成电路(L2)课堂笔记

    对于不导电介质,例如大气,他的特性阻抗是 以上讨论没有考虑传输介质的损失 但是很好估算:需要知晓每公里损耗多少dB note:75欧姆电阻用于有线电视,用于各种电视系统:50欧姆电阻用于与有线电视无关 ...

  10. 【公开课】印度理工学院 - CMOS射频集成电路(L1)课堂笔记

    功率放大器(PA)的内阻相当于源内阻 PA与LNA的输出与输入阻抗要与天线的特性阻抗匹配 天线需要无损耗 波导在课程中的定义是指长线,根据长线效应定义所得.

最新文章

  1. 卷积神经网络(CNN)实现CIFAR100类别分类
  2. ExtendHelper
  3. 易格斯拖链选型手册_拖链相关知识
  4. 计算机操作系统思维导图_我在b站学计算机
  5. 将字符串中的大写字母变成小写字母
  6. linux云服务器状态上报解决方案:外发个人邮箱
  7. 給服务器增加swap空间缓解内存压力
  8. 计算机技术Control,聚变控制计算机 (Fusion Control Computer)
  9. 学习记录1——vissim4.3安装和vissim4.3时间修改工具使用
  10. php ssl证书安装,PHPWAMP如何开启SSL,Apache下如何安装ssl证书?配置ssl证书很简单...
  11. java dwg转pdf_CAD处理控件Aspose.CAD转换功能演示:使用Java将DWG和DXF文件转换为PDF...
  12. Linux下禁用笔记本触摸板
  13. 辅音字母组合功能音中的浊化现象
  14. [MAE]Masked Autoencoders掩膜自编码器
  15. 笔记本损耗60 计算机提示,笔记本电脑电池损耗,详细教您笔记本电脑电池损耗怎么修复...
  16. python自动排版公众号_请问微信公众号推文如何实现自动排版?
  17. 同学们上课,今天我们学习:UI 操作一定要在 UI 线程吗?
  18. snmp工具_运维超级好用工具大PK,你在用哪个?
  19. Stern-Brocot树 (生成0-1之间的所有真分数)
  20. 富文本生成pdf-java

热门文章

  1. 招商银行笔试题之员工考勤记录
  2. 基于android酒店点餐系统设计,基于Android的餐厅点餐系统的设计与实现
  3. 肌酸报告:17个肌酸使用常见问题解答
  4. position与清除浮动
  5. 赞美是朵花,赠人玫瑰,手有余香
  6. 2021-05-26--CHEN scary
  7. java实现token 过期,java – SQS ExpiredToken:请求中包含的安全令牌是过期状态码:403...
  8. 为什么需要等待2MSL
  9. linux 网络速度非常慢,Linux认证:解决ubuntu8.10上网速度慢的问题
  10. Error connecting to node kafka:9092 (id: 1001 rack: null)