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