




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、Introduction to Computer ProgrammingSchool of Computer and Information Science Southwest Forestry University 2013.9計算機編程導論 西南林業大學計算機與信息學院2013.9ReviewChapter 1 sequential programmingUse computers to solve problemsEssentials of program designFlow chartProgram runningProblem-solving processSequential p
2、rogramming復習第1章 順序程序設計用計算機解決問題的方法程序設計要素流程圖程序運行過程解決問題的過程順序程序設計Chapter 2 Using ArrayArray are frequently used in storing data in program design. Almost every kind of programming language provides a array data structure, such as C and Basic provide one-dimensional, multi-dimensional array.Python provid
3、es the richest, the most flexible, and the most powerful forms in program design.This chapter describes the array structures (lists, tuples, dictionaries) in Python, and how to use them to achieve simple and powerful programs.第2章 使用序列序列是程序設計中經常用到的數據存儲方式,幾乎每一種程序設計語言都提供了表格數據結構,如C和Basic中的一維、多維數組等。Pytho
4、n提供的序列類型在所有程序設計語言中是最豐富,最靈活,也是功能最強大的。本章介紹使用Python中常用的序列結構(列表、元組、字典)來實現一些簡單而又功能強大的程序。 2.1 Array ProblemQ2-1 Data sortingDescription: Read 5 data from the keyboard, output in ascending order.Solutions 1: Use five variables to store data, compare and sort.2.1 序列問題【問題2-1】 數據排序問題問題描述:由用戶從鍵盤輸入五個數據,按升序排序后輸出
5、。 解決方案一:用五個變量來存儲輸入的五個數據,再用一一比較和交換來達到排序的目的。2.1 序列問題問題2-1程序:#data sort Ques2_1_0.pyx1 = input(請輸入第1個元素:)x2 = input(請輸入第2個元素:)x3 = input(請輸入第3個元素:)x4 = input(請輸入第4個元素:)x5 = input(請輸入第5個元素:)#將前兩個元素排好序if x1x2: x1,x2=x2,x1 #將前三個元素排好序if x1x3: x1,x2,x3=x3,x1,x2elif x2x3: x2,x3=x3,x2#將前四個元素排好序 if x1x4: x1,x2
6、,x3,x4=x4,x1,x2,x3elif x2x4: x2,x3,x4=x4,x2,x3elif x3x4: x3,x4=x4,x3#將前五個元素排好序 if x1x5: x1,x2,x3,x4,x5=x5,x1,x2,x3,x4elif x2x5: x2,x3,x4,x5=x5,x2,x3,x4elif x3x5: x3,x4,x5=x5,x3,x4elif x4x5: x4,x5=x5,x4#輸出排序結果print(x1,x2,x3,x4,x5) 2.1 Array ProblemShortcoming of Solution 1:Need to define many variabl
7、es to store dataThe program is not complex, but tedious, error-proneWhen sorting more data, such as 50, the programming becomes intolerableWhen sorting even more data, such as 1000, it will be ridiculous to write a program in this way.2.1 序列問題解決方案一存在的問題:需要定義多個變量來存儲數據程序雖不復雜,但很繁瑣,寫起來容易出錯當待排序數據達到一定量,如5
8、0個時,這樣的編程方式變得無法忍受當待排序數據達到一定量,如1000個時,這樣的程序無法編,無法看2.1 Array ProblemSolution 2:Use ListAnalysis:Input ten data, and store them in a listUse pythons sort () method to sort the list data and output.結束輸出排序后的結果開始用sort( )方法對列表中數據進行排序輸入10個數據存放在列表中圖2-1 數據排序流程圖2.1 序列問題解決方案二:使用列表分析:將用戶輸入的十個數據存放到一個列表中用python的so
9、rt( )方法對列表中數據進行排序后輸出。 結束輸出排序后的結果開始用sort( )方法對列表中數據進行排序輸入10個數據存放在列表中圖2-1 數據排序流程圖2.1 序列問題問題2-2程序: #data sort:Ques2_1.py data_list= #初始化一個空列表#循環十次,輸入十個數字放到列表中for integer in range(10): x=input(請輸入第+str(integer+1)+個元素:) data_list=data_list+xprint 排序前數據:,data_list#用sort方法對列表中的數據進行排序data_list.sort()print 排
10、序后數據:,data_list方案二優點:不管數據量多大,只用定義一個列表變量程序簡單,程序代碼量不隨數據量變大而增加程序可以調用Python內置函數來實現排序2.1 序列問題輸入及程序運行結果:請輸入第1個元素:54請輸入第2個元素:23請輸入第3個元素:67請輸入第4個元素:84請輸入第5個元素:41請輸入第6個元素:68請輸入第7個元素:34請輸入第8個元素:56請輸入第9個元素:98請輸入第10個元素:61排序前數據: 54, 23, 67, 84, 41, 68, 34, 56, 98, 61排序后數據: 23, 34, 41, 54, 56, 61, 67, 68, 84, 982
11、.1 Array ProblemQ2-2 Dictionary problemDescription: Find keyword explained.Analysis: some words are stored in the dictionary, the key is the first letter of the word, the value is the explanation of the word. The user enters a valid letter, the program queries the corresponding word and output it. I
12、f the input is out of range, the program ends.2.1 序列問題【問題2-2】 查字典問題 問題描述:根據用戶輸入的關鍵字的簡寫查詢相應名稱解釋。分 析:將一些程序設計中常用名稱存放在字典中,鍵是其英文的第一個字母,值是該名稱的解釋。由用戶輸入要查詢的名稱的英語第一個字母,若在合法的范圍內則進行查詢、輸出,若不在范圍內則結束程序。2.1 序列問題算法流程圖:開始定義字典結束圖2-2 查字典流程圖輸入ae范圍內的字母輸出查字典的結果2.1 序列問題問題2-2程序:#Dictionary Search:Ques2_2.py #定義字典dic=a:algo
13、rithm,算法,解決一種問題的大致步驟,b:bug,臭蟲,程序里的錯誤,c:compile,編譯,把用高級程序語言寫的程序轉換成低級語言,d:debugging,除蟲,找到及移除程序設計錯誤的過程,e:exception,異常,執行錯誤的另一個名稱#輸入要查詢的關鍵字以便進行字典查詢keyword=raw_input(請輸入要查詢的名詞關鍵字(ae):)#輸入ae之間的關鍵字則進行查詢,否則結束程序while keyword=a and keyword2.2 Array BasicsArray: A series of values, which are usually related to
14、, and in a certain order.Array c:Include 12 integer elementsUse Array:Array nameposition numberThe position of the first element is 0, c0The second element is c1The i-th element is ci-1Array can also be accessed from the rear:The last element is c-1Penultimate 2 is c-2Position number also known as t
15、he subscript or Index2.2 序列基礎知識序列:一系列連續值,它們通常是相關的,并且按一定順序排列。序列 c:12 個整數元素引用元素:序列名位置編號第1個元素的位置編號為 0,c0第 2個元素是 c1第 i個元素是ci-1 序列也可以從尾部訪問:最后一個元素是 c-1倒數第2個是 c-2倒數第 i個元素是 c-i位置編號也稱“下標”或“索引” ,是整數或整數表達式。2.3 ListList: A list is an ordered set of values, where each value is identified by an index.The elements
16、 of a list can be any typeEach data in the list called an elementAll its elements are separated by commas and placed in the bracket and List of examples:10, 20, 30, 40#All the elements are integer datacrunchy frog, ram bladder, lark vomit #All the elements are stringsspam, 2.0, 5, 10, 20#The list co
17、ntains a string element, a floating-point type element, an integer elements and a list2.3 列表列表:是Python中內置數據類型,是一個元素的有序集合一個列表中的數據類型可以各不相同列表中的每一個數據稱為元素其所有元素用逗號分割并放在一對中括號“”和“”中列表舉例:10, 20, 30, 40#所有元素都是整型數據的列表crunchy frog, ram bladder, lark vomit#所有元素都是字符串的列表spam, 2.0, 5, 10, 20#該列表中包含了一個字符串元素、一個浮點類型元素
18、、一個整型元素和一個列表類型元素2.3 List(1) Create: use the = to assign a list to a variable.For example: a_list = a, b, mpilgrim, z, example(2) Read elements We can access an element in the list by using the variable name and an index number. Pay attention to the list of the first elements of a serial number 0.For
19、 example: print(a_list2)mpilgrimNote: If a list has n elements, then the valid range is from -n to n-1, when the index number x is negative, indicating that the counting starts from the right, and the actual index number of the element is n + x .2.3 列表(1)創建列表:使用“=”將一個列表賦值給變量。例如: a_list = a, b, mpilg
20、rim, z, example(2)讀取元素 用變量名加元素序號(放中括號中)即可訪問列表中某個元素,注意列表的第一個元素序號為0。例如: print(a_list2)mpilgrim注意:若一個列表有n個元素,則訪問元素的合法序號范圍是-nn-1,當序號x為負時,表示從右邊計數,其訪問的元素實際為序號為n+x的元素。2.3 列表例如: print(a_list-1)example print(a_list-5)a print(a_list-7)Traceback (most recent call last): File , line 1, in print(a_list-7)IndexEr
21、ror: list index out of range print(a_list5)Traceback (most recent call last): File , line 1, in print(a_list5)IndexError: list index out of rangea_list = a, b, mpilgrim, z, example2.3 List(3) SlicesUse a range of indexes to intercept any part of the list to get a new list.The first number represents
22、 the slice start position, and the second number represents the slice cutoff (but does not contain) position.For example: print(a_list1:3)b, mpilgrim print(a_list1:-1)b, mpilgrim, za_list = a, b, mpilgrim, z, example2.3 列表(3)列表切片 可以使用列表序號對來截取列表中的任何部分從而得到一個新列表。序號對中第一個序號表示切片開始位置,第二個序號表示切片截止(但不包含)位置。 例
23、如: print(a_list1:3)b, mpilgrim print(a_list1:-1)b, mpilgrim, z2.3 列表注意:當切片的左索引為0時可缺省,當右索引為列表長度時也可缺省。例如: print(a_list:3)a, b, mpilgrim print(a_list3:)z, example print(a_list:)a, b, mpilgrim, z, examplea_list = a, b, mpilgrim, z, example2.3 List(4) Adding elementsMethod 1: Use the + and a new list att
24、ached to the tail of the original list; a_list = 1 a_list = a_list + a, 2.0 a_list1, a, 2.0 Method 2: Using append () method adds a new element to the end of the list; a_list.append(True) a_list1, a, 2.0, True 2.3 列表(4)增加元素方法一:使用“+”將一個新列表附加在原列表的尾部; a_list = 1 a_list = a_list + a, 2.0 a_list1, a, 2.0
25、方法二:使用append( )方法向列表尾部添加一個新元素; a_list.append(True) a_list1, a, 2.0, True2.3 ListMethod 3: Use the extend () method to add a list at the tail of the original list; a_list.extend(x, 4) a_list1, a, 2.0, True, x, 4 Method 4: Use the insert () method to add an element into the list anywhere. a_list.inser
26、t(0, x) a_listx, 1, a, 2.0, True, x, 42.3 列表方法三:使用extend( )方法將一個列表添加在原列表的尾部; a_list.extend(x, 4) a_list1, a, 2.0, True, x, 4方法四:使用insert( )方法將一個元素插入到列表的任意位置。 a_list.insert(0, x) a_listx, 1, a, 2.0, True, x, 42.3 List(5) SearchUse count ( ) method to calculate the number of an element in the list; a_
27、list.count(x)2Use ”in” Operator checking whether an element is in the list; 3 in a_listFalse 2.0 in a_listTruea_list = x, 1, a, 2.0, True, x, 42.3 列表(5)檢索元素使用count( )方法計算列表中某個元素出現的次數; a_list.count(x)2使用in運算符返回某個元素是否在該列表中; 3 in a_listFalse 2.0 in a_listTrue2.3 列表Use the index () method to return the
28、exact location of an element in the list;使用index()方法返回某個元素在列表中的準確位置; a_list.index(x)0 a_list.index(5)Traceback (most recent call last): File , line 1, in a_list.index(5)ValueError: 5 is not in lista_list = x, 1, a, 2.0, True, x, 42.3 List(6) Delete an elementWhen adding or removing elements, the lis
29、t will automatically expand or shrink. The list will never have gaps.Method 1: Use the “del” statement to delete the elements at a particular location del a_list1 a_listx, a, 2.0, True, x, 4a_list = x, 1, a, 2.0, True, x, 42.3 列表(6)刪除元素 當向列表中添加或刪除元素時,列表將自動拓展或收縮,列表中永遠不會有縫隙。方法一:使用del語句刪除某個特定位置的元素 del
30、a_list1 a_listx, a, 2.0, True, x, 42.3 列表Method2: Use “remove()” method to remove an element with a particular value方法二:使用remove方法刪除某個特定值的元素 a_list.remove(x) a_lista, 2.0, True, x, 4 a_list.remove(x) a_lista, 2.0, True, 4 a_list.remove(x)Traceback (most recent call last): File , line 1, in a_list.re
31、move(x)ValueError: list.remove(x): x not in lista_list = x, a, 2.0, True, x, 42.3 列表Method three: Use pop() method to pop out the element at the specified location. The last element is popped out if no parameters given.方法三:使用pop(參數)方法來彈出(刪除)指定位置的元素,缺省參數時彈出最后一個元素。 a_list.pop( )4 a_lista, 2.0, True a_
32、list.pop(1)2.0 a_lista, True a_list.pop(1)True a_lista a_list.pop( )a a_list a_list.pop( )Traceback (most recent call last): File , line 1, in a_list.pop( )IndexError: pop from empty list2.3.3 Function of the list1. cmp( )Format:cmp(List1,List2)Function:Compare the two lists,returns 1 when the first
33、 list is greater than the second, on the contrary returns -1, when the two elements of the list is the same returns 02. len( )Format:len(List)Function:Returns the number of elements in the list2.3.3 列表常用函數1. cmp( )格式:cmp(列表1,列表2)功能:對兩個列表進行比較,若第一個列表大于第二個,則結果為1,相反則為-1,元素完全相同則結果為0。 2. len( )格式:len(列表)功
34、能:返回列表中的元素個數。 2.3.3 Function of the list list1=123,xyz list2=123,abc cmp(list1,list2)1 list2=123,z cmp(list1,list2)-1 list2=list1 cmp(list1,list2)0 len(list1)2 2.3.3 Function of the list3. max( ),min( )Format:max(List), min(List)Function:Return the largest or smallest element in the list str_l=abc,xyz,123 num_l=123,456,222 max(str_l)xyz min(str_l)123 max(num_l)456 min(num_l)1232.3.3 列表常用函數3. max( )和min( ) 格式:max(列表), min(列表)功能:返回列表中的最大或最小元素。 str_l=abc,xyz,123 num_l=123
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年 懷化麻陽苗族自治縣招聘事業單位工作人員考試試題附答案
- 2025年 漢陰縣職業技術教育培訓中心招聘考試筆試試題附答案
- 2025年 福建信息職業技術學院招聘考試筆試試題附答案
- 2025年中國定制門窗行業市場深度分析及未來發展趨勢預測報告
- 中國影音設備行業市場全景調研及投資規劃建議報告
- 2024年中國電解氯化氫行業市場調查報告
- 中國甜菜行業發展潛力預測及投資戰略研究報告
- 2019-2025年中國低溫肉制品市場行情動態分析及發展前景趨勢預測報告
- 中國單色數字示波器行業市場發展前景及發展趨勢與投資戰略研究報告(2024-2030)
- 2021-2026年中國泵用金屬軟管行業市場供需格局及行業前景展望報告
- 風場前期相關windpro2中文版幫助文件
- GB/T 12149-2017工業循環冷卻水和鍋爐用水中硅的測定
- 成都小升初數學分班考試試卷五
- Q∕SY 01007-2016 油氣田用壓力容器監督檢查技術規范
- 水利水電 流體力學 外文文獻 外文翻譯 英文文獻 混凝土重力壩基礎流體力學行為分析
- 零星維修工程項目施工方案
- 物流公司超載超限整改報告
- 起重機安裝施工記錄表
- 江蘇省高中學生學籍卡
- 碳排放問題的研究--數學建模論文
- 贏越酒會講解示范
評論
0/150
提交評論