python学习笔记

0. Python学习参考书籍

《简明Python教程》

《Python学习手册》

《Python高级编程》

  Python官网: http://www.python.org/

 

 

1. Python的基本数据类型

 

数 test = 3.14
字符串str test = "hello world"
列表list shoplist = ['apple', 'mango', 'carrot', 'banana']
字典 dict ab = {'hw':'hello world'}
元组tuple zoo = ('wolf','elephant','penguin')

 

2. 一些基本知识点

  Python不需要变量的声明

单引号、双引号、三引号(里面的可以有换行,可以有单双引号)
函数的关键参数
python表达式中的赋值都是同时的,比如 a, b = b, a就实现了数值交换
DocStrings(文档字符串)
True False None

3. 操作技巧 skill

1) squares = [x**2 for x in range(10)]

2) squares = [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]

3) matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
matrix = [[row[i] for row in matrix] for i in range(4)]
print(matrix)

4) >>> lists = [[]] * 3 #shallow copy
>>> lists
[[], [], []]
>>> lists[0].append(3)
>>> lists
[[3], [3], [3]]

5) >>> lists = [[] for i in range(3)] # right way!! not shallow copy
>>> lists[0].append(3)
>>> lists[1].append(5)
>>> lists[2].append(7)
>>> lists
[[3], [5], [7]]

6) x = [2,3,4,5]
y = x
x.append(6)
print x,y

x = [2,3,4,5]
y = x[:]
x.append(6)
print x,y

4. 比较好的知识点

1) 列表 解析

squared= [x ** 2 for x in range(8) if no x % 2]
2) 函数 默认参数和关键参数
3) 判断两个对象是否指向同一个地址
a is b
id(a) == id(b)
4) 一些内建的函数
cmp(obj1, obj2)
repr(obj) 或者 `obj`
str(obj) int(obj)
type(obj)
eval()
isinstance()
id()
abs(num) coerce(num1,num2) divmod(num1,num2) pow(num1,num2,mod=1) round(num,ndig=0)
hex(num) oct(num) chr(num) ord(chr) unichr(num)
5) //
取整除,永远返回小于等于商的最接近的整数
6) 列表的切片操作
[] [:] [::]
7) copy模块
copy() deepcopy()
8) 字典的构建方法
adict = {}.fromkeys(('x','y'),-1) #如果值没有给出,则默认为
9) 字典的特殊输出格式,简单
adict = {'name':'baidu' , 'port':8080}
print 'host %(name)s is running on port %(port)d' % adict
10) 内置有用函数
apply filter map reduce iter
11) 三元表达式 和 列表解析表达式
12)
字典 默认迭代的是  key
文件 默认迭代的是  line
13) 用delegate()来实现类的代理工作

posted @ 2012-08-30 15:39  hanyuanbo  阅读(200)  评论(0编辑  收藏  举报