>>> list = range(100) >>> print(list) range(0, 100) >>> print(type(list)) <class 'range'> >>>
>>> for i in range(100): ... sum = sum + i + 1 ... >>> print(sum) 5050
>>> def f(a,b,c): ... return a+b+c ... >>> print(f(1,2,3)); 6
>>> def f(a,b,c): ... return a+b+c ... >>> print(f(a=1,b=2,c=3)); 6
>>> def f1(a,b,c=10): ... return a+b+c; ... >>> print(f1(1,2)) 13 >>> print(f1(5,2)) 17 >>> print(f1(2,4,5)) 11 >>> print(f1(b=6)) 17
>>> def func3(a,b,c): ... print(a,b,c) ... >>> a = (1,3,5) >>> func3(*a) 1 3 5
>>> def func3(a,b,c): ... print(a,b,c) ... >>> b = {"a":"10","b":"20","c":"30"} >>> func3(**b) 10 20 30
lambda a, b : a + b lambda 參數 : 方法體
>>> func = lambda a, b : a + b >>> def sum(f,a,b): ... print("--- the sum function ---") ... print(f(a,b)) ... >>> >>> sum(func,10,20) --- the sum function --- 30 >>>
>>> b = {"a":"1234"} >>> >>> dir(b) ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__' , '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', ' __lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__seta ttr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'co py', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update ', 'values']
>>> list = [1,2,3,4]; >>> i = iter(list) >>> i.__next__() 1 >>> i.__next__() 2 >>> i.__next__() 3 >>> i.__next__() 4 >>> i.__next__() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>>
>>> num_list = [1,2,3,4,5,6,7,8] >>> func = lambda x : x*x >>> num_map = map(func,num_list) >>> print(list(num_map)) [1, 4, 9, 16, 25, 36, 49, 64]
>>> str_list = ['adam', 'LISA', 'barT']; >>> func = lambda x : x[0:1].upper() + x[1:].lower(); >>> str_map = map(func,str_list); >>> print(list(str_map)); ['Adam', 'Lisa', 'Bart']
>>> str_list = ['test', None, '', 'str', ' ', 'END'] >>> def is_not_empty(s): ... return s and len(s.strip()); # and前的s用來排除None ... >>> rs = filter(is_not_empty,str_list) >>> print(list(rs)) ['test', 'str', 'END']
>>> from functools import reduce
>>> from functools import reduce >>> num_list = [2, 4, 5, 7, 12] >>> func = lambda x,y : x*y >>> rs = reduce(func,num_list) >>> print(rs) 3360
>>> from functools import reduce >>> num_list = [2, 4, 5, 7, 12] >>> func = lambda x,y : x*y >>> rs = reduce(func,num_list,100) >>> print(rs) 336000
__private_name = 1; def __private_func(self,name): print(name);
class ClassName():
# 先安裝setuptools https://pypi.python.org/pypi/setuptools # 後安裝pip https://pypi.python.org/pypi/pip
# 編譯(若是下載的是源碼) python setup.py build # 安裝(全部python的模塊都適用) python setup.py install # 安裝模塊(安裝玩pip後) pip install xxx
import module_name; import module_name as mn; from module_package import module_name; from module_package import *;
模塊搜索路徑: 1、程序所在的位置 2、標準庫的安裝路徑 3、操做系統環境變量PYTHONPATH指向的位置 可是若是咱們的模塊不在上述位置時,須要在環境變量中加入以下路徑: export PYTHONPATH=$PYTHONPATH:{module_path}
• 正則表達式 (re包)
• 時間與日期 (time, datetime包)
• 路徑與文件 (os.path包, glob包)
• 文件管理 (部分os包,shutil包)
• 存儲對象 (pickle包,cPickle包)
• 子進程 (subprocess包)
• 信號 (signal包)
• 線程同步 (threading包)
• 進程信息 (部分os包)
• 多進程初步 (multiprocessing包)
• 數學 (math包)
• 隨機數(random包)
• 循環器 (itertools)
• 數據庫 (sqlite3)
• …
• [官方文檔] • Python 2: • https://docs.python.org/2/library • Python 3: • https://docs.python.org/3/library/index.html