图书简介:
目录
第 1章初识 Python ·························································································· 1
1.1认识 Python ····························································································· 1
1.1.1 Python初识 ···················································································· 2
1.1.2 Python的特点及应用领域 ·································································· 2
1.1.3 Python的学习方法 ··········································································· 3
1.2搭建环境 ································································································· 4
1.2.1 Python下载 ···················································································· 5
1.2.2 Python IDLE开发环境 ······································································· 7
1.2.3 PyCharm开发环境 ··········································································· 8
1.2.4 Anaconda3开发环境 ········································································10
1.3打包发布 ································································································16
1.3.1 pip包管理工具 ···············································································16
1.3.2 PyInstaller······················································································19
1.4实训 1:使用环境搭建 ··············································································22【本章习题】 ··································································································22
第 2章编程基础 ·····························································································24
2.1认识 Python程序和中文编码 ······································································24
2.2 Python固定语法 ······················································································25
2.2.1 Python代码块 ················································································25
2.2.2 Python行处理 ················································································26
2.2.3 Python标识符及保留字 ····································································28
2.3 Python基本输入输出 ················································································30
2.3.1用户输入 ·······················································································30
2.3.2屏幕输出 ·······················································································32
2.4 Python变量和数据类型 ·············································································33
2.4.1变量和赋值 ····················································································34
基础教程
2.4.2简单数据类型 ·················································································35
2.4.3类型转换 ·······················································································40
2.5 Python的对象和变量 ················································································41
2.5.1对象的类型与身份 ···········································································42
2.5.2变量引用对象 ·················································································42
2.5.3共享引用 ·······················································································43
2.6 Python运算符 ·························································································45
2.6.1算术运算符 ····················································································45
2.6.2比较运算符 ····················································································47
2.6.3赋值运算符 ····················································································48
2.6.4位运算符 ·······················································································49
2.6.5逻辑运算符 ····················································································50
2.6.6成员运算符 ····················································································51
2.6.7身份运算符 ····················································································52
2.6.8运算符优先级 ·················································································52
2.7实训 2:班级竞选统计 ··············································································54【本章习题】 ··································································································56
第 3章控制流程 ·····························································································58
3.1条件语句 ································································································58
3.1.1 if条件语句 ····················································································59
3.1.2实践运用 ·······················································································62
3.2循环语句 ································································································63
3.2.1循环语句概述 ·················································································63
3.2.2 while循环语句 ···············································································63
3.2.3 for循环语句 ··················································································65
3.2.4循环嵌套 ·······················································································66
3.2.5实践运用 ·······················································································67
3.3其他语句 ································································································67
3.3.1 break语句 ·····················································································68
3.3.2 continue语句··················································································70
3.3.3 pass语句·······················································································71
3.3.4 else语句 ·······················································································71
3.4实训 3:数字金字塔 ·················································································73【本章习题】 ··································································································74
第 4章 Python数据类型 ···················································································76
4.1 Python序列操作 ······················································································76
4.2 Python字符串 ·························································································79
4.2.1 Python字符串操作 ··········································································80
目录
4.2.2字符串格式化输出 ···········································································83
4.2.3三引号 ··························································································85
4.2.4字符串函数 ····················································································86
4.3 Python列表 ····························································································95
4.3.1列表概述 ·······················································································96
4.3.2列表操作 ·······················································································96
4.3.3列表函数 ····················································································· 100
4.4 Python元组 ·························································································· 104
4.4.1元组概述 ····················································································· 104
4.4.2元组操作 ····················································································· 105
4.4.3元组函数 ····················································································· 108
4.5 Python字典 ·························································································· 109
4.5.1字典概述 ····················································································· 110
4.5.2字典操作 ······················································································111
4.5.3字典函数 ····················································································· 116
4.6 Python集合 ·························································································· 122
4.6.1集合概述 ····················································································· 122
4.6.2集合操作 ····················································································· 123
4.6.3集合函数 ····················································································· 126
4.7对象的浅复制与深复制 ··········································································· 127
4.7.1浅复制 ························································································ 128
4.7.2深复制 ························································································ 129
4.8推导式 ································································································· 129
4.8.1列表推导式 ·················································································· 130
4.8.2字典推导式 ·················································································· 131
4.8.3集合推导式 ·················································································· 132
4.9实训 4:创建学生信息管理系统用户数据 ···················································· 132【本章习题】 ································································································ 133
第 5章代码复用 —函数 ···············································································136
5.1 Python函数 ·························································································· 136
5.1.1函数概念 ····················································································· 137
5.1.2函数定义 ····················································································· 137
5.1.3函数调用 ····················································································· 138
5.1.4函数的嵌套定义 ············································································ 139
5.2 Python函数参数 ···················································································· 140
5.2.1参数··························································································· 140
5.2.2参数传递 ····················································································· 143
5.2.3函数返回值 ·················································································· 144
5.3 Python全局变量与局部变量 ····································································· 144
基础教程
5.3.1变量作用域 ·················································································· 145
5.3.2全局变量和局部变量 ······································································ 145
5.3.3 global语句 ·················································································· 146
5.3.4 nonlocal语句················································································ 147
5.4 Python匿名函数 ···················································································· 148 5.5实训 5:使用函数编写学生信息管理系统 ···················································· 149【本章习题】 ································································································ 152
第 6章代码复用 —模块 ···············································································154
6.1模块概述 ······························································································ 154
6.2模块的使用 ··························································································· 155
6.2.1模块导入 ····················································································· 156
6.2.2模块定位查找 ··············································································· 158
6.2.3自定义模块调用 ············································································ 159
6.3 Python中的包 ······················································································· 160
6.3.1包的概念 ····················································································· 160
6.3.2包使用实例 ·················································································· 161
6.4常用模块介绍 ······················································································· 162
6.4.1时间与日期 ·················································································· 163
6.4.2 math库······················································································· 166
6.4.3 json模块····················································································· 168
6.5实训 6:使用模块编写学生信息管理系统 ···················································· 170【本章习题】 ································································································ 173
第 7章 Python面向对象 ·················································································175
7.1面向对象编程简介 ················································································· 175
7.2类和对象 ······························································································ 177
7.2.1类的创建 ····················································································· 177
7.2.2实例对象 ····················································································· 179
7.2.3类的方法 ····················································································· 179
7.3类的继承 ······························································································ 182
7.4类的方法重写 ······················································································· 184
7.4.1方法重写 ····················································································· 185
7.4.2运算符重载 ·················································································· 186
7.5实训 7:使用类编写学生信息管理系统 ······················································· 187【本章习题】 ································································································ 190
第 8章文件处理 ···························································································192
8.1打开和关闭文件 ···················································································· 192
8.2文件读写 ······························································································ 196
目录
8.3重命名和删除文件 ················································································· 199
8.3.1 os模块 ······················································································· 199
8.3.2重命名和删除文件操作 ··································································· 202
8.4 Python文件夹操作 ················································································· 203
8.5 csv文件操作 ························································································· 204
8.5.1 csv数据······················································································ 205
8.5.2读取 csv数据··············································································· 205
8.5.3写入.csv文件··············································································· 208
8.6实训 8:基于文件的学生信息管理系统 ······················································· 209【本章习题】 ································································································ 212
第 9章异常处理 ···························································································214
9.1异常···································································································· 214
9.1.1异常概述··························································································· 215
9.1.2异常的检测与处理 ········································································· 217
9.2抛出异常 ······························································································ 221
9.2.1抛出异常处理 ··············································································· 221
9.2.2用户自定义异常 ············································································ 224
9.3 with语句 ····························································································· 225 9.4实训 9:实现异常处理的学生信息管理系统 ················································· 226【本章习题】 ································································································ 231
第 10章 MySQL数据库操作 ············································································233
10.1访问 MySQL数据库·············································································· 234
10.1.1 pymysql模块安装········································································ 234
10.1.2事务机制 ··················································································· 234
10.1.3 Python操作数据库过程及对象 ························································ 235
10.2数据库操作 ························································································· 236
10.2.1连接 MySQL数据库····································································· 236
10.2.2创建数据库表 ············································································· 237
10.2.3数据库读写 ················································································ 239
10.3实训 10:基于 MySQL数据库的学生信息管理系统 ······································ 243【本章习题】 ································································································ 247
第 11章计算生态 ··························································································249
11.1 Turtle标准库 ······················································································· 249
11.1.1 Turtle基础 ················································································· 250
11.1.2 Turtle绘图命令 ··········································································· 251
11.1.3 Turtle命令操作 ··········································································· 252
11.2 random标准库 ····················································································· 254
Python基础教程
11.2.1 random库介绍 ············································································ 255
11.2.2 random库常用函数 ······································································ 255
11.3 jieba第三方库 ······················································································ 257
11.3.1 jieba库简介 ················································································ 258
11.3.2 jieba库的安装 ············································································· 258
11.3.3 jieba分词应用 ············································································· 259
11.3.4 jieba单词统计 ············································································· 259
11.4 wordcloud第三方库 ·············································································· 260
11.4.1 wordcloud库安装 ········································································· 261
11.4.2 wordcloud基本使用 ······································································ 261
11.5实训 11:运行的时钟 ············································································ 265【本章习题】 ································································································ 267
附录全国计算机等级考试二级 Python语言程序设计考试大纲( 2018年版) ·············269
展开
前言
Python作为一门编程语言,已被应用在众多领域,如系统运维、图形处理、数学处理、文本处理、数据库编程、网络编程、 Web编程、多媒体应用、 pymo引擎、黑客编程、爬虫编写、机器学习、人工智能等, Python应用无处不在。
Python的设计哲学是“优雅、明确、简单”,它的语法清楚、干净、易读,程序易维护。编程简单直接,适合于初学编程者,让初学者专注于编程逻辑,而不是纠结于晦涩的语法细节。
为什么要学习本书
中国人工智能行业正处于一个创新发展的时期,对人才的需求也在急剧增长。国家相关教育部门对于“人工智能的普及”格外重视,不仅将 Python语言列入小学、中学和高中等教育体系中,并借此为未来国家和社会发展奠定了人工智能的人才培养基础,逐步由底层向高层推动“全民学 Python”,从而进一步实现人工智能技术的发展和社会人才结构的更迭。
随着大数据与人工智能时代的到来, Python已成为人们学习编程的首选语言。本教材力求为数据采集及分析提供全面的语言基础。作者根据“自主式一体化教学”模式,对教材的构成要素进行调整,重视学生的认知度、掌握度,按照 Python的有关知识由浅入深、从易到难进行编写,实现“教、学、做”一体化,从而提高了学生的持续发展能力。
通过对教材的学习,读者可学会运用 Python进行数据的处理,为数据采集及分析提供全面的语言基础。同时,教材也考虑到部分学有余力的同学参加全国计算机等级考试的要求,补充了全国计算机等级考试大纲规定的知识,因此也适合作为全国计算机等级考试 Python参考用书。
教材内容分布
教材基于 Python3,主要进行 Python基本语法、元组、列表、字典、文件的读写、函数与模块等 Python基础知识的讲授。具体章节内容如下。
第 1章主要是认识 Python。包括 Python发展历程、特点及应用领域,开发环境的搭建及程序的打包发布,并给出了 Python学习方法的建议。需要读者独立完成开发环境的搭建并了解程序打包发布的方法。
第 2章主要对 Python的基础语法进行讲解。包括中文编码、固定语法、标识符及保留字、基本输入输出、变量和数据类型、运算符等。读者在初学 Python时,须多动手写代码,这样才能加深印象,为后期深入学习打好基础。
第 3章主要介绍 Python的控制流程语句。包括条件语句、循环语句及其他语句。在开发中,须多加理解并掌握它们的使用。
基础教程
第 4章主要对 Python的数据类型进行了讲解。介绍了序列及序列操作、字符串、列表、元组、字典、集合、对象的浅复制与深复制、推导式等知识。读者需要掌握这些数据类型不同的特点及操作,以便在后续的开发中选择合适的类型对数据进行操作。
第 5章主要对函数进行了讲解。包括函数的定义及调用、参数及返回值、全局与局部变量、global与 nonlocal语句及匿名函数。函数作为关联功能的代码段,可以很好地提高代码的复用性。读者需要掌握函数的这些功能,也要能查询相关的函数手册或文档。
第 6章主要对 Python中的模块进行讲解。包括模块的制作使用、包及时间与日期、 math库、json模块这些常用的模块介绍。读者可以结合函数模块实现代码的封装,提高代码的可读性与可复用性,进一步熟悉导入 Python内置模块和第三方模块的方法,提高程序开发效率的能力。
第 7章主要介绍了面向对象编程的知识。包括面向对象编程概述、类和对象的创建、类的属性方法、类的继承、方法重写与运算符重载。读者通过本章的学习,培养使用面向对象思想进行程序设计的能力。
第 8章主要对文件操作进行讲解。包括文件打开和关闭、文件读写、文件重命名和删除、文件夹操作及 CSV文件操作等。通过本章的学习,读者能掌握文件的相关操作,能够使用相关方法来实现文件及文件数据集的操作。
第 9章主要对 Python中的异常进行处理。包括异常的介绍、系统内置异常的抛出和捕捉、用户自定义异常的处理、 with及 as语句的使用。通过本章的学习,读者能了解异常的处理,知道在程序中如何运行异常处理来提高程序的鲁棒性。
第 10章主要讲解了 MySQL数据库的操作。包括 pymysql模块安装、 Python操作数据库过程及对象、执行事务及错误处理,以及数据库操作。通过本章的学习,读者能掌握 Python操作 MySQL数据库的方法。
第 11章补充介绍了全国计算机等级考试二级考试中涉及的计算生态库,包括 turtle标准库、random标准库、 jieba第三方库及 wordcloud第三方库等。本章不作为基础必讲部分,可根据教学课时进行灵活安排。建议感兴趣的读者或计划参加二级 Python考试的读者认真学习。
在本书的学习中,读者在理解知识点的过程中遇到困难时,建议不要纠结于某个地方,可以继续往后学习。通常来说,通过逐渐深入的学习,前面不懂和疑惑的知识点会“豁然开朗”。在编程的学习中,一定要多动手实践。如果实践过程中碰到问题,可以停下来,整理思
路,认真分析问题发生的原因,并在问题解决后及时进行总结。另外,考虑到本书中实例代码有很多,所以约定全部变量都用正体,不区分正文和代码。本书中的实例代码、实训代码及每章习题中程序练习代码和素材可扫描左边的二维码免费下载,所提供的代码仅供参考。
本书编写成员
本书由周胜、鄢军霞、张松慧、王坤、朱永君负责编写,宋楚平负责审核。刘莉、杨艳、王禹参与了部分章节及视频等资料的制作整理工作。尽管编者付出了很多努力,在写作过程中,力求准确、完善,但书中难免会有不妥和错误之处,敬请读者批评指正。
编者 2019年 7月
展开