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
- Syntax语法:what are legal expressions
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<