【2017.04.20】
List Comprehension
Notes of Python Tutorial:
A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it. For example, this listcomp combines the elements of two lists if they are not equal:
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
and it’s equivalent to:
>>> combs = []
>>> for x in [1,2,3]:
... for y in [3,1,4]:
... if x != y:
... combs.append((x, y))
...
>>> combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
Python 對象
is(), id(), isinstance()
type, class的統(tǒng)一龄章;
is None 的真實(shí)含義
Slicing
兩個(gè)索引參數(shù)構(gòu)成先閉后開區(qū)間:
a = '12345'
>>> a[0:2]
'12'
Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.
>>> a[:2]
'12'
>>> a[1:]
'2345'
>>> a[:]
'12345'
slicing還支持第三個(gè)參數(shù) 'step' 表示步進(jìn):
>>> a[::1]
'12345'
>>> a[::2]
'135'
>>> a[::3]
'14'
>>> a[::4]
'15'
>>> a[::-1] 用這個(gè)方法可以實(shí)現(xiàn)list反轉(zhuǎn)
'54321'
>>> a[::-2]
'531'
【2017.05.02】
Division
Python 3.x 之后
- real division: /
- floor division: //
TODO 字符編碼/Unicode的知識一直沒有搞清楚
三目運(yùn)算
X if C else Y
Parameter Group
func(*tuple_grp_nonkw_args, **dict_grp_kw_args)
異常體系
- BaseException:
- Exception
- KeyboardException
- SystemExit
【2017.05.03】
裝飾器(Decorator)
本質(zhì)上decorator就是一個(gè)函數(shù)恼除;與被裝飾函數(shù)組成復(fù)合函數(shù)曹体;
Python Language Refference & PEP
一些更深入的萎河、與語言設(shè)計(jì)相關(guān)的內(nèi)容
reduce
reduce(func, [1, 2, 3]) === reduce(func(1, 2), 3)
Generator
yield 與 coroutine的關(guān)系堂鲜??
import
examples | suggest |
---|---|
import sys | good |
import sys, os, json | bad |
from module import name1[,name2] | OK |
from module import name1 | good |
from Tkinter import * | bad |
from cgi import FieldStorage as form | good |
Mutiple Inheritance
【20170517】
需要找另一個(gè)本書看下趁餐,
core p p 這本書整體講的太啰嗦俐芯。
learning python 講的可能好一點(diǎn)。
Classes and OOP
TODO 閱讀 <Learning Python>