think python内容提纲
python 2.7 document
https://docs.python.org/2.7/library/index.html
chapter1
python is an interpreted language. it is executed by an interpreter. there are two ways to use the interpreter: interactive mode (similar to MATLAB command window) and script mode
chapter2
- types of values: int, float, str, bool, list (use […,…]), tuple (use (…,…)), dict (use {…:…,…:…})
- octal format: start with 0 such as 0108
hexadecimal format: start with 0x such as 0x1016 - keywords:
[‘and’, ‘as’, ‘assert’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘exec’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘not’, ‘or’, ‘pass’, ‘print’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’]
you can get keywords list by inputing the following commands:
import keyword
keyword.kwlist
keyword.iskeyword(s) #Return true if s is a Python keyword. - operators:
+ addition (‘ha’+‘lol’‘halol’)
- subtraction
* multiplication (‘ha’*3’hahaha’)
/ floor division in python2 and division in python3 (floor division: 5/60; -1/6-1)
** exponentiation [ˌekspəʊˌnenʃɪ’eɪʃən] 求幂
^ XOR - comments start with the # symbol
chapter3
swampy下载地址:
http://www.greenteapress.com/thinkpython/swampy/
- type conversion functions:
int(‘32’)
int(3.999)
float(‘3.14’)
float(32)
str(32)
str(3.14) - math functions
import math
math.log10(100)==2.0
math.log(math.e)==1.0
math.sin(math.pi/6)==0.49999999999999994
math.sqrt(2)==1.4142135623730951
math.exp(math.log(3))==3.0000000000000004
…… - variables and parmeters are local
- function: fruitful functions (chapter6) and void functions
- function definition
def function_name(parameter1,parameter2,……)
function body - importing with from
(1) import math
math.pi
(2) from math import pi
pi
(3) from math import * (there might be conflicts between names defined in different modules)
pi
sin(pi/2)
chapter4 function interface design
-
encapsulation 函数封装 (lines of commands -> function)
generalization 函数泛化(square -> polygon, 从而circle可以调用polygon)
refactoring: rearranging a program to improve function interfaces and facilitate code reuse -
for-looping
>>> print range(4)
[0, 1, 2, 3]
>>> sum=0
>>> for i in range(4):