Lecture1:Goals of the course; what is computation; introduction to data types, operators, and variables
Python
High (√) VS. lowGeneral (√) 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/50>>>3**327
Variables
a3Lecture2:Operators and operands; statements; branching, conditionals, and iteration
Operators and operands
+ ‘c’Traceback (most recent call last): File “<stdin>”, line 1, in <module>TypeError: unsupported operand type(s) str(3) + ‘c”3c’type conversion 类型转换 str(3) type checking 类型检查 weak VS. strong typing TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’type disciplineoperation precedence 算符优先 * >> / >> + – when is doubt,, use ()+ /%+4*523Variables 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 interpretprint, 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.冒号: startcarriage 回车 is endx = 15if(x/2)* 2 == x:: 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 = 0x = 3itersLeft = xwhile(itersLeft>0) :y = y + xitersLeft = itersLeft -1print yLecture3:Common code patterns:iterative programs
iterative programs
construct the block of code changing the variablewhat to do when done 循环结束后做什么
flow chart 流程图
# x开方# find the square root of a perfect squarex = 16ans = 0while ans*ans <= x:ans = ans + 1print ans
Created with Raphal 2.1.2Startans = 0yesno
x = 15if(x/2)* 2 == x:: print ‘Odd’
Created with Raphal 2.1.2Startyesno
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. 程序中可能有错误。ans = 0if x > 0:while ans*ans < x:ans = ans + ans*ans != x:print x, ‘is not a perfect square’else: print anselse: print x, ‘is a negative number’
Exhaustive enumeration 穷尽/枚举
try all “reasonable” values until you find the solutioni < x:if x%i == 0:print ‘divisor’, ii = i+1# find x’s divisorx = 10for 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 divisorx = 100divisors = ()for i in range(1, x):if x%i == 0:divisors = divisors + (i,)print divisors突然之间失去了语言。那才是真正的寂寞,