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
- 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
x = 15
if(x/2)* 2 == x:
print 'Even'
else: print 'Odd'
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