python
type()
import inspect
print(inspect.getargspec(func)) //查方法参数
hasattr(list,'append')
isinstance(1,(int,float))
raise TypeError('')
_class_
_name_
_base_
_dict_
print '%s' % 'h'
@
strip() //去两边空格 strip('\r\n') 去换行符
lstrip()
rstrip()
赋值
一、序列赋值:
x,y,z = 1,2,3
我们可以看作:x = 1,y = 2,z = 3
二、链接赋值:
x = y = 1
print id(x)
print id(y)
大家可以看下,2个执行的结果是一样的。说明变量x和y都是存储了整数对象1的引用地址。
三、增量赋值。
比如x = x + 1 我们可以改成x+=1
运算符
(ob1 is ob2) 等价于 (id(ob1) == id(ob2))
异常
try:
...
except exception1:
...
except exception2:
...
except:
...
else:
...
finally:
...
try->异常->except->finally
try->无异常->else->finally
raise StopIteration()
函数
函数没返回值时:return None
空函数体:pass
默认参数:(参数值在函数定义阶段计算出来,参数值需非可变类型)def Test(a=1):
可变参数:
数组包裹:def Test(*a):
字典包裹:def Test(**a):
循环
range()
enumerate()
zip()
iter()
list()
yield
循环对象包含:next()
生成器表达式:x for x in range(10)
表推导:[x**2 for x in range(10)]
ta = [1,2,3]
tb = [9,8,7]
# cluster
zipped = zip(ta,tb)
print(zipped)
# decompose
na, nb = zip(*zipped)
print(na, nb)
lambda
map()
filter()
reduce()
func = lambda x,y: x + y
print func(3,4)
list
[].extend([]) //拼接数组
[].append()
[].count(a)
[].index(a)
[].insert(0,a)
[].remove(a)
[].pop()
[].sort()
len()
dictionary
{}.keys()
{}.values()
{}.items{}
{}.clear()
del a[b]
dic = {'lilei': 90, 'lily': 100, 'sam': 57, 'tom': 90}
for key in dic:
print dic[key]
file
f=open('a.txt','r')
f.read(N)
f.readline()
f.readlines()
f.write('')
f.close()
model
import a as b
from a import function1
文件夹 包 包含__init__.py 空文件
CLASS
__init__ 构造函数
类的属性:
class my(object):
aa='aa'
my.aa='class aa'
a=my()
b=my()
b.aa='aaa'
print a.aa
print b.aa
print my.aa
result:
class aa
aaa
class aa
动态属性(对象的属性):
class my(object):
def createAtt(self):
self.bb='bb'
a=my()
b=my()
a.createAtt()
print a.bb
print b.bb 报错)
dir()
help()

浙公网安备 33010602011771号