生命苦短,我用 pythonpython
Python是一種功能強大的語言,有着功能豐富的第三方包,推崇功能齊全的理念,被普遍用於多種場景下。算法
*nix系統默認的已經內置了一個python,可經過 python -V
檢測:數組
# 經過python腳原本編譯一個源碼文件 import py_compile py_compile.compile('hello.py')
# python解釋器輸出優化代碼 python -O -m py_complie hello.py
__init__.py
文件(必選) 和 多個模塊文件組成第一行註釋(組織行)用於指定解釋器,第二行註釋用於指定編碼從而中文支持bash
#!/usr/bin/python # -*- coding: utf-8 -*- import os, sys help('類名') #模塊都可經過help函數幫助文檔 ## 控制語句,python沒有switch ## input1 = int(input('請鍵入數字')) if input1 == 1: print 'a'; elif input1 == 2: print 'b'; else del input1 #刪除變量 while int(input('請輸入小於4的數字')) < 4: print 'OK'; else: print '輸入錯誤'; for x in range(0, 24 , 1): print x ## 字符串 ## str1 = 'hello world' str1.startswith('hello') 'hello' in str1 str1.find('hello') ## python的三種序列類型 ## list1 = ['student', 'teacher'] # list列表,可變類型,傳統意義的數組 tuple1 = ('student', 'teacher') #tuple元組,不可變類型,單個元素的元組必須跟一個逗號以區分 dict1 = {'role1':'student', 'role2':'teacher'} # 字典,鍵值對 #列表操做 ','.join(list1) list2 = list1[:] #一份全量拷貝,非refer list1.append('xxx') #元組操做 print '%s and %s' % tuple1 #格式化輸出 #字典操做 for role, title in dict1.items(): print 'xxx' if 'role1' in dict1: print 'xxx' if dict1.has_key('role1'): print 'xxx' ## 生成器Generator ## * 將迭代數據轉爲數據算法:每次迭代,臨時掛起循環,彈出一個值當即進入使用,使用結束繼續下一次迭代 * 生成器使用循環來使用其數據,`for`循環內部經過`next()`方法機制得到生成器中下一個值 * 建立Generator: - 簡單Generator基於列表生成式改造: var1 = (x * x for x in range(100)) - 複雜Generator可用`yield` 關鍵字函數 ## 函數 ## # 函數不明確指定,則默認 `return None` # 能夠從函數的第一行開始定義docstring文檔字符串,多行字符串形式,首行大寫字母開始句號結尾,第二行空行,第三行描述,經過 `__doc__` 來調用docstring def welcome(name, from, to='usa'): print 'welcome ', name, ' from ', from, ' to ', to; welcome(name='tom', from='china') #關鍵參數式調用 # *前綴多餘函數參數會做爲元組存儲在args; **前綴多餘函數參數會做爲字典存儲在args def powersum(arg1, *args): print args; ## 模塊 ## #import導入的對象將從`sys.path`中搜索相應的`.py`模塊,並運行其主塊的語句 import 模塊 #單一的導入模塊, 可讀性好,同時沒有名字衝突 from 模塊 import 符號表 # 這種導入形式是不推薦 import sys sys.argv #命令行參數列表,第一個元素爲腳本名稱,其後元素爲傳入參數 sys.path #模塊的搜索路徑列表(同PYTHONPATH環境變量),第一個空字符串元素表明當前目錄 #__name__常量能得到當前模塊名,當模塊被直接python解釋執行而非導入時,模塊名設爲 '__main__' if __name__ == '__main__': print 'current is in main module' dir('sys') #dir函數返回sys模塊的符號表,不指定模塊則返回當前模塊的信息 # 注意事項 * 模塊私有變量和函數約定加_、 __ 下劃線前綴 * 模塊搜索路徑(sys.path變量):當前目錄、內置模塊、第三方模塊 * 增長模塊搜索路徑 - `sys.path.append(xxx)` - 修改`PYTHONPATH`環境變量 ## 面向對象 ## #對象全部成員都是公共的 # 特例:__打頭的變量是實際私有的,_打頭的變量是慣例上認爲的私有 class Foo count = 1 #類變量 def __init__(self, name): self.name = name #對象變量 def welcome(self, name): print 'hi, ' + name #繼承 class Foomore(Foo) def say(self, words) print words foomore = Foomore('Tom') foomore.welcome() ## 文件處理 ## file1 = file('test.dat', 'w') file1.write('xxxx') while True: line = fiel1.readline() if len(line) == 0: break; #當行語句 file1.close() ## 異常 ## import sys try: s = input('請輸入') except EOFError, e: print '程序退出' sys.exit() except: print '程序有異常' ## 列表綜合 ## listone = [2, 3, 4] listtwo = [2*i for i in listone if i > 2] #listone中全部大於2的數都是原來的2倍的列表 ## lambda ## #lambda語句用來建立新的函數對象 def make_repeater(n): return lambda s: s*n #lambda須要一個參數,後面僅跟單個表達式做爲函數體 twice = make_repeater(2) print twice('word') #word做爲s參數傳入lambda
id()
函數獲取變量標識來驗證type
類來查看具體類型global a, b, c
u'中'和u'\u4e2d'
表示了同一個數據u'中文'.encode('utf-8') => '\xe4\xb8\xad\xe6\x96\x87'
程序加斷點app
import pdb pdb.set_trace()
調試命令函數
l # 查看周圍代碼 n # 單步執行 p # 打印變量 c # 繼續執行 q # 退出
sublime下python提示插件推薦用Anaconda優化