Python基础教程
发布时间:2024-04-07 23:04:16浏览次数:44Python基础教程(微课版)林志灿 涂晓彬 林智欣 主编高等院校计算机应用系列教材北 京Python基础教程(微课版) 文前.indd 1 2023-11-15 09:42:45
VIIIPython基础教程(微课版)14.6.1 编写表单 ·····························31614.6.2 通用视图 ·····························318 14.7 管理投票应用的静态资源 ·········· 32014.7.1 自定义应用界面和风格 ···········32014.7.2 管理静态资源 ·······················320 14.8 完善投票管理后台 ··················· 32114.8.1 修改后台表单 ·······················32114.8.2 修改字段列表 ·······················32514.8.3 更改后台界面和风格 ··············326 14.9 打包和发布投票系统 ················ 32814.9.1 重用的重要性 ·······················32814.9.2 打包项目和应用 ····················32814.9.3 安装和卸载自定义包 ··············33114.9.4 发布包 ································332 14.10 本章小结 ······························ 332 14.11 思考与练习 ··························· 332··········································333参考文献Python基础教程(微课版) 文前.indd 8 2023-11-15 09:42:46
初识 Python第1章Python语言伴随人工智能的兴起而得到快速蓬勃的发展,它是一种解释型的、面向对象的、动态数据类型的高级程序设计语言。像Perl语言一样,Python源代码同样遵循GPL协议。Python优雅的语法和动态类型,再结合它的解释性,使其在大多数平台的许多领域成为编写脚本或开发应用程序的理想语言。从云端、客户端到物联网终端,Python无处不在,它已成为人工智能首选的编程语言。本章的学习目标: 了解Python语言的起源与应用领域; 熟悉Python语言的版本,Python的下载、安装和启动; 了解Python常用的开发工具; 熟悉进行Python编程的几种方式; 了解Python初学者可能遇到的常见问题。 1.1 Python概述 Python是一门跨平台、开源、免费的解释型高级动态编程语言。除了解释执行,Python还支持伪编译,通过将源代码转换为字节码来优化程序、提高运行速度以及对源代码进行保密,并且支持使用py2exe、pyinstaller、cx_Freeze或其他类似工具将Python程序及其所有依赖库打包为扩展名为.exe的可执行程序,从而可以脱离Python解释器环境和相关依赖库而在Windows平台上独立运行;Python支持命令式编程、函数式编程,完全支持面向对象程序设计。也有人喜欢把Python语言称为“胶水语言”,因为它可以把多种不同语言编写的程序融合到一起实现无缝拼接,更好地发挥不同语言和工具的优势,满足不同应用领域的需求。目前Python的主流版本为Python 3.x。在选择Python版本时,一定要先考虑清楚使用Python来做哪方面的开发,有哪些扩展库可以使用,这些扩展库最高支持哪个版本的Python基础教程(微课版) 正文.indd 1 2023-11-15 09:41:22
2Python基础教程(微课版)2Python,等等,确定完之后再做出选择,这样就不会把时间浪费在Python和各种扩展库的反复安装上。1.1.1 Python起源1989年圣诞节期间,在阿姆斯特丹,Guido van Rossum为了打发圣诞节的无趣,决定开发一种新的脚本解释程序,作为ABC 语言的一种继承。之所以选用Python作为名称,是因为Guido本人是英国一个名叫Monty Python的喜剧团体的爱好者。ABC语言是由Guido参与设计的一种教学语言。就Guido本人看来,ABC这种语言非常优美和强大,是专门为非专业程序员设计的。但是ABC语言最终并没有成功,究其原因,Guido认为是其非开放性造成的。Guido决定在Python中避免这一错误。同时,他还想实现在ABC语言中闪现过但未曾实现的特性。就这样,Python诞生了。可以说,Python是从ABC语言发展而来的,主要受到Modula-3(另一种相当优美且强大的语言,为小型团体而设计)的影响,并且结合了UNIX shell和C语言的使用习惯。自2004年以后,Python的使用率呈线性增长。2011年1月,Python在TIOBE编程语言排行榜上被评为2010年度最受欢迎的语言。 由于Python语言的简洁性、易读性及可扩展性,在国外用Python做科学计算的研究机构日益增多,一些知名大学已采用Python来教授程序设计课程。例如卡内基-梅隆大学的编程基础、麻省理工学院的计算机科学及编程导论就使用Python语言讲授。众多开源的科学计算软件包都提供了Python的调用接口,例如著名的计算机视觉库OpenCV、三维可视化库VTK、医学图像处理库ITK。而Python专用的科学计算扩展库就更多了,例如NumPy、SciPy和matplotlib三个十分经典的科学计算扩展库,它们分别为Python提供快速数组处理、数值运算及绘图功能。因此,Python语言及其众多的扩展库所构成的开发环境十分适合工程技术、科研人员处理实验数据、制作图表,甚至开发科学计算应用程序。1.1.2 Python版本Python 作为一种语言,它也是随时间而逐步演进的: 早期版本的 Python 被称作是 Python 1; 在 2000 年,Python 2 的第一个版本发布,它目前仍在使用中; 2008 年 Python 3 的第一个版本发布,它是目前的最高版本。Python 2 于 2000 年 10 月 16 日发布,其最后一个版本是 2.7。Python 2.7 在 2020 年 1 月 1 日已无法得到 Python 社区的支持,所以其状态类似于 Windows XP 目前的状态。Python 3 于 2008 年 12 月 3 日发布,目前的版本是 3.11.2。Python 3 是目前最活跃的版本,基本上新开发的 Python 代码都会支持 Python 3。Python 4 是未来的版本,目前还处于萌芽状态,至今没有相关发布。本教程未涉及 Python 4 的相关内容。Python 3 和 Python 2 并不是完全兼容的,即在 Python 2 中可以运行的代码并不一定可Python基础教程(微课版) 正文.indd 2 2023-11-15 09:41:22
33第1章 初识 Python以在 Python 3 中运行。Python 社区意识到了这个问题,所以在 Python 3 中也提供了一些工具,如 2to3,这些工具可以帮助用户将 Python 2 编写的代码转换成 Python 3 编写的代码。现阶段来看,多数 Python 库都完成了向 Python 3 迁移的任务,本书的代码也将以 Python 3 为主。建议读者安装 Python 3.5 及以上版本来练习本书中的代码示例。1.1.3 Python应用1. 常规软件开发Python支持函数式编程和面向对象编程,能够承担任何类型软件的开发工作。因此,常规的软件开发、脚本编写、网络编程等都属于标配能力。2. 科学计算随着NumPy、SciPy、matplotlib等众多扩展库的开发,Python越来越适合用于科学计算、绘制高质量的2D和3D图形。与科学计算领域最流行的商业软件MATLAB相比,Python是一门通用的程序设计语言,比MATLAB所采用的脚本语言的应用范围更广,有更多的扩展库提供支持。虽然MATLAB中的许多高级功能和工具箱目前还是无法替代的,不过在日常的科研中仍然有很多的工作可由Python代劳。3. 自动化运维该功能几乎是Python应用的自留地。作为运维工程师首选的编程语言,Python在自动化运维方面已深入人心,比如Saltstack和Ansible都是大名鼎鼎的自动化平台。4. 云计算开源云计算解决方案OpenStack就是基于Python开发的。5. Web开发基于Python的Web开发框架有很多,比如耳熟能详的Django,还有Tornado和Flask。其中,Python+Django架构组合的应用范围非常广,开发速度非常快,学习门槛低,能够帮助开发人员快速搭建可用的Web服务。6. 网络爬虫网络爬虫也称网络蜘蛛,是大数据行业中获取数据的核心工具。没有网络爬虫自动地、不分昼夜地、高智能地在互联网上爬取免费的数据,那些大数据相关的公司恐怕要少四分之三。能够编写网络爬虫的编程语言有不少,但Python绝对是其中的主流之一,其Scrapy爬虫框架的应用非常广泛。7. 数据分析在大量数据的基础上,结合科学计算、机器学习等技术,对数据进行清洗、去重、规格化和有针对性的分析是大数据行业的基石。Python是进行数据分析的主流语言之一。8. 人工智能Python在人工智能大范畴领域内的机器学习、神经网络、深度学习等方面都是主流的编程语言,得到广泛的支持和应用。Python基础教程(微课版) 正文.indd 3 2023-11-15 09:41:22
4Python基础教程(微课版)4 1.2 搭建Python开发环境 在Windows、Linux、macOS操作系统上,都可以搭建Python开发环境。1.2.1 下载Python要搭建Python开发环境,首先必须下载和安装相应的工具。这些工具可免费下载。(1) 可以从Python官方网站下载安装包。(2) 也可以从网上下载ActivePython组件包。ActivePython是对Python核心模块和常用模块的二进制封装,是ActiveState公司发布的Python开发环境。ActivePython使得Python的安装更容易,并且可以应用在各种操作系统上。ActivePython包含一些常用的Python扩展,以及Windows环境下的编程接口。如果是Windows用户,下载msi包安装即可;如果是UNIX用户,下载tar.gz包直接解压即可。(3) Python的IDE具体包括PythonWin、Eclipse+PyDev插件、Komodo、EditPlus、PyCharm等。1.2.2 安装Python1. 在Windows操作系统上安装Python从官网的Windows发行版本列表中找到需要的安装程序,如图1-1所示。32位操作系统下载32-bit安装包,64位操作系统下载64-bit安装包。图1-1 Python下载界面Python基础教程(微课版) 正文.indd 4 2023-11-15 09:41:22
55第1章 初识 Python本书使用的是Python 3.11.2。单击Latest Python 3 Release - Python 3.11.2,打开下载界面,下拉界面至Files列表,然后单击Windows installer(64-bit)下载项开始下载,如图1-2 所示。图1-2 下载Python 3.11.2下载Python安装程序后,双击运行安装程序,初始界面如图1-3所示。选中底部的Add python.exe to PATH复选框,然后单击Install Now按钮。系统可能要求确认对系统所做的更改,单击OK按钮接受这些更改,进入安装界面,如图1-4所示。如果需要修改安装路径,可以单击Customize installation进行修改。图1-3 Python安装程序的初始界面 图1-4 Python安装界面在安装过程的最后,将看到如图1-5所示的界面,单击Close按钮关闭即可。2. 在Linux操作系统上安装Python在Linux操作系统上安装Python要简单得多,这里以Ubuntu Linux为例。Python在Ubuntu下有两种常用安装方法。 通过Ubuntu官方的apt工具包安装。图1-5 安装成功界面Python基础教程(微课版) 正文.indd 5 2023-11-15 09:41:23
6Python基础教程(微课版)6 通过编译Python源代码安装。例如,采用apt安装方式时,输入以下命令:sudo apt-get install python3.11.2apt将Python安装包下载到本地并自动进行安装。Python被默认安装到usr/local/lib/python311目录中。安装完毕后,可以直接输入python命令来查看Python版本号或是否安装成功。1.2.3 启动Python安装成功后,打开Windows的命令提示符窗口,输入python命令,即可显示当前Python的版本号,并进入Python交互模式,如图1-6所示。图1-6 在命令提示符窗口中启动Python在Python交互模式下可以直接输入python命令并执行。例如,输入以下命令:>>> print("hello")hello>>> print("hello world")hello world>>> x=3>>> y=4>>> x+y7命令提示符窗口如图1-7所示。图1-7 直接输入python命令执行在命令提示符窗口中使用交互模式执行python命令,只适用于测试功能。当关闭窗口时,所有输入的命令和执行结果均无法重现,因此,对于一些需要重复使用的代码,显得无能为力。因此,本书使用IDLE(Integrated Development Learning Environment,集成开发学习环境) 来讲解Python的功能,IDLE又称Eric Idle(Monty Python剧组的一位成员)。Python基础教程(微课版) 正文.indd 6 2023-11-15 09:41:23
77第1章 初识 PythonPython安装成功后,同时会安装IDLE。在Windows的【开始】菜单的所有程序中可以找到IDLE的启动项,如图1-8所示。单击IDLE(Python 3.11 64-bit)选项,打开IDLE窗口,如图1-9所示。图1-8 IDLE启动项 图1-9 IDLE窗口在IDLE中,可通过两种方式与Python交互:shell(输入的Python命令将立即执行)或文本编辑器(允许创建程序代码文档)。目前几乎所有的操作系统都支持IDLE。在macOS或Linux操作系统中,打开终端,输入idle,按下Enter键即可。shell内有引擎,IDLE程序包含Python引擎。Python引擎运行Python程序。与命令提示符窗口一样,IDLE Python Shell也可以执行输入的Python命令,将它们输入Python引擎,然后显示结果。1.2.4 多版本Python及虚拟环境的安装实际开发中,有可能需要用到不同的Python版本或版本库,这难免会涉及不同Python版本的安装以及不同扩展库的安装。本节以Windows环境为例,介绍多版本Python及虚拟环境的安装。1. 多版本Python的安装前面已经安装了Python 3.11.2,这里介绍一下Python 3.7.9的安装。(1) 首先从Python官网下载Python 3.7.9的安装包,然后双击运行,打开如图1-10所示的界面。(2) 单击Customize installation按钮,打开Optional Features界面,如图1-11所示。图1-10 安装Python 3.7.9的初始界面图1-11 Optional Features界面(3) 单击Next按钮,打开Advanced Options界面,设置安装路径,如图1-12所示。从中可以看出,不能添加路径到系统变量,先单击Install按钮进行安装,后面再专门解决。(4) 单击Next按钮进入安装过程,如图1-13所示。安装完毕后,单击Finish按钮完成安装。至此,Python 3.7.9已安装完成。Python基础教程(微课版) 正文.indd 7 2023-11-15 09:41:23
8Python基础教程(微课版)8图1-12 Advanced Options界面图1-13 安装Python 3.7.9(5) 此时运行cmd命令进入DOS命令窗口,输入python命令显示Python 3.11.2版本,因为没有把Python 3.7.9版本添加到环境变量中。下面来设置系统环境变量。打开【控制面板】|【系统和安全】|【系统】,单击【高级系统设置】按钮,如图1-14 所示。图1-14 单击【高级系统设置】按钮(6) 打开【系统属性】对话框,单击【环境变量】按钮,如图1-15所示。 图1-15 单击【环境变量】按钮(7) 打开【环境变量】对话框,如图1-16所示,在【系统变量】列表框中选择Path,然后单击【编辑】按钮。(8) 弹出“编辑环境变量”对话框,单击【新建】按钮,分别添加C:\Python\Python37和Python基础教程(微课版) 正文.indd 8 2023-11-15 09:41:23
99第1章 初识 PythonC:\Python\Python37\Scripts到环境变量中,如图1-17所示。图1-16 【环境变量】对话框 图1-17 添加路径(9) 找到Python的安装目录,分别将Python37和Python311子目录中python.exe和pythonw.exe的名称修改为python37.exe、pythonw37.exe和python311.exe、pythonw311.exe。然后运行cmd命令,输入python37即可运行Python 3.7.9版本,输入python311即可运行Python 3.11.2版本,如图1-18所示。图1-18 执行python37和python311命令(10) Python安装包需要用到包管理工具pip,但是当同时安装多版本Python时,pip只是其中一个版本,以下将提供一种修改方式,重新安装两个版本的pip,使得两个Python版本的pip能够共存。在DOS命令窗口中输入以下命令:python311 -m pip install --upgrade pip --force-reinstallpython37 -m pip install --upgrade pip --force-reinstall会显示重新安装成功。2. Python虚拟环境的安装在Windows操作系统中安装Python虚拟环境的步骤如下。(1) 安装virtualenv镜像,执行以下命令(pip3.11为Python311下的pip):pip3.11 install virtualenvPython基础教程(微课版) 正文.indd 9 2023-11-15 09:41:24
内 容 简 介本书专门针对Python新手量身定做,涵盖Python 3实际开发中经常用到的重要知识点, 内 容 主 要 包 括Python 语言的类型和对象、运算符和表达式、编程结构和控制流、函数、序列、多线程编程、正则表达式、面向对象编程、文件和目录操作、数据库编程、网络编程和邮件收发、Django 框架和项目范例。在介绍知识点的过程中,理论和实践相结合。书中还安排了不少实践示例,以帮助读者巩固所学,能够学以致用。本书内容丰富、结构合理、思路清晰、语言简洁流畅、示例翔实。本书可作为高等院校 Python 程序设计课程的教材,也可作为 Python Web 应用开发人员的参考资料。本书配套的电子课件、实例源文件、习题答案和思维导图可以到 http://www.tupwk.com.cn/downpage 网站下载,也可以扫描前言中的“配套资源”二维码获取。扫描前言中的“看视频”二维码可以直接观看教学视频。本书封面贴有清华大学出版社防伪标签,无标签者不得销售。版权所有,侵权必究。举报:010-62782989,beiqinquan@tup.tsinghua.edu.cn。图书在版编目(CIP)数据Python基础教程:微课版 / 林志灿,涂晓彬,林智欣主编. —北京:清华大学出版社,2023.9高等院校计算机应用系列教材ISBN 978-7-302-64581-8Ⅰ. ①P… Ⅱ. ①林… ②涂… ③林… Ⅲ. ①软件工具—程序设计—高等学校—教材 Ⅳ. ①TP311.561中国国家版本馆 CIP 数据核字 (2023) 第 180342 号责任编辑:胡辰浩封面设计:高娟妮版式设计:孔祥峰责任校对:成凤进责任印制:宋 林出版发行:清华大学出版社 网 址:https://www.tup.com.cn,https://www.wqxuetang.com 地 址:北京清华大学学研大厦 A 座 邮 编:100084 社 总 机:010-83470000 邮 购:010-62786544 投稿与读者服务:010-62776969,c-service@tup.tsinghua.edu.cn 质 量 反 馈:010-62772015,zhiliang@tup.tsinghua.edu.cn印 装 者:三河市龙大印装有限公司经 销:全国新华书店开 本:185mm×260mm 印 张:21.5 字 数:510 千字版 次:2023 年 11 月第 1 版 印 次:2023 年 11 月第 1 次印刷定 价:86.00 元——————————————————————————————————————————————产品编号:101493-01Python基础教程(微课版) 文前.indd 2 2023-11-15 16:02:49
10Python基础教程(微课版)10(2) 新建virtualenv,例如,在Python311安装目录下新建一个名为scrapytest的虚拟环境:virtualenv scrapytest(3) 使用cd命令进入C:\Python\Python311\scrapytest\Scripts目录,直接输入activate命令并执行,进入虚拟环境,如图1-19所示。进入虚拟环境,就可以运行Python进行测试了。(4) 当安装多个Python版本时,可以更改虚拟环境的Python版本,例如,要为虚拟环境更改Python版本到Python 3.7,命令如下:virtualenv -p C:\Python\Python37\python37.exe C:\Python\Python311\scrapytest(5) 当不需要使用虚拟环境时,可以退出虚拟环境,执行以下命令:deactivate.bat(6) 如果虚拟环境过多,管理起来会不太方便。这时,可以使用专门的虚拟环境管理包virtualenvwrapper进行管理,pip安装如下(此处调用的是Python311下的pip311):pip3.11 install virtualenvwrapper在Windows操作系统中安装如下:pip3.11 install virtualenvwrapper-win(7) 安装完毕后,在C:\Python\Python311下建立workon文件夹,然后设置环境变量WORKON_HOME为C:\Python\Python311\workon。设置完成后,可以使用virtualenvwrapper管理虚拟环境,这时新建虚拟环境的命令格式如下: mkvirtualenv virtual_name 例如,要新建一个名为py3scrapy的虚拟环境,命令如图1-20所示。(8) 要查看已安装的虚拟环境,可以执行workon命令,如图1-21所示。图1-20 使用virtualenvwrapper新建虚拟环境 图1-21 查看已安装的虚拟环境 1.3 Python开发环境的使用 “工欲善其事,必先利其器。”学习Python语言也一样,熟悉开发环境是学习一门编程语言的第一步。图1-19 进入虚拟环境Python基础教程(微课版) 正文.indd 10 2023-11-15 09:41:24
1111第1章 初识 Python1.3.1 使用自带的IDLEIDLE是Python的官方标准开发环境,从官方网站下载并安装合适的Python版本后,也就同时安装了IDLE。相对于其他Python开发环境而言,IDLE虽比较简单,但具备Python应用开发的几乎所有功能,且不需要进行复杂配置,界面如图1-22所示。图1-22 Python 3.11.2 IDLE的界面1.3.2 常用的第三方开发工具除了默认安装的IDLE,还有大量的其他开发环境,如wingIDE、PyCharm、PythonWin、Eclipse、Spyder、IPython、Komodo等。严格来说,所有这些开发环境都是对Python解释器python.exe的封装,核心是完全一样的,只是加了外挂而已。这些开发环境使用起来方便,减少了出错率,尤其是拼写错误。以PyCharm为例,界面如图1-23所示。图1-23 PyCharm开发环境1.3.3 官网交互式环境如果暂时什么都不想安装,只是简单地想试试Python语言的功能,可以试试Python官方网站提供的Interactive Shell。登录Python官方网站后,单击图1-24所示方框内的右箭头图标,稍等片刻即可进入如图1-25所示的界面。如果想尝试在安卓手机上编写Python程序,可以安装支持Python 3.x的QPython 3。 注意:Python基础教程(微课版) 正文.indd 11 2023-11-15 09:41:24
12Python基础教程(微课版)12图1-24 Python官方网站图1-25 Python官方网站提供的Interactive Shell界面 1.4 初学者常见的问题 1.4.1 为什么提示“python不是内部或 外部命令……”已经安装了Python,但是在DOS命令窗口中运行python命令时却提示“python不是内部或外部命令,也不是可运行的程序”。原因:在环境变量中未给Path添加值。 解决办法:打开环境变量,为系统变量中的Path变量添加Python安装路径,假如Python的安装路径为C:\Python\Python37,就将这个路径添加到系统环境变量中(参照前面1.2.2节的操作方法),然后再运行python命令。Python基础教程(微课版) 正文.indd 12 2023-11-15 09:41:24
1313第1章 初识 Python1.4.2 如何在Python交互模式下运行.py文件要运行已编写好的.py文件,可以单击【开始】菜单,在【搜索程序和文件】文本框中输入完整的文件名(包括路径)。例如,要运行D:\ceshi.py文件,可以使用下面的命令:python311 D:\ceshi.py在运行.py文件时,如果文件名或路径比较长,可以先在命令窗口中输入python加一个空格,然后直接把文件拖放到空格的位置,这时文件的完整路径将显示在空格的右侧,最后按下Enter键运行即可。 1.5 本章实战 1.5.1 IDLE的简单使用本节采用标准的IDLE作为开发环境来演示Python的强大功能,几乎所有代码都可以直接在其他开发环境中运行,不需要做任何修改。有时候可能需要同时安装多个不同的版本,例如,同时安装Python 3.11.2和Python 3.7.9,可以根据不同的开发需求在两个版本之间切换。多版本并存并不会影响在IDLE环境中直接运行程序,只需要启动相应版本的IDLE。在命令提示符环境中运行Python程序时,可在调用Python主程序时指定其完整路径,或者修改系统环境变量Path来实现不同版本之间的切换(Path系统环境变量的修改方法参考前面的内容)。如果能够熟练使用开发环境提供的一些快捷键,可以大幅提升开发效率。在IDLE环境中,除了撤销(Ctrl+Z)、全选(Ctrl+A)、复制(Ctrl+C)、粘贴(Ctrl+V)、剪切(Ctrl+X)等常规快捷键,其他比较常用的快捷键及功能说明如表1-1所示。表1-1 IDLE中的常用快捷键及功能说明快捷键 功能说明Tab 补全单词,列出全部可选单词供选择Alt+P 浏览历史命令(上一条)Alt+N 浏览历史命令(下一条)Ctrl+F6 重启Shell,之前定义的对象和导入的模块全部失效F1 打开Python帮助文档Alt+/ 自动补全前面曾经出现过的单词,如果之前有多个单词具有相同的前缀,则在多个单词间循环切换Ctrl+] 缩进代码块Ctrl+[ 取消代码块缩进Alt+3 注释代码块Alt+4 取消代码块注释Python基础教程(微课版) 正文.indd 13 2023-11-15 09:41:25
14Python基础教程(微课版)14启动Python后默认处于交互模式,直接在Python提示符“>>>”的后面输入相应命令并按Enter键即可执行这些命令。如果执行顺利,可立即看到执行结果,否则会提示有关错误的信息并抛出异常。例如:>>> 2+1 #井号为注释符,其后的内容不会被执行3>>> import math #导入Python标准库math>>> math.sqrt(25) #使用标准库函数sqrt计算平方根5.0>>> 3**2 #使用**进行幂运算9>>> 3*(1+9)30>>> 2/0 #除0错误,会抛出异常Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> 2/0ZeroDivisionError: division by zero>>> x='hello #语法错误,字符串的末尾缺少一个单引号SyntaxError: incomplete input从以上代码可以看出,交互模式一般用来实现一些简单的业务逻辑,或者验证某些功能。复杂的业务逻辑更多的是通过编写Python程序来实现,这样能方便代码的不断完善和重复利用。在IDLE界面中使用菜单命令File | New File创建一个程序文件,输入代码并保存为文件(务必保证扩展名为.py,如果是GUI程序,扩展名为.pyw)。然后,使用菜单命令Run | Run Module运行程序,程序运行的结果将直接显示在IDLE交互界面中。例如,假设程序文件test1.py的内容如下:def main(): print("this is a test program")main()在IDLE环境中运行该文件后,显示结果如下:======================= RESTART: D:/pyproject/test1.py =======================this is a test program在命令提示符环境中运行的方法和结果如图1-26所示。图1-26 在命令提示符环境中运行程序为提高代码的运行速度,以及对Python源代码进行保密,可以在命令提示符环境中使用python311 –OO –m py_compile file.py命令将Python程序file.py伪编译为.pyc文件,其中选项 –OO表示优化编译。 技巧:Python基础教程(微课版) 正文.indd 14 2023-11-15 09:41:25
1515第1章 初识 Python1.5.2 pip工具的使用Python语言中有三类库:内置库、标准库和扩展库。其中,内置库和标准库在Python安装成功后即安装。内置库不需要使用import命令导入就能直接使用;标准库和扩展库需要先导入才能使用。扩展库主要通过pip工具来管理。使用pip工具之前需要查看是否可用,打开命令提示符环境,输入 pip,如图1-27所示。如果pip工具不能使用,检查Python的安装目录,找到安装目录中的pip.exe文件,然后将其添加到系统环境变量Path中,之后重启再试。图1-27 pip工具常用的pip命令如下。 pip list:查看已安装的扩展库。 pip install package_name:安装名为package_name的扩展库。 pip uninstall package_name:卸载名为package_name的扩展库。例如,使用pip工具安装pandas库,命令如图1-28所示。图1-28 安装pandas库Python基础教程(微课版) 正文.indd 15 2023-11-15 09:41:25
16Python基础教程(微课版)161.5.3 初始化环境本书后面章节的示例讲解主要使用Python 3.11.2,为了使用方便,这里将Python 3.11.2安装目录下的python311.exe改回python.exe,pythonw311.exe改回pythonw.exe,如图1-29所示。图1-29 修改文件名打开命令提示符窗口,输入python命令,测试设置是否成功,如图1-30所示。然后重新执行pip命令:python -m pip install --upgrade pip --force-reinstall图1-30 测试设置 1.6 本章小结 本章作为开篇,首先介绍了Python语言的起源、Python的主流版本、Python语言的应用领域。Python是一门胶水语言,目前主流版本为Python 3.x。Python语言目前主要用于软件开发、科学计算、自动化运维、云计算、Web开发、网络爬虫、数据分析和人工智能等领域。在使用Python语言之前,需要先搭建Python开发环境。到Python官方网站下载匹配操作系统的安装包,一键安装即可。可以在同一台计算机上安装多个Python版本或通过Python虚拟环境来使用多版本的Python。可以通过pip工具来安装、卸载、更新Python扩展库。可以在官网的交互式环境、IDLE工具或者专门的IDE环境(如PyCharm软件)中编写、调试Python程序。 1.7 思考与练习 1. Python语言的主流版本及应用主要有哪些?2. 请实践在Windows操作系统上搭建Python开发环境。3. 如何在Windows操作系统上安装多版本Python?4. 如何安装Python虚拟环境?5. 如何在IDLE中创建Python程序并执行?6. 如何通过pip工具安装扩展库,以及如何在Python程序中使用扩展库?Python基础教程(微课版) 正文.indd 16 2023-11-15 09:41:25
Python是一种解释型的、面向对象的、带有动态语义的高级程序设计语言。在使用Python时,开发人员可以保持自己的代码风格,可以使用更清晰易懂的程序来实现想要的功能。对于一个没有任何编程经历的人来说,既简单又强大的Python就是完美的选择。随着云计算、大数据、人工智能等技术的迅速崛起,对Python人才的迫切需求和现实中Python人才的匮乏让长期沉默的Python语言瞬间备受青睐,本书作为教材,可以说是应运而生。随着技术的快速发展,Python的版本也在更新迭代,不断推出新版本,目前主流的版本为 Python 3系列,每推出一个Python新版本都会增加不少新特性。本书基于Python 3.11编写而成,可以满足想学习和了解Python最新版本及其特性的读者。本书专门为Python新手量身定做,是编者学习和使用Python过程中的体会和经验总结,涵盖实际开发中的所有重要知识点,内容详尽,代码可读性和可操作性强。本书主要介绍Python语言的类型和对象、运算符和表达式、编程结构和控制流、函数、序列、多线程编程、正则表达式、面向对象编程、文件和目录操作、数据库编程、网络操作和邮件收发等。在讲解每个知识点时,先讲解理论,后列举实际示例,各章还安排了习题,以帮助读者将所学应用到实际中,做到学以致用。本书的特色是,使用通俗易懂的描述和丰富的代码示例,提高本书的可读性,将复杂问题简化,使学习Python变得轻松。本书共分14章,各章内容安排如下。第1章主要介绍Python的起源、应用场合、前景以及Python 3的新特性。第2章主要介绍Python的基础知识,为后续章节学习相关内容做铺垫。第3章重点介绍字符和序列(列表、元组、集合等)。第4章重点介绍流程控制语句,主要包括分支结构、循环结构。第5章主要介绍正则表达式。第6章重点介绍函数。函数是组织好的、可重用的、用来实现单一或相关功能的代码段。第7章重点介绍面向对象编程技术。Python从设计之初就是一门面向对象的语言,提供了一些语言特性以支持面向对象编程。第8章重点介绍模块,从import语句开始介绍,然后逐步深入。第9章介绍如何处理各种异常和错误,以及创建和自定义异常类。第10章重点介绍如何使用Python在硬盘上创建、读取和保存文件,以及目录的创建、删除、遍历等。第11章主要介绍Python多线程编程。前 言Python基础教程(微课版) 文前.indd 1 2023-11-15 09:42:45
IIPython基础教程(微课版)第12章重点介绍Python数据库编程,并实现简单的增删改查操作。第13章重点介绍Python网络编程。第14章介绍如何使用Django框架创建一个投票管理系统,以及如何打包和发布该系统。本书分为14章,由闽南理工学院的林志灿、涂晓彬、林智欣合作编写完成。其中林志灿编写了第1、3、5、12、14章,涂晓彬编写了第2、4、6、8、11章,林智欣编写了第7、9、10、13章。由于作者水平有限,书中难免存在不足之处,欢迎广大读者批评指正。我们的信箱是992116@qq.com,电话是010-62796045。本书配套的电子课件、实例源文件、习题答案和思维导图可以到http://www.tupwk.com.cn/downpage网站下载,也可以扫描下方的“配套资源”二维码获取。扫描下方的“看视频”二维码可以直接观看教学视频。扫描下载配套资源扫一扫看视频编 者2023年9月Python基础教程(微课版) 文前.indd 2 2023-11-15 09:42:45
初识Python ······················· 1第 1 章 1.1 Python概述 ································· 11.1.1 Python起源 ··································21.1.2 Python版本 ··································21.1.3 Python应用 ··································3 1.2 搭建Python开发环境 ····················· 41.2.1 下载Python ··································41.2.2 安装Python ··································41.2.3 启动Python ··································61.2.4 多版本Python及虚拟环境的安装 ·······7 1.3 Python开发环境的使用 ················· 101.3.1 使用自带的IDLE ························111.3.2 常用的第三方开发工具 ················111.3.3 官网交互式环境 ·························11 1.4 初学者常见的问题 ······················· 121.4.1 为什么提示“python不是内部或 外部命令……” ·························121.4.2 如何在Python交互模式下 运行.py文件 ·······························13 1.5 本章实战 ··································· 131.5.1 IDLE的简单使用 ························131.5.2 pip工具的使用 ···························151.5.3 初始化环境 ·······························16 1.6 本章小结 ··································· 16 1.7 思考与练习 ································ 16Python语言基础 ··············· 17第 2 章 2.1 Python语法特点 ·························· 172.1.1 注释 ········································182.1.2 代码规范 ··································20 2.2 标识符与保留字 ·························· 222.2.1 标识符 ·····································222.2.2 保留字 ·····································23 2.3 使用变量 ··································· 242.3.1 变量的定义 ·······························242.3.2 变量的类型 ·······························25 2.4 基本数据类型 ····························· 252.4.1 数字类型 ··································252.4.2 字符串类型 ·······························272.4.3 布尔类型 ··································292.4.4 数据类型转换 ····························29 2.5 运算符 ······································ 302.5.1 算术运算符 ·······························302.5.2 比较运算符 ·······························322.5.3 赋值运算符 ·······························322.5.4 逻辑运算符 ·······························342.5.5 位运算符 ··································342.5.6 成员运算符 ·······························352.5.7 身份运算符 ·······························362.5.8 运算符的优先级 ·························36 2.6 基本输入输出 ····························· 382.6.1 使用input()函数输入 ····················382.6.2 使用print()函数输出 ·····················38 2.7 本章实战 ··································· 392.7.1 求和 ········································392.7.2 求平方根 ··································402.7.3 求水仙花数 ·······························412.7.4 判断素数 ··································42 2.8 本章小结 ··································· 43 2.9 思考与练习 ································ 44目 录Python基础教程(微课版) 文前.indd 3 2023-11-15 09:42:46
IVPython基础教程(微课版)字符与序列 ························45第 3 章 3.1 字符串的常见操作 ······················· 463.1.1 字符串长度的计算 ······················473.1.2 字母的大小写转换 ······················473.1.3 字符串的分隔 ····························483.1.4 字符串的拼接 ····························493.1.5 字符串查找 ·······························523.1.6 字符串替换 ·······························523.1.7 统计字符出现的次数 ···················533.1.8 去除字符串中的空格和特殊字符 ····533.1.9 格式化字符串 ····························543.1.10 encode()和decode()方法 ···············55 3.2 序列 ········································· 563.2.1 索引 ········································563.2.2 切片 ········································573.2.3 序列相加 ··································583.2.4 序列相乘 ··································593.2.5 检查某个元素是否是序列的成员 ····593.2.6 计算序列的长度、最大值和 最小值 ·····································60 3.3 列表序列 ··································· 603.3.1 删除列表元素 ····························613.3.2 访问列表元素 ····························623.3.3 更新与扩展列表 ·························623.3.4 对列表元素进行统计 ···················633.3.5 对列表进行排序 ·························643.3.6 列表推导式 ·······························65 3.4 元组 ········································· 673.4.1 元组的创建 ·······························673.4.2 访问元组元素 ····························693.4.3 连接元组 ··································693.4.4 删除元组 ··································693.4.5 元组的运算符 ····························703.4.6 生成器 ·····································703.4.7 元组与列表的区别 ······················71 3.5 字典 ········································· 723.5.1 字典的创建 ·······························723.5.2 访问字典 ··································723.5.3 修改字典 ··································733.5.4 删除字典元素 ····························733.5.5 字典的内置方法 ·························733.5.6 字典的遍历 ·······························74 3.6 集合 ········································· 753.6.1 集合的创建 ·······························753.6.2 集合的常见操作 ·························763.6.3 集合的内置方法 ·························78 3.7 本章实战 ··································· 79 3.8 本章小结 ··································· 80 3.9 思考与练习 ································ 81流程控制语句 ······················83第 4 章 4.1 分支结构 ··································· 834.1.1 单分支if结构 ·····························834.1.2 双分支if…else结构 ······················844.1.3 多分支if…elif…else结构 ···············84 4.2 循环结构 ··································· 854.2.1 while循环 ··································854.2.2 while死循环 ·······························864.2.3 while…else语句 ··························864.2.4 for循环 ·····································874.2.5 循环控制语句 ····························874.2.6 循环嵌套 ··································88 4.3 本章实战 ··································· 894.3.1 判断闰年 ··································894.3.2 使用snaps库制作数字闹钟 ·············90 4.4 本章小结 ··································· 90 4.5 思考与练习 ································ 90正则表达式 ·························92第 5 章 5.1 认识正则表达式 ·························· 925.1.1 元字符 ·····································935.1.2 预定义字符 ·······························935.1.3 特殊分组用法 ····························94 5.2 re模块中的常用功能函数··············· 955.2.1 re.compile函数 ····························955.2.2 re.match函数 ······························955.2.3 re.search函数 ······························965.2.4 re.findall函数 ······························975.2.5 re.finditer函数 ·····························975.2.6 re.split函数 ································98Python基础教程(微课版) 文前.indd 4 2023-11-15 09:42:46
V目 录5.2.7 re.sub函数 ·································985.2.8 re.subn函数 ································995.2.9 注意事项 ··································99 5.3 本章实战 ································· 100 5.4 本章小结 ································· 102 5.5 思考与练习 ······························ 102函数 ··································103第 6 章 6.1 函数的创建和调用 ····················· 1036.1.1 创建函数 ································1036.1.2 调用函数 ································104 6.2 参数传递 ································· 1066.2.1 不可变类型参数和可变类型参数 ··1066.2.2 参数形式 ································107 6.3 返回值 ·····································1116.3.1 return语句 ·······························1116.3.2 返回多个值 ·····························111 6.4 变量的作用域 ····························1126.4.1 全局变量和局部变量 ·················1136.4.2 global和nonlocal关键字 ··············113 6.5 匿名函数(lambda) ·······················114 6.6 Collatz序列 ·······························115 6.7 本章小结 ··································116 6.8 思考与练习 ·······························116面向对象编程 ····················117第 7 章 7.1 面向对象编程概述 ······················1177.1.1 面向对象编程中的术语介绍 ········1187.1.2 类的定义 ································1187.1.3 类的使用 ································1197.1.4 类的方法 ································120 7.2 深入介绍类 ······························ 1207.2.1 类的构造方法 ··························1217.2.2 类的访问权限 ··························124 7.3 封装 ······································· 129 7.4 继承与多态 ······························ 1327.4.1 类的单继承 ·····························1327.4.2 类的多继承 ·····························1337.4.3 构造函数的继承 ·······················1347.4.4 方法重写 ································1357.4.5 继承下的多态 ··························135 7.5 类的专有方法 ··························· 138 7.6 本章实战 ································· 144 7.7 本章小结 ································· 145 7.8 思考与练习 ······························ 146模块 ··································148第 8 章 8.1 模块 ······································· 1488.1.1 标准模块 ································1498.1.2 import语句 ······························1498.1.3 搜索路径 ································1508.1.4 from…import语句 ·····················1518.1.5 创建模块 ································1528.1.6 安装第三方模块 ·······················152 8.2 模块的高级技术 ························ 1538.2.1 __name__属性 ··························1538.2.2 dir函数 ···································154 8.3 Python中的包 ··························· 1558.3.1 包的定义 ································1558.3.2 包的导入 ································1558.3.3 包的组织 ································156 8.4 常用的内置模块 ························ 1568.4.1 collections ·······························1568.4.2 base64 ····································1598.4.3 struct ······································1608.4.4 hashlib ····································1618.4.5 itertools ··································1638.4.6 XML ······································1648.4.7 HTMLParser ····························166 8.5 本章实战 ································· 1668.5.1 创建模块 ································1668.5.2 安装模块 ································170 8.6 本章小结 ································· 171 8.7 思考与练习 ······························ 171异常处理和程序调试 ·········172第9 章 9.1 异常 ······································· 1739.1.1 错误与异常的概念 ····················1739.1.2 Python内置异常 ························1739.1.3 requests模块的相关异常 ·············175Python基础教程(微课版) 文前.indd 5 2023-11-15 09:42:46
VIPython基础教程(微课版)9.1.4 用户自定义异常 ·······················176 9.2 异常处理 ································· 1769.2.1 捕获所有异常 ··························1779.2.2 捕获指定异常 ··························1779.2.3 捕获多个异常 ··························1789.2.4 异常中的else ····························1789.2.5 异常中的finally ·························1789.2.6 使用raise语句主动抛出异常 ·········1799.2.7 使用traceback模块查看异常 ·········180 9.3 程序调试 ································· 1819.3.1 调试 ······································1819.3.2 断言 ······································1829.3.3 logging ···································1839.3.4 pdb ········································1839.3.5 pdb.set_trace() ··························1859.3.6 IDE ·······································186 9.4 单元测试 ································· 1869.4.1 单元测试概述 ··························1869.4.2 运行单元测试 ··························1889.4.3 setUp()与tearDown()方法 ············188 9.5 文档测试 ································· 189 9.6 本章小结 ································· 191 9.7 思考与练习 ······························ 191目录和文件操作 ················192第10 章 10.1 基本文件操作 ························· 19210.1.1 打开和关闭文件 ····················19210.1.2 文件模式 ·····························19410.1.3 缓冲 ···································195 10.2 基本文件方法 ························· 19610.2.1 读和写 ································19610.2.2 重命名 ································20010.2.3 序列化和反序列化 ·················201 10.3 目录操作 ······························· 20610.3.1 路径 ···································20710.3.2 目录内容 ·····························20910.3.3 获取文件信息 ·······················21010.3.4 重命名、移动、复制和删除 文件 ···································21110.3.5 创建和删除目录 ····················21110.3.6 文件通配符 ··························212 10.4 轮换文件 ······························· 213 10.5 本章小结 ······························· 214 10.6 思考与练习 ···························· 215多线程编程 ·······················216第11章 11.1 进程和线程 ···························· 21611.1.1 进程 ···································21611.1.2 线程 ···································21711.1.3 多进程和多线程 ····················217 11.2 使用线程 ······························· 21811.2.1 全局解释器锁 ·······················21811.2.2 退出线程 ·····························21911.2.3 Python的线程模块 ··················219 11.3 _thread模块 ···························· 219 11.4 threading模块 ·························· 22211.4.1 守护线程 ·····························22211.4.2 Thread对象 ···························223 11.5 线程同步 ······························· 227 11.6 Queue模块 ······························ 229 11.7 线程与进程的比较 ··················· 23011.7.1 线程切换 ·····························23111.7.2 计算密集型与IO密集型 ···········23111.7.3 异步IO ································232 11.8 本章实战 ······························· 23211.8.1 斐波那契数列、阶乘和加和 ·····23211.8.2 使用队列解决生产者/消费者 模型 ···································23411.8.3 子进程的使用 ·······················23511.8.4 进程池的使用 ·······················23611.8.5 多个子进程间的通信 ··············237 11.9 本章小结 ······························· 238 11.10 思考与练习 ··························· 239数据库编程 ·······················240第 12章 12.1 使用dbm创建持久字典 ·············· 24112.1.1 选择dbm模块 ························24112.1.2 创建持久字典 ·······················24112.1.3 访问持久字典 ·······················24212.1.4 dbm与关系数据库的适用场合 ···244Python基础教程(微课版) 文前.indd 6 2023-11-15 09:42:46
VII目 录 12.2 关系数据库与SQL ···················· 24412.2.1 SQL语言 ·····························24512.2.2 创建数据库 ··························24712.2.3 定义表 ································248 12.3 使用Python的DB API ················ 24912.3.1 下载DB API模块 ···················24912.3.2 创建连接 ·····························24912.3.3 数据库的CRUD操作 ···············25012.3.4 使用事务并提交结果 ··············25512.3.5 检查模块的功能和元数据 ········25612.3.6 处理错误 ·····························256 12.4 使用mysql-connector ················· 25712.4.1 连接MySQL数据库 ················25712.4.2 创建数据库 ··························25812.4.3 创建数据表 ··························25812.4.4 主键设置 ·····························25912.4.5 插入数据 ·····························26012.4.6 查询数据 ·····························26112.4.7 where条件语句 ······················26212.4.8 排序 ···································26312.4.9 limit语句 ·····························26412.4.10 删除记录 ···························26512.4.11 更新数据 ····························26512.4.12 删除数据表 ························266 12.5 本章小结 ······························· 267 12.6 思考和练习 ···························· 267网络编程 ···························270第 13 章 13.1 网络编程概述 ························· 270 13.2 TCP/IP简介 ···························· 27113.2.1 TCP/IP协议族概述 ·················27113.2.2 应用层 ································27113.2.3 传输层 ································27413.2.4 网络层 ································27513.2.5 IP地址与端口 ·······················276 13.3 发送电子邮件 ························· 27713.3.1 使用Python发送邮件 ··············27713.3.2 使用Python发送HTML格式的 邮件 ···································27913.3.3 使用Python发送带附件的邮件 ··28013.3.4 在HTML文本中添加图片 ········28113.3.5 使用第三方SMTP服务发送 邮件 ···································282 13.4 接收Internet邮件 ······················ 28313.4.1 通过POP3下载邮件 ················28313.4.2 解析邮件 ·····························284 13.5 套接字编程 ···························· 28613.5.1 TCP编程 ·····························28613.5.2 UDP编程 ·····························290 13.6 本章小结 ······························· 291 13.7 思考和练习 ···························· 291Django与投票管理系统 ·····292第14章 14.1 Web框架的功能 ······················· 29314.1.1 Web框架的基本功能 ···············29314.1.2 Web框架的其他通用功能 ·········293 14.2 Django框架的安装 ···················· 29414.2.1 Django框架的特点 ·················29414.2.2 Django框架的版本 ·················29414.2.3 在Windows下安装Django ·········295 14.3 使用Django框架 ······················· 29614.3.1 创建pyqi项目 ························29614.3.2 创建投票应用polls ·················29814.3.3 项目的目录结构 ····················29914.3.4 初步配置视图和urls ···············299 14.4 为pyqi项目创建数据库 ·············· 30114.4.1 为pyqi项目配置数据库 ············30114.4.2 为polls应用创建模型 ··············30214.4.3 为polls应用激活模型 ··············30314.4.4 测试生成的模型API ···············30514.4.5 使用Django管理界面 ··············307 14.5 完善投票应用的视图 ················ 31014.5.1 编写视图 ·····························31114.5.2 为视图添加模板 ····················31214.5.3 渲染模板 ·····························31314.5.4 抛出Http404异常 ···················31414.5.5 get_object_or_404() ················31414.5.6 为投票应用使用模板 ··············31514.5.7 为URL名称添加名称空间 ········315 14.6 为投票应用定制表单 ················ 316Python基础教程(微课版) 文前.indd 7 2023-11-15 09:42:46