<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] ### 課前準確: * 在`Windows`系統里安裝`python`,參考 [安裝python](http://www.hmoore.net/k12edu/k_12/785427#Windowspython_4) ***** ### 本節課參考資料 * `Teach Your Kids to Code.pdf` 第七章:函數 * `python:從入門到實踐.pdf` 第八章:函數 * [W3schools:python_functions](https://www.w3schools.com/python/python_functions.asp) * [python_lambda](https://blog.csdn.net/zjuxsl/article/details/79437563) ***** ### 如何定義一個函數 無參函數: ``` def my_function(): print("你好,我是一個無參函數!") ``` 有參函數: ``` def my_function(fname): print("很高興認識你,"+fname) my_function("JiaMing") my_function("Ben") my_function("LiMing") ``` 上面這段代碼也可以改成`for`循環: ``` def my_function(fname): print("很高興認識你,"+fname) names = ["JiaMing","Ben","LiMing"] for fname in names: my_function(fname) ``` 帶默認值的有參函數: ``` def my_function(country = "挪威"): print("我來自" + country) my_function("瑞典") my_function("印度") my_function() my_function("巴西") ``` 函數可以用來計算: ``` def my_function(x): return 5 * x print(my_function(3)) print(my_function(5)) print(my_function(9)) ``` ### 一種特殊的函數:`lambda`函數 * `lambda`函數是匿名函數 * `lambda`函數有輸入和輸出 * `lambda`函數的功能比較簡單 下面三段代碼分別傳入了一個、兩個、三個參數、任意數量的參數: ``` x = lambda a : a + 10 print(x(5)) ``` ``` x = lambda a, b : a * b print(x(5, 6)) ``` ``` x = lambda a, b, c : a + b + c print(x(5, 6, 2)) ``` 使用` *args` 表示任意個參數 ``` y = lambda *args : sum(args) print(y(5, 6, 2)) ``` 當在另一個函數中使用`lambda`函數時,`lambda`函數的功能將得到更好的顯示。假設您有一個函數定義,它接受一個參數,并且該參數將被乘以一個未知數: ``` def myfunc(n): return lambda a : a * n mydoubler = myfunc(2) print(mydoubler(11)) ``` 想一想,哪個參數是`n`,哪個是`a`? ``` def myfunc(n): return lambda a : a / n mydoubler = myfunc(0) mytripler = myfunc(10) print(mydoubler(20)) print(mytripler(20)) ``` ``` # chapter7_1.py def get_formatted_name(first_name, last_name): """返回整潔的姓名""" full_name = first_name + last_name return full_name.title() while True: print("\n你叫什么名字:") print("如果你不想告訴我,請按 'q'。") f_name = input("你的姓: ") if f_name == 'q': break l_name = input("你的名:") if l_name == 'q': break formatted_name = get_formatted_name(f_name, l_name) print("\n你好啊, " + formatted_name + "!") ``` ### 做一個練習 ``` # chapter7_2.py import random import turtle t = turtle.Pen() t.speed(0) turtle.bgcolor("black") colors = ["red", "yellow", "blue", "green", "orange", "purple", "white", "gray"] # 定義函數 random_spiral def random_spiral(): t.pencolor(random.choice(colors)) size = random.randint(10,40) x = random.randrange(-turtle.window_width()//2, turtle.window_width()//2) y = random.randrange(-turtle.window_height()//2, turtle.window_height()//2) t.penup() t.setpos(x,y) t.pendown() for m in range(size): t.forward(m*2) t.left(91) for n in range(50): random_spiral() ``` ### 課程小結 1. 有參數、無參數函數 2. `lambda`函數 3. 如何表達任意個參數
                  <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>

                              哎呀哎呀视频在线观看