<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之旅 廣告
                ## 函數參數(二) ### 1\. 缺省參數 調用函數時,缺省參數的值如果沒有傳入,則取默認值。 下例會打印默認的age,如果age沒有被傳入: ~~~ def printinfo(name, age=35): # 打印任何傳入的字符串 print("name: %s" % name) print("age %d" % age) # 調用printinfo函數 printinfo(name="miki") # 在函數執行過程中 age去默認值35 printinfo(age=9 ,name="miki") ~~~ 以上實例輸出結果: ~~~ name: miki age: 35 name: miki age: 9 ~~~ #### 總結: * 在形參中默認有值的參數,稱之為缺省參數 * 注意:帶有默認值的參數一定要位于參數列表的最后面 ~~~ >>> def printinfo(name, age=35, sex): ... print name ... File "<stdin>", line 1 SyntaxError: non-default argument follows default argument ~~~ ### 2\. 不定長參數 有時可能需要一個函數能處理比當初聲明時更多的參數, 這些參數叫做不定長參數,聲明時不會命名。 基本語法如下: ~~~ def functionname([formal_args,] *args, **kwargs): """函數_文檔字符串""" function_suite return [expression] ~~~ #### 注意: * 加了星號(\*)的變量args會存放所有未命名的變量參數,args為元組 * 而加\*\*的變量kwargs會存放命名參數,即形如key=value的參數, kwargs為字典. ~~~ >>> def fun(a, b, *args, **kwargs): ... """不定長參數演示示例""" ... print("a =%d" % a) ... print("b =%d" % b) ... print("args:") ... print(args) ... print("kwargs: ") ... for key, value in kwargs.items(): ... print("key=%s" % value) ... >>> fun(1, 2, 3, 4, 5, m=6, n=7, p=8) # 注意傳遞的參數對應 a = 1 b = 2 args: (3, 4, 5) kwargs: p = 8 m = 6 n = 7 >>> >>> >>> >>> c = (3, 4, 5) >>> d = {"m":6, "n":7, "p":8} >>> fun(1, 2, *c, **d) # 注意元組與字典的傳參方式 a = 1 b = 2 args: (3, 4, 5) kwargs: p = 8 m = 6 n = 7 >>> >>> >>> >>> fun(1, 2, c, d) # 注意不加星號與上面的區別 a = 1 b = 2 args: ((3, 4, 5), {'p': 8, 'm': 6, 'n': 7}) kwargs: >>> >>> ~~~ ### 3\. 缺省參數在\*args后面 ~~~ def sum_nums_3(a, *args, b=22, c=33, **kwargs): print(a) print(b) print(c) print(args) print(kwargs) sum_nums_3(100, 200, 300, 400, 500, 600, 700, b=1, c=2, mm=800, nn=900) ~~~ #### 說明: * 如果很多個值都是不定長參數,那么這種情況下,可以將缺省參數放到 \*args的后面, 但如果有\*\*kwargs的話,\*\*kwargs必須是最后的
                  <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>

                              哎呀哎呀视频在线观看