1. 程式人生 > >Python 程式設計語言 筆記(一)

Python 程式設計語言 筆記(一)

原文地址https://blog.csdn.net/longteng007/article/details/50989535

<div class="htmledit_views">                  <p></p> <p align="center" style="text-align:left;border:0px;list-style:none;"> <span><span style="font-family:'Microsoft YaHei';font-size:18px;"><span><span>學習課程:</span></span><a href="http://www.icourse163.org/course/bit-268001#/info" rel="nofollow" target="_blank"><span>《</span><span>Python  語言程式設計》</span></a></span></span></p> <p align="center" style="text-align:left;border:0px;list-style:none;"> <span style="font-family:'Microsoft YaHei';font-size:18px;"><span>開設學校:</span><span>北京理工大學</span></span></p> <p align="center" style="text-align:left;border:0px;list-style:none;"> <span><span style="font-family:'Microsoft YaHei';font-size:18px;"><span>教學老師:</span><span><span>嵩天、黃天羽、禮欣</span></span></span></span></p> <p align="center" style="text-align:left;border:0px;list-style:none;"> <span><span><span><span style="font-family:'Microsoft YaHei';font-size:18px;"><span>學習者:</span><span>廣東海洋大學  計科1154 許佳耀</span></span></span></span></span></p> <p align="center" style="text-align:left;border:0px;list-style:none;"> <span style="font-family:'Microsoft YaHei';font-size:18px;"><span>學習時間:</span><span>&nbsp;2016年3月6日 4h</span></span></p> <p></p> <p align="center" style="text-align:left;border:0px;list-style:none;"> <span><span style="font-family:'Microsoft YaHei';font-size:18px;">一、學習心得</span></span></p> <p align="center" style="text-align:left;border:0px;list-style:none;"> <span style="font-family:'Microsoft YaHei';font-size:18px;">第一次有幸能夠在《學習與創業》課程老師 丁老師 的指導下選了這門程式語言課。在上網路課之前其實我的程式語言和其他方面的學習一直都是靠看各種電子書來進行的,雖然電子書很方便,但是唯一的缺點就是書是死的。在閱讀過程中遇到的問題沒有辦法能夠及時得到解決。好在有慕課可以彌補這個缺點,在跟隨嵩老師學習Python語言的過程中遇到不懂的問題可以及時在論壇裡詢問,也可以自己噹噹老師給別人的問題予以解答。這樣既能解決自身的問題,同時也是對自己所學知識的一種鞏固和提升。</span></p> <p align="center" style="text-align:left;border:0px;list-style:none;"> <span style="font-family:'Microsoft YaHei';font-size:18px;">我覺得嵩老師的課條理和層次都很清晰,讓我能夠知道我應該需要注意哪些地方,並且在分析語言語法元素的過程中老師可以逐條分析語句,我覺得這樣一方面能看得清楚整個語言的語法結構,另外一方面對語法中不明白的部分,通過講解也可以得到較為全面的認識。我希望在接下來的一個星期裡,我可以把這些語法規則都記住,然後去編寫一個屬於自己的一個python程式吧。</span></p> <p align="center" style="text-align:left;border:0px;list-style:none;"> <span><span style="font-family:'Microsoft YaHei';font-size:18px;"><br></span></span></p> <p align="center" style="text-align:left;border:0px;list-style:none;"> <span><span style="font-family:'Microsoft YaHei';font-size:18px;">二、課堂筆記</span></span></p> <p align="center" style="text-align:left;border:0px;list-style:none;"> <span style="font-family:'Microsoft YaHei';font-size:18px;"><strong>第一週 基本程式設計</strong></span></p> <p><strong><span style="font-family:'Microsoft YaHei';font-size:18px;">1. 1程式設計的基本方法 —— IPO(輸入、輸出、處理(演算法))</span></strong></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">(1)理解問題的計算部分:抽象問題 →使用IPO → 從多種方法中選擇一種解決問題</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">(2)程式編寫步驟:</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ①分析問題:分析問題的計算部分</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ②確定問題:將計算部分劃分為<strong><span style="color:#FF0000;">IPO</span></strong>三部分</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ③設計演算法&nbsp;&nbsp;&nbsp; ④ 編寫程式&nbsp;&nbsp;&nbsp; ⑤ 除錯測試&nbsp;&nbsp;&nbsp; ⑥ 升級維護</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">例:溫度轉換問題</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">【問題描述】對於攝氏度和華氏度,存在以下轉換:</span></p> <table border="1" cellspacing="0" cellpadding="0"><tbody><tr><td style="background:#5B9BD5;"> <p align="center"><span style="color:#FFFFFF;"><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;</span></span></p> </td> <td style="background:#5B9BD5;"> <p align="center"><span style="color:#FFFFFF;"><span style="font-family:'Microsoft YaHei';font-size:18px;">氣壓</span></span></p> </td> <td style="background:#5B9BD5;"> <p align="center"><span style="color:#FFFFFF;"><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;結冰點&nbsp;</span></span></p> </td> <td style="background:#5B9BD5;"> <p align="center"><span style="color:#FFFFFF;"><span style="font-family:'Microsoft YaHei';font-size:18px;">沸點</span></span></p> </td> </tr><tr><td> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;攝氏度&nbsp;</span></p> </td> <td> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;1標準大氣壓&nbsp;</span></p> </td> <td> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;">0度</span></p> </td> <td> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;100度&nbsp;</span></p> </td> </tr><tr><td> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;">華氏度</span></p> </td> <td> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;">1標準大氣壓</span></p> </td> <td> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;">0度</span></p> </td> <td> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;">212度</span></p> </td> </tr></tbody></table><p><span style="font-family:'Microsoft YaHei';font-size:18px;">請利用python程式完成以上轉換。</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">【解題步驟】</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">①&nbsp;&nbsp;&nbsp;&nbsp;分析問題的計算部分 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;簡化為三個步驟:</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">②&nbsp;&nbsp;&nbsp;&nbsp;確定功能,利用IPO方法進一步分析:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ①確定IPO</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">輸入:華氏度(F)/攝氏度(C) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;②編寫程式</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">處理:溫度轉化公式&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span></span>&nbsp; &nbsp;&nbsp;③除錯程式</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">輸出:攝氏度(C)/華氏度(F)</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">③&nbsp;&nbsp;&nbsp;&nbsp;設計演算法</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">④&nbsp;&nbsp;&nbsp;&nbsp;編寫程式</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">⑤&nbsp;&nbsp;&nbsp;&nbsp;測試除錯</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">⑥&nbsp;&nbsp;&nbsp;&nbsp;升級維護</span></p> <p><strong><span style="font-family:'Microsoft YaHei';font-size:18px;">1.2 python程式元素分析</span></strong></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;"><span style="color:#002060;">程式</span><span style="color:#002060;">1</span><span style="color:#002060;">:</span></span></p> <p></p> <pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment"># TempConvert.py</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">val = input(<span class="hljs-string">"請輸入帶溫度表示符號的溫度值(例如: 32C): "</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">if</span> val[<span class="hljs-number">-1</span>] <span class="hljs-keyword">in</span> [<span class="hljs-string">'C'</span>,<span class="hljs-string">'c'</span>]:                                 <span class="hljs-comment"># in 判斷左側內容是否在右側集合中</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">       f = <span class="hljs-number">1.8</span> *float(val[<span class="hljs-number">0</span>:<span class="hljs-number">-1</span>]) + <span class="hljs-number">32</span>                    <span class="hljs-comment"># float() 將括號中的字串轉換為浮點數</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">       print(<span class="hljs-string">"轉換後的溫度為: %.2fF"</span>%f)                  <span class="hljs-comment"># .2表示輸出的浮點數f有兩位小數</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">elif</span> val[<span class="hljs-number">-1</span>] <span class="hljs-keyword">in</span> [<span class="hljs-string">'F'</span>, <span class="hljs-string">'f'</span>]:</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">       c =(float(val[<span class="hljs-number">0</span>:<span class="hljs-number">-1</span>]) - <span class="hljs-number">32</span>) / <span class="hljs-number">1.8</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">       print(<span class="hljs-string">"轉換後的溫度為: %.2fC"</span>%c)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">else</span>:</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">       print(<span class="hljs-string">"輸入有誤"</span>)</div></div></li></ol></code><div class="hljs-button" data-title="複製"></div></pre><br><br><p></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">(1)縮排:標明程式碼層次關係、表明程式框架</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">(2)註釋:輔助說明,不被執行也不受語法約束,可以寫入任何內容,用以提高可讀性</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 註釋方法:</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">①&nbsp;&nbsp;&nbsp;&nbsp;單行註釋:# 開頭;</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">②&nbsp;&nbsp;&nbsp;&nbsp;多行註釋:''' 開頭和結尾;</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">(3)變數:能夠表示值的一個名稱,值可以變化</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;命名:給自定義元素關聯名字的過程(唯一性)</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;命名規則:① <u><span style="color:#FF0000;">大小寫字母、數字、下劃線(大小寫不統一)</span></u></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;②首字母只能是大小寫字母或下劃線,<span style="color:#002060;">不能使用空格</span></span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;③ 中文等非字母符號也可以作為名字</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;④不能與python定義的保留字相同</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">(4)表示式:產生/計算資料值的一行程式碼(通過保留字和操作符)</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">(5)空格的使用:① 表示每行縮排關係的空格不能改變</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;②空格不能分割命名</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">(6)input()函式:從控制檯獲得使用者的輸入。使用方法為:</span></p> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;"><span style="color:#FF0000;">&lt;</span><span style="color:#FF0000;">變數</span><span style="color:#FF0000;">&gt; = input(&lt;</span><span style="color:#FF0000;">提示性文字</span><span style="color:#FF0000;">&gt;)</span></span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">獲得的使用者輸入以字串&lt;””&gt;的形式儲存在&lt;變數&gt;中</span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">如果val = “28C”,則val[-1] = “C”,val[0:2] = “28” 表示從區間[0,2)的一個子字串</span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">可以通過val[0:-1]來獲取除了最後一個字元以外的字串</span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">(7)分支語句:根據判斷條件,執行不同的路徑。使用方法:</span></p> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;"><span style="color:#FF0000;">If &lt;</span><span style="color:#FF0000;">條件</span><span style="color:#FF0000;">1</span><span style="color:#FF0000;">成立</span><span style="color:#FF0000;">&gt;</span></span></p> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;"><span style="color:#FF0000;">&lt;</span><span style="color:#FF0000;">表示式組</span><span style="color:#FF0000;">1&gt;</span></span></p> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;"><span style="color:#FF0000;">elif &nbsp; &nbsp;&lt;</span><span style="color:#FF0000;">條件</span><span style="color:#FF0000;">2</span><span style="color:#FF0000;">成立</span><span style="color:#FF0000;">&gt;</span></span></p> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;"><span style="color:#FF0000;">&lt;</span><span style="color:#FF0000;">表示式組</span><span style="color:#FF0000;">2&gt;</span></span></p> <p align="center"><span style="color:#FF0000;"><span style="font-family:'Microsoft YaHei';font-size:18px;">…</span></span></p> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;"><span style="color:#FF0000;">elif &nbsp; &nbsp;&lt;</span><span style="color:#FF0000;">條件</span><span style="color:#FF0000;">N-1</span><span style="color:#FF0000;">成立</span><span style="color:#FF0000;">&gt;</span></span></p> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;"><span style="color:#FF0000;">&lt;</span><span style="color:#FF0000;">表示式組</span><span style="color:#FF0000;">N-1&gt;</span></span></p> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;"><span style="color:#FF0000;">else: &lt;</span><span style="color:#FF0000;">表示式組</span><span style="color:#FF0000;">N&gt;</span></span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">(8)賦值:同步賦值:同時給多個變數賦值。使用方法:</span></p> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;"><span style="color:#FF0000;">&lt;</span><span style="color:#FF0000;">變數</span><span style="color:#FF0000;">1&gt;,…,&lt;</span><span style="color:#FF0000;">變數</span><span style="color:#FF0000;">N&gt; = &lt;</span><span style="color:#FF0000;">表示式</span><span style="color:#FF0000;">1&gt;,…,&lt;</span><span style="color:#FF0000;">表示式</span><span style="color:#FF0000;">N&gt;</span></span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">例:若要使變數x和y的值實現交換,則只需一行程式碼:x, y= y, x</span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">(9)print()函式:用來輸出字元資訊,或以字元的形式輸出變數的值,通過%選擇要輸出的變數</span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">(10)迴圈語句:控制程式迴圈執行的語句,根據判斷/計數條件確定迴圈次數。</span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">使用方法:</span></p> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;"><span style="color:#FF0000;">for I in range(&lt;</span><span style="color:#FF0000;">計數值</span><span style="color:#FF0000;">&gt;):</span></span></p> <p align="center"><span style="font-family:'Microsoft YaHei';font-size:18px;color:#FF0000;">&lt;</span><span style="font-family:'Microsoft YaHei';font-size:18px;color:#FF0000;">表示式組</span><span style="font-family:'Microsoft YaHei';font-size:18px;color:#FF0000;">&gt;</span></p> <p><span style="color:#FF0000;"><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;</span></span></p> <p align="left"><strong><span style="font-family:'Microsoft YaHei';font-size:18px;">函式編寫模板</span></strong></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">(1)input–print模板:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (2)initial – print模板:</span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">① 使用者輸入:input()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ① 初始變數:運算需要的初始值</span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">② 運算部分:演算法實現 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;② 運算部分:演算法實現</span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">③ 結果輸出:print()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ③ 結果輸出:print()</span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;</span></p> <p align="left"><strong><span style="font-family:'Microsoft YaHei';font-size:18px;">1.3 python蟒蛇程式的實現分析:</span></strong></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;"><span style="color:#002060;">程式</span><span style="color:#002060;">2</span><span style="color:#002060;">:</span></span></p> <p align="left"></p><pre onclick="hljs.copyCode(event)"><code class="language-python hljs"><ol class="hljs-ln" style="width:973px"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">import</span> turtle                &lt;span style=<span class="hljs-string">"white-space:pre"</span>&gt;    &lt;/span&gt;<span class="hljs-comment"># (烏龜)軌跡描繪函式庫</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">drawSnake</span><span class="hljs-params">(rad,angle,len,neckrad)</span>:</span>    &lt;span style=<span class="hljs-string">"white-space:pre"</span>&gt;    &lt;/span&gt;<span class="hljs-comment"># 繪製蟒蛇函式</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(len):</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">        turtle.circle(rad,angle)&lt;span style=<span class="hljs-string">"white-space:pre"</span>&gt;    &lt;/span&gt;<span class="hljs-comment"># 圓形軌跡函式,引數rad描述圓形半徑的位置</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">        turtle.circle(-rad,angle)&lt;span style=<span class="hljs-string">"white-space:pre"</span>&gt;    &lt;/span&gt;<span class="hljs-comment"># rad為負值,半徑在烏龜右側</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">    turtle.circle(rad,angle/<span class="hljs-number">2</span>)    &lt;span style=<span class="hljs-string">"white-space:pre"</span>&gt;    &lt;/span&gt;<span class="hljs-comment"># 引數angle表示烏龜沿圓形爬行的弧度值</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">    turtle.fd(rad)                <span class="hljs-comment"># 也稱為turtle.forward()函式</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">&lt;span style=<span class="hljs-string">"white-space:pre"</span>&gt;                        &lt;/span&gt;<span class="hljs-comment"># 表示軌跡直線移動,引數表示移動距離</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">    turtle.circle(neckrad+<span class="hljs-number">1</span>,<span class="hljs-number">180</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">    turtle.fd(rad*<span class="hljs-number">2</span>/<span class="hljs-number">3</span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">    turtle.setup(<span class="hljs-number">1300</span>,<span class="hljs-number">800</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>)    <span class="hljs-comment"># 啟動圖形視窗,引數分別為視窗的寬度、高度</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">&lt;span style=<span class="hljs-string">"white-space:pre"</span>&gt;                    &lt;/span&gt;<span class="hljs-comment"># 視窗左上角點的橫、縱座標位置</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">    pythonsize = <span class="hljs-number">30</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">    turtle.pensize(pythonsize)    <span class="hljs-comment"># 軌跡的寬度,引數為畫素大小,這裡為30</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">    turtle.pencolor(<span class="hljs-string">"blue"</span>)        <span class="hljs-comment"># 軌跡顏色,引數為字串(英文或16進位制顏色程式碼)</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">    turtle.seth(<span class="hljs-number">-40</span>)        <span class="hljs-comment"># 軌跡運動方向,引數為角度(逆時針,以正東方向為0度)</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">    drawSnake(<span class="hljs-number">40</span>,<span class="hljs-number">80</span>,<span class="hljs-number">5</span>,pythonsize/<span class="hljs-number">2</span>)    <span class="hljs-comment"># 繪製蟒蛇函式,用以繪製python蟒蛇</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">main()</div></div></li></ol></code><div class="hljs-button" data-title="複製"></div></pre><br><br><br><br><p></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">def定義函式:① 函式是一組程式碼的集合,用以完成特定的功能,這組程式碼的名字就叫函式名</span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">② def所定義的函式,必須要通過函式名對函式進行呼叫才能執行</span></p> <p align="left"><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;</span></p> <p align="left"><strong><span style="font-family:'Microsoft YaHei';font-size:18px;">函式庫的引用:</span></strong></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">方式一:<span style="color:#FF0000;">import &lt;</span><span style="color:#FF0000;">庫名&gt;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>如:import turtle</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">方式二:<span style="color:#FF0000;">from &lt;</span><span style="color:#FF0000;">庫名&gt; import &lt;</span>函式名&gt;</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;"><span style="color:#FF0000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from &lt;</span><span style="color:#FF0000;">庫名&gt; import *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>← &nbsp;呼叫函式時不需要使用庫名,直接使用</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">如:from turtle *</span></p> <p><span style="font-family:'Microsoft YaHei';font-size:18px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fd(100)</span></p> <p><span style="color:#002060;"><span style="font-family:'Microsoft YaHei';font-size:18px;">注意:方式一可呼叫不同庫中的同名函式,方式二則不可</span></span></p>             </div>