没有合适的资源?快使用搜索试试~ 我知道了~
首页A bite of python
A bite of python
需积分: 10 23 下载量 107 浏览量
更新于2023-03-16
评论 8
收藏 594KB PDF 举报
'A Byte of Python' is a free book on programming using the Python language. It serves as a tutorial or guide to the Python language for a beginner audience. If all you know about computers is how to save text files, then this is the book for you. This book is written for the latest Python 3, even though Python 2 is the commonly found version of Python today
资源详情
资源评论
资源推荐
A Byte of Python
Swaroop C H
03 Dec 2012
Contents
1 A Byte of Python 8
1.1 Who Reads A Byte of Python? . . . . . . . . . . . . . . . . . . . 8
1.2 Academic Courses . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3 License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4 Read Now . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.5 Buy the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.6 Download . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.7 Read the book in your native language . . . . . . . . . . . . . . . 13
2 Preface 13
2.1 Who This Book Is For . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 History Lesson . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3 Status Of The Book . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4 Official Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.5 Something To Think About . . . . . . . . . . . . . . . . . . . . . 14
3 Introduction 15
3.1 Features of Python . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.1.1 Simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.1.2 Easy to Learn . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.1.3 Free and Open Source . . . . . . . . . . . . . . . . . . . . 15
3.1.4 High-level Language . . . . . . . . . . . . . . . . . . . . . 16
1
3.1.5 Portable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.1.6 Interpreted . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.1.7 Object Oriented . . . . . . . . . . . . . . . . . . . . . . . 16
3.1.8 Extensible . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.1.9 Embeddable . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.1.10 Extensive Libraries . . . . . . . . . . . . . . . . . . . . . . 17
3.2 Python 2 versus 3 . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.3 What Programmers Say . . . . . . . . . . . . . . . . . . . . . . . 18
4 Installation 18
4.1 Installation on Windows . . . . . . . . . . . . . . . . . . . . . . . 18
4.1.1 DOS Prompt . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.1.2 Running Python prompt on Windows . . . . . . . . . . . 20
4.2 Installation on Mac OS X . . . . . . . . . . . . . . . . . . . . . . 20
4.3 Installation on Linux . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5 First Steps 21
5.1 Using The Interpreter Prompt . . . . . . . . . . . . . . . . . . . . 21
5.2 Choosing An Editor . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.3 Using A Source File . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.3.1 Executable Python Programs . . . . . . . . . . . . . . . . 25
5.4 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
6 Basics 27
6.1 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
6.2 Literal Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
6.3 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
6.4 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
6.4.1 Single Quote . . . . . . . . . . . . . . . . . . . . . . . . . 29
6.4.2 Double Quotes . . . . . . . . . . . . . . . . . . . . . . . . 29
2
6.4.3 Triple Quotes . . . . . . . . . . . . . . . . . . . . . . . . . 29
6.4.4 Strings Are Immutable . . . . . . . . . . . . . . . . . . . . 29
6.4.5 The format method . . . . . . . . . . . . . . . . . . . . . 30
6.5 Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6.6 Identifier Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6.7 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
6.8 Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
6.9 How to write Python programs . . . . . . . . . . . . . . . . . . . 32
6.10 Example: Using Variables And Literal Constants . . . . . . . . . 32
6.10.1 Logical And Physical Line . . . . . . . . . . . . . . . . . . 33
6.10.2 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
7 Operators and Expressions 36
7.1 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
7.1.1 Shortcut for math operation and assignment . . . . . . . 38
7.2 Evaluation Order . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
7.3 Changing the Order Of Evaluation . . . . . . . . . . . . . . . . . 40
7.4 Associativity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
7.5 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
7.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
8 Control Flow 41
8.1 The if statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
8.2 The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . 44
8.3 The for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
8.4 The break Statement . . . . . . . . . . . . . . . . . . . . . . . . . 46
8.4.1 Swaroop’s Poetic Python . . . . . . . . . . . . . . . . . . 47
8.5 The continue Statement . . . . . . . . . . . . . . . . . . . . . . . 47
8.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3
9 Functions 48
9.1 Function Parameters . . . . . . . . . . . . . . . . . . . . . . . . . 49
9.2 Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
9.3 Using The global Statement . . . . . . . . . . . . . . . . . . . . . 51
9.4 Default Argument Values . . . . . . . . . . . . . . . . . . . . . . 52
9.5 Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 53
9.6 VarArgs parameters . . . . . . . . . . . . . . . . . . . . . . . . . 54
9.7 Keyword-only Parameters . . . . . . . . . . . . . . . . . . . . . . 55
9.8 The return Statement . . . . . . . . . . . . . . . . . . . . . . . . 56
9.9 DocStrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
9.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
10 Modules 58
10.1 Byte-compiled .pyc files . . . . . . . . . . . . . . . . . . . . . . . 60
10.2 The from . . . import statement . . . . . . . . . . . . . . . . . . . 60
10.3 A module’s name . . . . . . . . . . . . . . . . . . . . . . . . . . 61
10.4 Making Your Own Modules . . . . . . . . . . . . . . . . . . . . . 61
10.5 The dir function . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
10.6 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
10.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
11 Data Structures 65
11.1 List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
11.1.1 Quick Introduction To Objects And Classes . . . . . . . . 66
11.2 Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
11.3 Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
11.4 Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
11.5 Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
11.6 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
11.7 More About Strings . . . . . . . . . . . . . . . . . . . . . . . . . 75
11.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4
12 Problem Solving 76
12.1 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
12.2 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
12.3 Second Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
12.4 Third Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
12.5 Fourth Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
12.6 More Refinements . . . . . . . . . . . . . . . . . . . . . . . . . . 84
12.7 The Software Development Process . . . . . . . . . . . . . . . . . 85
12.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
13 Object Oriented Programming 85
13.1 The self . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
13.2 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
13.3 Object Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
13.4 The init method . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
13.5 Class And Object Variables . . . . . . . . . . . . . . . . . . . . . 89
13.6 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
13.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
14 Input Output 95
14.1 Input from user . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
14.2 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
14.3 Pickle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
14.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
15 Exceptions 99
15.1 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
15.2 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
15.3 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . 100
15.4 Raising Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 101
15.5 Try .. Finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
15.6 The with statement . . . . . . . . . . . . . . . . . . . . . . . . . 103
15.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
5
剩余128页未读,继续阅读
jk0833
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0