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
>>> 95
1
>>> 95
4
>>> 345
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<

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值