<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] > * python不用花括號表示同一個代碼塊,用縮進對齊的方式,表示同一個代碼塊 ## 1. hello world > * 執行Python腳本有兩種方法 > 1. python <腳本> > 2. 像執行shell腳本一樣執行 1. python <腳本> ~~~ print("hello world") if True: print ("True") else: print ("False") ~~~ ![](https://box.kancloud.cn/08c2b3d3a1e1e7934b663c09fd71e792_588x63.png) 2. 像執行shell腳本一樣執行 > 1. 腳本開頭加入解析器 > 2. 腳本具有可執行權限 ` vim sys.py` ~~~ #!/usr/bin/python3 # 加入解析器 import sys print('================Python import mode=========================='); print ('命令行參數為:') for i in sys.argv: print (i) print ('\n python 路徑為',sys.path) ~~~ ~~~ chmod +x sys.py ./sys.py # 執行腳本 ~~~ ![](https://box.kancloud.cn/73b53a067e2944c717e64dd34176bba4_958x201.png) * * * * * ## 2. 注釋 > 確保對模塊, 函數, 方法和行內注釋使用正確的風格 > Python中的注釋有單行注釋和多行注釋: > Python中單行注釋以 # 開頭,例如:: ~~~ # 這是一個注釋 print("Hello, World!") ~~~ > 多行注釋用三個單引號 ''' 或者三個雙引號 """ 將注釋括起來,例如: 1. 單引號(''') ~~~ #!/usr/bin/python3 ''' 這是多行注釋,用三個單引號 這是多行注釋,用三個單引號 這是多行注釋,用三個單引號 ''' print("Hello, World!") ~~~ 2. 雙引號(""") ~~~ #!/usr/bin/python3 """ 這是多行注釋,用三個單引號 這是多行注釋,用三個單引號 這是多行注釋,用三個單引號 """ ~~~ `print("Hello, World!") ` * * * * * ## 3. 迭代器 1. for 循壞迭代 ~~~ #!/usr/bin/python3 list=[1,2,3,4] it = iter(list) # 創建迭代器對象 for x in it: print (x, end=" ") ~~~ 2. while 循環迭代 ~~~ #!/usr/bin/python3 import sys # 引入 sys 模塊 list=[1,2,3,4] it = iter(list) # 創建迭代器對象 while True: try: print (next(it)) except StopIteration: sys.exit() ~~~ * * * * * ## 4. yield >1 . 在 Python 中,使用了 yield 的函數被稱為生成器(generator)。 > 2. 跟普通函數不同的是,生成器是一個返回迭代器的函數,只能用于迭代操作,更簡單點理解生成器就是一個迭代器。 >3. 在調用生成器運行的過程中,每次遇到 yield 時函數會暫停并保存當前所有的運行信息,返回yield的值。并在下一次執行 next()方法時從當前位置繼續運行。 以下實例使用 yield 實現斐波那契數列: ~~~ #!/usr/bin/python3 import sys def fibonacci(n): # 生成器函數 - 斐波那契 a, b, counter = 0, 1, 0 while True: if (counter > n): return yield a a, b = b, a + b counter += 1 f = fibonacci(10) # f 是一個迭代器,由生成器返回生成 while True: try: print (next(f), end=" ") except StopIteration: sys.exit() ~~~ 打印結果如下 0 1 1 2 3 5 8 13 21 34 55 ## 5. 函數 > 你可以定義一個由自己想要功能的函數,以下是簡單的規則: > 1. 函數代碼塊以 def 關鍵詞開頭,后接函數標識符名稱和圓括號 ()。 > 2. 任何傳入參數和自變量必須放在圓括號中間,圓括號之間可以用于定義參數。 > 3. 函數的第一行語句可以選擇性地使用文檔字符串—用于存放函數說明。 > 3. 函數內容以冒號起始,并且縮進。 > 4. return [表達式] 結束函數,選擇性地返回一個值給調用方。不帶表達式的return相當于返回 None。 ~~~ def 函數名(參數列表): 函數體 # 對齊表示在一個代碼塊 ~~~ ## 6. 格式化字符串 ### 6.1 str.format 1. 位置格式,從零開始,按照順序輸出format的內容 ~~~ >>> '{1},{0},{1}'.format('kzc',18) '18,kzc,18' >>> '{1},{0},{0}'.format('kzc',18) '18,kzc,kzc' >>> ~~~ 2. 關鍵字參數,通過參數輸出format的內容 ~~~ >>> '{name},{age}'.format(age=18,name='kzc') 'kzc,18' ~~~ 3. 通過索引(list) > 0[1],表示format中第一個數據,[1]表示下標 ~~~ >>> p=['kzc',18] >>> '{0[0]},{0[1]}'.format(p) 'kzc,18' >>> '{0[1]},{0[0]}'.format(p) '18,kzc ~~~ 4. 精度 ~~~ >>> '{0:.3f}'.format(321.33345) # 0表示format的第一個數據(0默認可不寫),.3f表示浮點數格式,并保留三個小數 '321.333' >>> '{:.2f}'.format(321.33345) '321.33' ~~~ * * * * * #### 6.2 % `print("I'm %s. I'm %d year old" % ('Vamei', 99))` ## 7. 裝飾器 > * 被裝飾函數指向被裝飾后的函數,不在指向 > * 代碼遵循開放封閉原則,裝飾器可以在不修改源代碼的前提下,對功能進行修改 ![](https://box.kancloud.cn/aa20b059af7f9a2e97ecc684452abf22_679x347.png) ### 7.1 return ~~~ def decorate(func): #1.因為是被裝飾的,會把這個test函數傳入給裝飾器,func指向test()函數 def inner(): return func() # 返回一下被裝飾函數的返回值,不管他有沒有返回值,這樣比較通用 return inner # 2.裝飾器返回一個被裝飾的函數的引用給test @decorate def test(): return "test value" print(type(test)) # test表示一個函數,test()表示函數調用 print(test()) # 3.此時test指向inner()函數` ~~~ ![](https://box.kancloud.cn/5a276bf25496a75d11117bde96308e5b_602x75.png) ### 7.2 裝飾器參數 #### 1. 定參 ~~~ from time import ctime,sleep def w1(func): def inner(a,b): print("%s called at %s" %(func.__name__,ctime())) print(a,b) func(a,b) return inner def w2(func): def inner(): @w1 def foo(a,b): print(a,b) foo(3,5) ~~~ #### 2. 變參 ~~~ def decorate(func): def inner(*x): for i in x: print(i) return inner @decorate def test(*a): # 使用元組類型參數 for i in a: print(i) test(123,32,'d','k') def decorate1(func): def inner(**x): # 字典類型參數 for i in x.keys(): print(i,x[i]) return inner @decorate1 def test1(**a): for i in a: print(i) test1(a="dailin",b="chenhuan") ~~~ ## 8. random 1. uniform(10,20)隨機生成區間小數 ~~~ In [2]: import random In [3]: random.uniform(10,20) Out[3]: 18.093162205505266 In [4]: random.uniform(10,20) Out[4]: 13.232674331833612 ~~~ 2. 生成區間整數 ~~~ In [5]: random.randint(10,20) Out[5]: 18 ~~~ 3. 隨機選取字符 ~~~ In [9]: random.choice('sbae') Out[9]: 's' In [10]: random.choice('sbae') Out[10]: 'b' ~~~ 4. 隨機抽取 ~~~ In [13]: random.sample('sbae',2) Out[13]: ['s', 'a'] ~~~ ## 9. 隊列 ~~~ # Python3.X from queue import Queue # Python2.X from Queue import Queue ~~~ ## 10. 元類 元類就是?來創建這些類(對象) 的, 元類就是類的類, 我們由元類創建類(對象),再由類(實例)創建實例對象 > 1. 在大多數編程語言中,類就是用來描述如何生成對象的代碼段,python中任然成立. > 2. 類(用class關鍵字定義的代碼段)在python中也是對象,python解釋器在遇到class關鍵字時就會創建一個對象 ~~~ >>> class ObjectCreator(object): … pass ~~~ 會在內存中,生成一個對象名字為ObjectCreator,這個對象具有生成對象的能力,對元類可以: > 1. 你可以將它賦值給?個變量 > 2. 你可以拷貝它 > 3. 你可以為它增加屬性 > 4. 你可以將它作為函數參數進?傳遞 ~~~ In [2]: class test: ...: pass ...: In [3]: print(test) <class '__main__.test'> # test元類 In [4]: print(type(test)) <class 'type'> In [7]: print(hasattr(test,'t')) # 判斷是否有某些屬性 False In [8]: t = test() In [9]: print(t) <__main__.test object at 0x0000000004D54C18> In [10]: print(type(t)) <class '__main__.test'> # 他的類型是test元類 ~~~ ## 11. 安裝pip3工具 ~~~ sudo apt-get -y install python3-pip ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看