提取码:y4fb
内容简介
《21天学通Python》全面、系统、深入地讲解了Python编程基础语法与高级应用。在讲解过程中,通过大量实际操作的实例将Python语言知识全面、系统、深入地呈现给读者。此外,作者专门为本书录制了大量的配套教学视频,让读者通过分析实例、运行实例尽快熟悉Python编程,在学习中遇到问题时,也可以作为参考。
《21天学通Python》共分三篇。第一篇介绍Python语言的特点、安装、语法基础、程序流程控制、面向过程的编程方法、面向对象的编程方法、程序异常的处理;第二篇介绍了Python语言中的包与模块、迭代器、生成器、装饰器、上下文管理器等进阶语法,同时介绍了使用Python标准库中的文件系统处理、图形化界面编程、正则表达式、网络编程、多进(线)程编程、数据库编程。此外,还介绍了运用第三方库的Web网站编程、图片处理;第三篇通过两个案例介绍Pyth...
《21天学通Python》全面、系统、深入地讲解了Python编程基础语法与高级应用。在讲解过程中,通过大量实际操作的实例将Python语言知识全面、系统、深入地呈现给读者。此外,作者专门为本书录制了大量的配套教学视频,让读者通过分析实例、运行实例尽快熟悉Python编程,在学习中遇到问题时,也可以作为参考。
《21天学通Python》共分三篇。第一篇介绍Python语言的特点、安装、语法基础、程序流程控制、面向过程的编程方法、面向对象的编程方法、程序异常的处理;第二篇介绍了Python语言中的包与模块、迭代器、生成器、装饰器、上下文管理器等进阶语法,同时介绍了使用Python标准库中的文件系统处理、图形化界面编程、正则表达式、网络编程、多进(线)程编程、数据库编程。此外,还介绍了运用第三方库的Web网站编程、图片处理;第三篇通过两个案例介绍Python的综合编程技术。
《21天学通Python》在内容上涉及面广,从基本操作到高级技术及综合案例,涉及Python语言的基础语法和编程特性。《21天学通Python》在设计上浅显易懂、实例丰富,尤其适合广大编程初学者自学,也适合对Python语言感兴趣的爱好者作为参考用书。
作者简介
本书的作者团队成员为一线开发工程师、资深编程专家或专业培训师,在编程开发方面有着丰富的经验,并已出版过多本相关畅销书,颇受广大读者认可。长期积累的开发实战经验,结合对编程入门爱好者需求与特点的深入调查研究,本书的内容结构十分有利于编程爱好者和相关学员快速提高水平。
目录
第1章编程与Python.........................................................................................1
1.1什么是编程........................................................................................................1
1.1.1硬件与软件...........................................................................................................1
1.1.2编程语言...............................................................................................................2
1.1.3编程与调试...........................................................................................................3
· · · · · · (更多)
第1章编程与Python.........................................................................................1
1.1什么是编程........................................................................................................1
1.1.1硬件与软件...........................................................................................................1
1.1.2编程语言...............................................................................................................2
1.1.3编程与调试...........................................................................................................3
1.2选择Python的理由...........................................................................................4
1.2.1Python是免费的自由软件...................................................................................4
1.2.2Python是跨平台的...............................................................................................4
1.2.3Python功能强大...................................................................................................4
1.2.4Python清晰优雅...................................................................................................5
1.3安装Python........................................................................................................5
1.3.1在Windows下安装Python..................................................................................6
1.3.2在Linux下安装Python........................................................................................7
1.4选择开发工具....................................................................................................9
1.4.1Python自带开发工具:IDLE..............................................................................9
1.4.2文本编辑器:Emacs.............................................................................................9
1.4.3Python开发工具:PythonWin...........................................................................12
1.5编辑和运行Python程序.................................................................................15
1.5.1你好,Python......................................................................................................15
1.5.2运行程序文件........................................................................................................................15
1.5.3交互式运行Python.............................................................................................16
1.6小结..................................................................................................................17
1.7本章习题..........................................................................................................17
第2章Python起步...........................................................................................19
2.1Python语法基础..............................................................................................19
2.1.1缩进分层.............................................................................................................19
2.1.2代码注释.............................................................................................................20
2.1.3断行.....................................................................................................................20
2.2Python输入/输出.............................................................................................21
2.2.1接收键盘/输入....................................................................................................21
2.2.2显示处理结果........................................................................................................................22
2.3用Python计算.................................................................................................22
2.3.1算式与代数式运算..............................................................................................23
2.3.2惊奇.....................................................................................................................24
2.4小结..................................................................................................................25
2.5本章习题..........................................................................................................25
第3章Python数据类型......................................................................................................27
3.1Python简单数据类型......................................................................................27
3.1.1字符串(str)........................................................................................................................27
3.1.2整数(int).........................................................................................................30
3.1.3浮点数(float)..................................................................................................31
3.1.4类型转换.............................................................................................................31
3.2字符串进阶.....................................................................................................................32
3.2.1原始字符串.........................................................................................................32
3.2.2格式化字符串..............................................................................................................33
3.2.3中文字符串处理..................................................................................................33
3.3标志符与赋值号..............................................................................................34
3.3.1标志符.................................................................................................................34
3.3.2“=”.................................................................................................................34
3.4Python结构数据类型......................................................................................35
3.4.1列表(list).........................................................................................................35
3.4.2元组(tuple)........................................................................................................................37
3.4.3字典(dict).......................................................................................................37
3.5内置常量与逻辑运算符、比较运算符..........................................................39
3.5.1常用内置常量.................................................................................................39
3.5.2Python中逻辑运算符.........................................................................................39
3.5.3Python中比较运算符.........................................................................................40
3.5.4Python中其他逻辑操作符.................................................................................40
3.6序列..................................................................................................................41
3.6.1序列切片.............................................................................................................41
3.6.2序列内置操作..............................................................................................42
3.7小结..................................................................................................................43
3.8本章习题..........................................................................................................43
第4章控制语句执行流程.................................................................................45
4.1用if选择执行语句..........................................................................................45
4.1.1if基础..................................................................................................................45
4.1.2if语句的嵌套......................................................................................................48
4.2用for循环执行语句.......................................................................................50
4.2.1for基础................................................................................................................50
4.2.2for语句与break语句、continue语句...............................................................50
4.2.3for语句与range()函数.......................................................................................52
4.2.4for语句与内置迭代函数....................................................................................53
4.3用while循环执行语句...................................................................................54
4.3.1while基础.............................................................................................................54
4.3.2增量赋值运算符..................................................................................................55
4.4推导或内涵(listcomprehension)................................................................56
4.4.1推导基础.............................................................................................................56
4.4.2推导进阶.............................................................................................................56
4.5小结..................................................................................................................57
4.6本章习题..........................................................................................................57
第5章自定义功能单元(一)..........................................................................59
5.1使用函数..........................................................................................................59
5.1.1声明函数.............................................................................................................59
5.1.2调用函数.............................................................................................................60
5.2深入函数..........................................................................................................61
5.2.1默认值参数.........................................................................................................61
5.2.2参数传递.............................................................................................................63
5.2.3可变数量参数传递..............................................................................................63
5.2.4拆解序列的函数调用..........................................................................................66
5.2.5函数调用时参数的传递方法..............................................................................66
5.3变量的作用域..................................................................................................68
5.4使用匿名函数(lambda)..............................................................................69
5.5Python常用内建函数......................................................................................70
5.6小结..................................................................................................................71
5.7本章习题..........................................................................................................71
第6章自定义功能单元(二)..........................................................................73
6.1面向对象编程概述..........................................................................................73
6.1.1万物皆对象.........................................................................................................73
6.1.2对象优越性.........................................................................................................74
6.1.3类和对象.............................................................................................................74
6.2定义和使用类..................................................................................................74
6.2.1定义类.................................................................................................................74
6.2.2使用类.................................................................................................................75
6.3类的属性和方法..............................................................................................76
6.3.1类的方法.............................................................................................................76
6.3.2类的属性.............................................................................................................78
6.3.3类成员方法与静态方法......................................................................................80
6.4类的继承..........................................................................................................81
6.4.1类的继承.............................................................................................................81
6.4.2多重继承.............................................................................................................82
6.4.3方法重载.............................................................................................................84
6.5小结..................................................................................................................84
6.6本章习题..........................................................................................................84
第7章错误、异常和程序调试..........................................................................86
7.1语法错误..........................................................................................................86
7.2异常的处理......................................................................................................87
7.2.1异常处理的基本语法..........................................................................................87
7.2.2Python主要的内置异常及其处理......................................................................89
7.3手工抛出异常..................................................................................................91
7.3.1用raise手工抛出异常........................................................................................91
7.3.2assert语句...........................................................................................................92
7.3.3自定义异常类.........................................................................................93
7.4用pdb调试程序..............................................................................................94
7.4.1调试语句块函数..................................................................................................94
7.4.2调试函数.............................................................................................................95
7.5测试程序..........................................................................................................96
7.5.1用testmod函数测试...........................................................................................96
7.5.2用testfile函数测试.............................................................................................97
7.6小结..................................................................................................................98
7.7本章习题..........................................................................................................98
第二篇Python编程高阶
第8章复杂程序组织......................................................................................100
8.1模块................................................................................................................100
8.1.1模块概述...........................................................................................................100
8.1.2自己编写模块................................................................................................101
8.1.3模块位置...........................................................................................................102
8.1.4__pycache__目录..............................................................................................103
8.1.5具有独立运行能力的模块................................................................................103
8.2包....................................................................................................................104
8.2.1包概述...............................................................................................................104
8.2.2包详解...............................................................................................................105
8.3Python常用标准库简介................................................................................106
8.3.1数学类模块.......................................................................................................106
8.3.2日期与时间类.....................................................................................107
8.4小结................................................................................................................107
8.5本章习题........................................................................................................107
第9章迭代器、生成器与装饰器....................................................................109
9.1迭代器............................................................................................................109
9.1.1迭代器概述.......................................................................................................109
9.1.2自定义迭代器...............................................................................110
9.1.3内置迭代器工具................................................................................................110
9.2生成器............................................................................................................113
9.2.1生成器创建.......................................................................................................113
9.2.2深入生成器.......................................................................................................114
9.2.3生成器与协程...............................................................................115
9.3装饰器............................................................................................................116
9.3.1装饰器概述.......................................................................................................116
9.3.2装饰函数...........................................................................................................116
9.3.3装饰类...............................................................................................................117
9.4小结................................................................................................................118
9.5本章习题........................................................................................................119
第10章Python进阶话题..................................................................................................120
10.1函数与命名空间..........................................................................................120
10.2闭包及其应用..............................................................................................121
10.2.1闭包概述.........................................................................................................121
10.2.2闭包与延迟求值..............................................................................................122
10.2.3闭包与泛型函数..............................................................................................122
10.3上下文管理器..............................................................................................123
10.4用字符串操作对象属性..............................................................................125
10.5用字典构造分支程序..................................................................................126
10.6重载类的特殊方法......................................................................................127
10.7鸭子类型(ducktyping)与多态...............................................................128
10.8小结..............................................................................................................129
10.9本章习题......................................................................................................129
第11章文件与文件系统.................................................................................131
11.1文件操作基础..............................................................................................131
11.1.1open()函数.......................................................................................................131
11.1.2用fileinput操作文件......................................................................................133
11.2常用文件和目录操作..................................................................................134
11.2.1获得当前路径..................................................................................................134
11.2.2获得目录中的内容..........................................................................................134
11.2.3创建目录..........................................................................................................135
11.2.4删除目录..........................................................................................................135
11.2.5判断是否是目录..............................................................................................135
11.2.6判断是否为文件..............................................................................................135
11.2.7遍历某目录下的所有文件和目录..................................................................136
11.2.8由文件名批量获取姓名和考号......................................................................136
11.2.9批量文件重命名..............................................................................................137
11.3编译为可执行文件......................................................................................138
11.3.1用py2exe生成可执行程序............................................................................138
11.3.2用cx_freeze生成可执行文件........................................................................139
11.4小结..............................................................................................................141
11.5本章习题......................................................................................................141
第12章基于tkinter的GUI编程....................................................................142
12.1GUI概述......................................................................................................142
12.1.1GUI是什么.....................................................................................................142
12.1.2Python编写GUI程序库................................................................................142
12.2tkinter图形化库简介...................................................................................143
12.2.1创建GUI程序第一步.....................................................................................143
12.2.2创建GUI程序第二步.....................................................................................144
12.3tkinter组件.....................................................................................................................145
12.3.1组件分类.........................................................................................................145
12.3.2布局组件.........................................................................................................145
12.4常用tkinter组件..........................................................................................146
12.4.1按钮.................................................................................................................146
12.4.2文本框.............................................................................................................148
12.4.3标签.................................................................................................................149
12.4.4菜单.................................................................................................................150
12.4.5单选框和复选框..............................................................................................152
12.4.6绘制图形.........................................................................................................154
12.5响应操作事件..............................................................................................156
12.5.1事件基础.........................................................................................................156
12.5.2响应事件.........................................................................................................158
12.6对话框..........................................................................................................160
12.6.1标准对话框.....................................................................................................160
12.6.2自定义对话框..................................................................................................166
12.7小结..............................................................................................................167
12.8本章习题......................................................................................................167
第13章正则表达式........................................................................................169
13.1正则表达式基础..........................................................................................169
13.1.1正则表达式概述..............................................................................................169
13.1.2正则表达式基本元字符..................................................................................169
13.1.3常用正则表达式..............................................................................................171
13.2re模块..........................................................................................................172
13.2.1正则匹配搜索函数..........................................................................................172
13.2.2sub()与subn()函数..........................................................................................173
13.2.3split()函数........................................................................................................174
13.2.4正则表达式对象..............................................................................................174
13.3分组匹配与匹配对象使用..........................................................................178
13.3.1分组基础.........................................................................................................178
13.3.2分组扩展.........................................................................................................178
13.3.3匹配对象与组的使用......................................................................................179
13.3.4匹配对象与索引使用......................................................................................180
13.4正则表达式应用示例..................................................................................181
13.5小结..............................................................................................................183
13.6本章习题......................................................................................................183
第14章网络编程............................................................................................184
14.1网络编程基础..............................................................................................184
14.1.1什么是网络.....................................................................................................184
14.1.2网络协议.........................................................................................................184
14.1.3地址与端口.....................................................................................................185
14.2套接字的使用..............................................................................................186
14.2.1用socket建立服务器端程序..........................................................................186
14.2.2用socket建立客户端程序..............................................................................187
14.2.3用socket建立基于UDP协议的服务器与客户端程序................................188
14.2.4用socketserver模块建立服务器....................................................................190
14.3urllib与http包使用....................................................................................191
14.3.1urllib和http包简介........................................................................................191
14.3.2用urllib和http包访问网站...........................................................................194
14.4用poplib与smtplib库收发邮件................................................................195
14.4.1用poplib检查邮件.........................................................................................195
14.4.2用smtplib发送邮件........................................................................................197
14.5用ftplib访问FTP服务...............................................................................199
14.5.1ftplib模块简介................................................................................................199
14.5.2使用Python访问FTP....................................................................................201
14.6小结..............................................................................................................203
14.7本章习题......................................................................................................203
第15章线程和进程........................................................................................204
15.1线程..............................................................................................................204
15.1.1用threading.Thread直接在线程中运行函数.................................................204
15.1.2通过继承threading.Thread类来创建线程.....................................................205
15.1.3线程类Thread使用........................................................................................205
15.2进程..............................................................................................................209
15.2.1进程基础.........................................................................................................209
15.2.2用Popen类创建进程......................................................................................210
15.3小结..............................................................................................................212
15.4本章习题......................................................................................................212
第16章数据库编程........................................................................................213
16.1Python数据库应用程序接口......................................................................213
16.1.1数据库应用程序接口概述..............................................................................213
16.1.2数据库游标的使用..........................................................................................214
16.2Python操作SQLite3数据库......................................................................214
16.2.1SQLite3数据库简介.......................................................................................214
16.2.2SQLite3数据库操作实例...............................................................................215
16.3Python操作MariaDB数据库.....................................................................217
16.3.1MariaDB数据库简介......................................................................................218
16.3.2建立MariaDB数据库操作环境.....................................................................218
16.3.3MariaDB数据库操作实例..............................................................................220
16.4Python操作MongoDB数据库...................................................................222
16.4.1MongoDB数据库简介....................................................................................222
16.4.2建立MongoDB数据库操作环境...................................................................222
16.4.3MongoDB数据库基础....................................................................................223
16.4.4MongoDB数据库操作实例............................................................................226
16.4.5用对象关系映射(ORM)工具操作MongoDB数据库..............................228
16.5小结..............................................................................................................231
16.6本章习题......................................................................................................231
第17章Web网站编程...................................................................................232
17.1Web网站编程概述......................................................................................232
17.2FlaskWeb框架及其应用............................................................................233
17.2.1FlaskWeb框架简介........................................................................................233
17.2.2FlaskWeb框架初识........................................................................................233
17.2.3URL装饰器与URL参数传递.......................................................................235
17.2.4GET与POST请求的参数传递.....................................................................237
17.2.5使用cookie与session跟踪客户....................................................................239
17.2.6使用静态文件资源与页面文件......................................................................241
17.2.7接收上传文件..................................................................................................243
17.2.8Flask框架中使用数据库................................................................................244
17.3TornadoWeb框架及其应用........................................................................247
17.3.1Tornado框架简介...........................................................................................247
17.3.2Tornado框架初识...........................................................................................247
17.3.3请求参数的获取..............................................................................................249
17.3.4用cookie与安全cookie跟踪客户.................................................................251
17.3.5URL转向与静态文件资源.............................................................................252
17.3.6TornadoWeb框架应用举例...........................................................................254
17.4小结..............................................................................................................259
17.5本章习题......................................................................................................259
第18章数据结构基础....................................................................................261
18.1表、栈和队列..............................................................................................261
18.1.1用列表来创建表..............................................................................................261
18.1.2自定义栈数据结构..........................................................................................262
18.1.3实现队列功能..................................................................................................264
18.2树和图..........................................................................................................265
18.2.1用列表构建树..................................................................................................265
18.2.2实现二叉树类与遍历二叉树..........................................................................266
18.2.3用字典构建与搜索图......................................................................................269
18.3查找与排序..................................................................................................271
18.3.1实现二分查找..................................................................................................271
18.3.2用二叉树排序..................................................................................................273
18.4小结..............................................................................................................275
18.5本章习题......................................................................................................275
第19章用Pillow库处理图片.........................................................................276
19.1第三方Pillow库概述..................................................................................276
19.1.1安装第三方Pillow库.....................................................................................276
19.1.2Pillow库简介..................................................................................................276
19.1.3Pillow库处理图像基础..................................................................................277
19.1.4Image模块中函数的使用...............................................................................278
19.1.5Image模块中Image类的使用.......................................................................281
19.1.6使用ImageChops模块进行图片合成............................................................286
19.1.7使用ImageEnhance模块增强图像效果........................................................289
19.1.8使用ImageFilter模块的滤镜.........................................................................290
19.1.9使用ImageDraw模块画图.............................................................................291
19.2使用Pillow库处理图片举例......................................................................292
19.2.1图片格式转换..................................................................................................292
19.2.2批量生成缩略图..............................................................................................294
19.2.3为图片添加Logo............................................................................................297
19.3小结..............................................................................................................301
19.4本章习题......................................................................................................302
第三篇Python编程实战
第20章案例1做一个Windows上的360工具...........................................303
20.1案例背景......................................................................................................303
20.2从创建图形化界面开始..............................................................................304
20.2.1创建基本图形化工作界面..............................................................................304
20.2.2响应菜单事件..................................................................................................306
20.3清理垃圾文件..............................................................................................308
20.3.1迭代目录.........................................................................................................308
20.3.2扫描垃圾文件..................................................................................................309
20.3.3多线程加速.....................................................................................................311
20.3.4扫描所有磁盘..................................................................................................312
20.3.5删除垃圾文件..................................................................................................314
20.4搜索文件......................................................................................................316
20.4.1搜索超大文件..................................................................................................316
20.4.2按名称搜索文件..............................................................................................317
20.5小结..............................................................................................................318
第21章案例2Python搞定大数据..............................................................320
21.1案例背景......................................................................................................320
21.1.1大数据处理方式概述......................................................................................320
21.1.2处理日志文件..................................................................................................321
21.1.3要实现的案例目标..........................................................................................322
21.2分割日志文件..............................................................................................322
21.3用Map函数处理小文件.............................................................................324
21.4用Reduce函数归集数据............................................................................326
21.5小结..............................................................................................................327