Python筆記---------------[]python
列表解析web
>>> [(x,y) for x in range(3) for y in range(5)]app
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4)]函數
生成器表達式(每次只加載一個元素到內存裏)spa
返回可迭代對象orm
多重繼承對象
(新式類採用廣度優先,經典類採用深度優先)繼承
s = (1,2,[2,3])ip
i = s#前拷貝id(i) == id(s) s的改變會影響i內存
i = copy.deepcopy(s) 深拷貝
>>> d = [(x,y,z,a,v,c) for x in range(9) for y in range(9) for z in range(9) for a in range(9) for v in range(9) for c in range(9)] 暴力破解
Isinstance(s, str)判斷s這個對象是否是’str’object
For i in range(10:
If i == 2:
Continue
Print i
Class Girl(human):
Def __init__(self, name, old):
Print ‘We are People’, name
Switch = {
‘-’:a-b,
‘+’:a+b
}
Print Switch.get(c,’Please input +-*/’) #用字典的get這樣不報錯
>>> hello[:3:-1]
'dlrow o'
>>> hello[3:-1]
'lo worl'
>>> hello[4:]
'o world'
>>>
S = 'abcdefghijk'
for i in range(0,len(S),2):
print S[i] #小技巧
--------------------------------------------
L1 = [1,2,3,3]
L2 = [1, 2, 3, 4]
返回L1中的數值這個數值必須是L2中沒有的。
Set(L1).difference(L2) --------------------------------------------------------
利用enumerate()函數,能夠在每次循環中同時獲得下標和元素:
S = 'abcdefghijk'
for (index,char) in enumerate(S): print index print char
實際上,enumerate()在每次循環中,返回的是一個包含兩個元素的定值表(tuple),兩個元素分別賦予index和char。
with open(file) as myfile:
Print myfile.read() #不用再關閉myfile
------------------------------------------------
class num(object):
def __init__(self, value):
self.value = value
def getNeg(self):
return -self.value
def setNeg(self, value):
self.value = -value
def delNeg(self):
print("value also deleted")
del self.value
neg = property(getNeg, setNeg, delNeg, "I'm negative")
x = num(1.1)
print(x.neg) x.neg = -22
print(x.value)
print(num.neg.__doc__)
del x.neg
裝飾器能夠對一個函數、方法或者類進行加工。
def decorator(F):
def new_F(a, b):
print("input", a, b)
return F(a, b)
return new_F
# get square sum
@decorator
def square_sum(a, b):
return a**2 + b**2 #square_sum = decorator(square_sum)
Def func(formal_args, i = 0, *args, **kwargs):
print formal_args
print i
print args
print kwargs
以上是不帶參數的裝飾器
def Func1(F):
def new_fun():
print('func1')
F()
return new_fun
def Func21(F):
def new_fun2():
print('func2')
F()
return new_fun2
@Func1
@Func21
def Func4():
pass
print(0b1110) # 二進制,以0b開頭
print(0o10) # 八進制,以0o開頭
print(0x2A) # 十六進制,以0x開頭
環境配置$export PYTHONPATH=$PYTHONPATH:/home/vamei/mylib
Python的標準庫隨着Python一塊兒安裝。當咱們須要非標準包時,就要先安裝。
這是安裝Python附加包的一個好的起點。你能夠在Linux repository中查找可能存在的Python包 (好比在Ubuntu Software Center中搜索matplot)。
好比使用以下方法來安裝、卸載或者升級web.py:
$pip install -i http://mirrors.aliyuncs.com/pypi/simple web.py$pip uninstall web.py$pip install -i http://mirrors.aliyuncs.com/pypi/simple --upgrade web.py
若是你的Python安裝在一個非標準的路徑(使用$which python來確認python可執行文件的路徑)中,好比/home/vamei/util/python/bin中,你能夠使用下面方法設置pip的安裝包的路徑:
$pip install -i http://mirrors.aliyuncs.com/pypi/simple --install-option="--prefix=/home/vamei/util/" web.py
str1 = ['21+23=', '32/8=', '32*12=']
e=[]
for s in str1:
s2 = s + str(eval(s[:-1]))
e.append(s2)
print(e)