本書詳細介紹了Python 程序設計的基本環(huán)境、概念、方法和應用,特別是Python 在數(shù)據(jù)獲取、數(shù)據(jù)處理、數(shù)據(jù)展示等方面的應用。本書分為三部分:第一部分(第1~ 4 章)是Python 語言的基礎知識,包括數(shù)據(jù)與運算、語句與控制結構、組合數(shù)據(jù)類型;第二部分(第5 ~ 8 章)是Python 語言的高級部分,包括函數(shù)、常用的算法 、文件與異常、類與模塊;第三部分(第9 ~12 章)是Python 的綜合應用,包括文本處理、網(wǎng)絡爬蟲及API 調用、圖表展示及綜合應用、圖形用戶界面。本書內容詳盡、循序漸進,既介紹語法,又講解應用。本書提供了大量典型實例,特別是數(shù)字、文本、圖形三方面的實例。所有的實例都配有二維碼,讀者掃碼后可以打開一個界面,其中可以修改并測試程序,還能分步運行,以查看程序的運行流程和變量的變化。本書可作為高等學校各專業(yè)Python 程序設計課程的教材,也可作為全國等級考試二級Python 的參考書,還可作為計算機技術的培訓教材和自學用書。
唐大仕,博士,教學名師,自1995年開始一直在北京大學從事計算機應用與編程教學科研工作!禖#程序設計》和《Java程序設計》分別于2017年和2018年獲評國家級精品在線開放課程。
目錄
第1 章 Python 基礎 ······················.1
1.1 Python 語言 ······························.1
1.1.1 程序設計語言 ··················.1
1.1.2 Python 語言的產(chǎn)生 ············.3
1.1.3 Python 語言的特點 ············.3
1.1.4 Python 語言的應用場景 ······.5
1.2 Python 的安裝 ···························.6
1.2.1 下載 Python ·····················.6
1.2.2 安裝 Python ·····················.7
1.3 Python 程序的書寫與運行 ············.8
1.3.1 使用IDLE 交互式環(huán)境·······.8
1.3.2 程序文件的建立與運行 ······.9
1.4 簡單的Python 程序 ····················.11
1.4.1 輸入、處理、輸出
(IPO) ···························.11
1.4.2 常見的語法要素 ···············12
1.5 其他集成開發(fā)工具 ······················16
1.5.1 Visual Studio Code·············16
1.5.2 PyCharm·························17
1.5.3 Anaconda ························17
本章小結 ········································19
習題1 ············································19
第2 章 數(shù)據(jù)與運算 ·······················20
2.1 變量與數(shù)據(jù)類型 ·························20
2.1.1 數(shù)據(jù)、常量與變量 ············20
2.1.2 基本數(shù)據(jù)類型 ··················20
2.1.3 類型的判斷與轉換 ············22
2.2 算術運算及數(shù)學函數(shù) ···················23
2.2.1 運算符與表達式 ···············23
2.2.2 算術運算符 ·····················23
2.2.3 內置函數(shù) ························25
2.2.4 math 模塊中的數(shù)學函數(shù) ·····27
2.3 關系運算及邏輯運算 ···················28
2.3.1 關系運算 ························28
2.3.2 邏輯運算 ························29
2.3.3 運算符的優(yōu)先級與結合性 ···30
2.4 字符串運算及格式化 ···················31
2.4.1 字符串的常見運算 ············31
2.4.2 數(shù)據(jù)的顯示與格式化 ·········32
本章小結 ········································34
習題2 ············································34
第3 章 語句與控制結構 ·················36
3.1 基本語句及順序結構 ···················36
3.1.1 三種基本流程及流程圖 ······36
3.1.2 簡單語句 ························37
3.2 分支語句 ··································38
3.2.1 if 語句 ···························38
3.2.2 if 語句的嵌套 ··················40
3.2.3 elif 語句 ·························40
3.2.4 應用舉例——自動出題
并判分 ···························42
3.3 循環(huán)語句 ··································44
3.3.1 for 循環(huán)語句 ····················44
3.3.2 while 循環(huán)語句 ················47
3.3.3 循環(huán)的五要素及153
檢查法 ···························48
3.3.4 循環(huán)語句的嵌套 ···············49
3.4 循環(huán)語句的應用 ·························50
3.4.1 繪圖 ······························50
3.4.2 數(shù)列求和 ························54
3.4.3 字符串加密 ·····················55
3.4.4 跳轉語句 ························56
本章小結 ········································57
習題3 ············································57
第4 章 組合數(shù)據(jù)類型 ····················59
4.1 列表 ········································59
4.1.1 列表是一個序列 ···············59
4.1.2 列表的操作 ·····················60
4.1.3 列表的遍歷 ·····················63
4.1.4 列表的方法 ·····················64
4.2 字典 ········································65
4.2.1 字典即映射 ·····················65
4.2.2 字典的操作 ·····················66
4.2.3 字典的遍歷 ·····················68
4.2.4 字典的方法 ·····················68
4.3 元組與集合 ·······························69
4.3.1 元組 ······························69
4.3.2 集合 ······························71
4.3.3 列表、字典、元組、集合的
區(qū)別與轉換 ·····················73
4.4 組合數(shù)據(jù)的應用 ·························76
4.4.1 列表在統(tǒng)計中的應用 ·········76
4.4.2 字典在統(tǒng)計中的應用 ·········77
4.4.3 蒙特卡羅法 ·····················78
本章小結 ········································82
習題4 ············································82
第5 章 函數(shù) ································84
5.1 函數(shù)的定義與調用 ······················84
5.1.1 函數(shù)的概念 ·····················84
5.1.2 定義函數(shù) ························85
5.1.3 任務分解與代碼重用 ·········86
5.2 函數(shù)中的變量與參數(shù)傳遞 ·············91
5.2.1 函數(shù)調用的執(zhí)行流程 ·········91
5.2.2 變量的生存期和作用域 ······93
5.2.3 參數(shù)的傳遞 ·····················94
5.3 函數(shù)的高級特性 ·························95
5.3.1 列表推導式 ·····················96
5.3.2 排序與lambda 表達式········97
5.3.3 映射、篩選及高階函數(shù) ······99
本章小結 ·····································.101
習題5 ·········································.101
第6 章 常用的算法 ·····················102
6.1 算法 ·····································.102
6.1.1 算法的概念 ··················.102
6.1.2 算法的判斷標準及代價 ···.103
6.2 窮舉算法 ·······························.106
6.3 迭代算法 ·································110
6.4 遞歸算法 ·································111
本章小結 ·······································116
習題6 ···········································117
第7 章 文件與異常 ····················.119
7.1 文件及其基本操作 ·····················119
7.1.1 文件路徑及文件分類 ········119
7.1.2 文件的基本操作 ············.121
7.2 文本文件與二進制文件 ·············.123
7.2.1 文本文件的讀寫 ············.123
7.2.2 二進制文件的讀寫 ·········.126
7.2.3 文本文件的應用示例 ······.126
7.3 數(shù)據(jù)文件的應用 ······················.127
7.3.1 數(shù)據(jù)文件的讀取及統(tǒng)計 ···.127
7.3.2 數(shù)據(jù)文件的查詢及篩選 ···.128
7.3.3 數(shù)據(jù)文件的分類匯總 ······.129
7.4 目錄操作及文件批量處理 ··········.130
7.4.1 文件及目錄的基本操作 ···.131
7.4.2 目錄遍歷及文件批處理 ···.132
7.5 異常與程序的調試 ···················.134
7.5.1 程序錯誤的種類 ············.134
7.5.2 異常捕獲處理 ···············.135
7.5.3 程序的調試:斷點、跟蹤、
監(jiān)視 ···························.138
本章小結 ·····································.141
習題7 ·········································.142
第8 章 類與模塊 ························143
8.1 類的定義及使用 ······················.143
8.1.1 對象是屬性與方法的
封裝 ···························.143
8.1.2 類的定義 ·····················.144
8.1.3 魔術方法 ·····················.147
8.1.4 繼承 ···························.151
8.1.5 面向對象程序設計的三大
特點 ···························.152
8.2 模塊及包 ·······························.153
8.2.1 模塊 ···························.153
8.2.2 包 ······························.155
8.3 Python 標準庫簡介 ··················.157
8.3.1 數(shù)學、文本相關 ············.157
8.3.2 集合、統(tǒng)計、函數(shù)相關 ···.159
8.3.3 時間、系統(tǒng)、文件相關 ···.159
8.3.4 其他的“自帶電池” ·······.161
8.4 第三方庫簡介 ·························.161
8.4.1 安裝第三方庫 ···············.162
8.4.2 使用第三方庫 ···············.163
本章小結 ·····································.167
習題8 ·········································.167
第9 章 文本處理 ························168
9.1 字符串 ··································.168
9.1.1 字符串的運算 ···············.168
9.1.2 字符串的方法 ···············.170
9.2 正則表達式 ····························.173
9.2.1 正則表達式三要素:字符、
量詞、位置 ··················.173
9.2.2 正則表達式的測試 ·········.176
9.2.3 查找 ···························.178
9.2.4 匹配、替換及分割 ·········.180
9.3 文本統(tǒng)計 ·······························.182
9.3.1 字母、單詞、文字的
統(tǒng)計 ···························.182
9.3.2 N-Gram 的統(tǒng)計 ·············.183
9.4 自然語言處理 ·························.185
9.4.1 中文分詞 ·····················.185
9.4.2 情感分析 ·····················.188
本章小結 ·····································.189
習題9 ·········································.190
第10 章 網(wǎng)絡爬蟲及API 調用 ·······191
10.1 網(wǎng)絡信息獲取 ·······················.191
10.1.1 網(wǎng)絡信息獲取的相關
概念 ·························.191
10.1.2 使用urlopen() 函數(shù) ·······.194
10.1.3 使用requests 庫 ···········.196
10.2 網(wǎng)絡信息及內容解析 ··············.198
10.2.1 網(wǎng)絡信息的種類 ··········.198
10.2.2 網(wǎng)頁結構及HTML
標記 ·························.199
10.2.3 使用正則表達式處理
網(wǎng)頁 ·························.202
10.2.4 使用bs4 解析網(wǎng)頁 ········.202
10.3 網(wǎng)絡爬蟲 ·····························.206
10.3.1 通用的爬蟲程序 ··········.206
10.3.2 專用的爬蟲程序 ··········.208
10.4 API ······································211
10.4.1 JSON 數(shù)據(jù) ···················211
10.4.2 調用API ····················.213
本章小結 ·····································.217
習題10 ········································.217
第11 章 圖表展示及綜合應用 ········219
11.1 Matplotlib ·····························.219
11.1.1 Matplotlib 基本使用 ······.219
11.1.2 折線圖、散點圖、柱狀圖、
餅圖 ·························.222
11.1.3 繪制幾何圖形及函數(shù)
圖像 ·························.226
11.2 ECharts ································.229
11.2.1 pyecharts 基本使用 ·······.229
11.2.2 柱狀圖、折線圖、
餅圖 ·························.230
11.2.3 地圖 ·························.234
11.3 數(shù)據(jù)獲取、處理、展示的綜合
應用 ···································.237
11.3.1 《全宋詞》詞云 ············.237
11.3.2 財經(jīng)數(shù)據(jù)顯示 ·············.238
11.3.3 各地區(qū)好影片數(shù)量
分析 ·························.240
本章小結 ·····································.245
習題11 ········································.245
第12 章 圖形用戶界面 ·················246
12.1 Tkinter 圖形用戶界面 ··············.246
12.1.1 圖形用戶界面框架 ·······.246
12.1.2 組件 ·························.250
12.1.3 布局 ·························.252
12.1.4 事件 ·························.255
12.1.5 變量 ·························.257
12.2 Tkinter 應用 ··························.258
12.2.1 自動出題并判分 ··········.258
12.2.2 錄入人員信息 ·············.262
12.2.3 背單詞 ······················.264
12.2.4 顯示建議詞 ················.265
12.3 圖像處理 ·····························.266
12.3.1 Image 模塊及基本的圖像
處理 ·························.267
12.3.2 圖像過濾與增強 ··········.268
12.3.3 圖像的像素點計算 ·······.270
12.4 PyInstaller 打包應用程序 ·········.273
本章小結 ·····································.274
習題12 ········································.274
參考文獻·····································276