没有合适的资源?快使用搜索试试~ 我知道了~
首页Python Crash Course:动手实践项目编程入门指南
"《Python Crash Course》是由Eric Matthes编著的一本实用且项目导向的编程入门书籍。这本书专为那些希望快速掌握Python编程语言的新手设计,通过实践操作和实际项目来引导读者步入编程的世界。作者以其丰富的经验和深入浅出的方式,让学习者在互动式的学习过程中逐步建立对编程概念的理解。 本书的核心理念是"动手做"(Hands-On, Project-Based),强调理论与实践相结合的重要性。它将Python编程的基础知识分解成一系列易于理解和操作的模块,涵盖了数据类型、控制流程、函数、类和对象、模块和包等核心内容,适合初学者循序渐进地学习。每个章节后,都有配套的实战项目,让读者有机会将所学应用到实际问题解决中,培养解决问题的能力。 书中还特别关注代码可读性和编程规范,教育读者如何写出清晰、高效的Python代码。此外,作者还分享了编程思维的培养,帮助读者理解程序设计的基本原则和逻辑结构,以便于在未来面对更复杂的项目时能够游刃有余。 《Python Crash Course》不仅是一本教材,也是一份宝贵的资源库,提供了大量的实例和案例分析,适合自学和团队学习。版权方面,该书受到严格的保护,未经许可不得任何形式复制或传播。No Starch Press出版社出版,包括了详细的作者信息、编辑团队以及联系方式,便于读者寻求进一步的支持和咨询。 《Python Crash Course》是一本全面而实用的Python编程入门指南,无论你是初次接触编程还是希望提升技能,都将为你提供一个扎实的起点,让你在探索编程世界的过程中更加得心应手。"
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/10815166/bg10.jpg)
Contents in Detail xv
A Dictionary in a Dictionary ................................... 113
Exercise 6-7: People .........................................114
Exercise 6-8: Pets ...........................................115
Exercise 6-9: Favorite Places ...................................115
Exercise 6-10: Favorite Numbers ................................115
Exercise 6-11: Cities .........................................115
Exercise 6-12: Extensions......................................115
Summary ...................................................... 115
7
USER INPUT AND WHILE LOOPS 117
How the input() Function Works ....................................... 118
Writing Clear Prompts ....................................... 118
Using int() to Accept Numerical Input............................. 119
The Modulo Operator ....................................... 120
Accepting Input in Python 2.7.................................. 121
Exercise 7-1: Rental Car...................................... 121
Exercise 7-2: Restaurant Seating ................................ 121
Exercise 7-3: Multiples of Ten .................................. 121
Introducing while Loops ............................................ 122
The while Loop in Action ..................................... 122
Letting the User Choose When to Quit ............................ 122
Using a Flag.............................................. 124
Using break to Exit a Loop .................................... 125
Using continue in a Loop ..................................... 126
Avoiding Infinite Loops ...................................... 126
Exercise 7-4: Pizza Toppings .................................. 127
Exercise 7-5: Movie Tickets.................................... 127
Exercise 7-6: Three Exits ..................................... 128
Exercise 7-7: Infinity ........................................ 128
Using a while Loop with Lists and Dictionaries ............................. 128
Moving Items from One List to Another............................ 128
Removing All Instances of Specific Values from a List .................. 129
Filling a Dictionary with User Input .............................. 130
Exercise 7-8: Deli .......................................... 131
Exercise 7-9: No Pastrami .................................... 131
Exercise 7-10: Dream Vacation................................. 131
Summary ...................................................... 131
8
FUNCTIONS 133
Defining a Function ............................................... 134
Passing Information to a Function ............................... 134
Arguments and Parameters.................................... 135
Exercise 8-1: Message....................................... 135
Exercise 8-2: Favorite Book ................................... 135
Passing Arguments ................................................ 135
Positional Arguments ........................................ 136
Keyword Arguments ........................................ 137
Default Values ............................................ 138
![](https://csdnimg.cn/release/download_crawler_static/10815166/bg11.jpg)
xvi Contents in Detail
Equivalent Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Avoiding Argument Errors .................................... 140
Exercise 8-3: T-Shirt ......................................... 141
Exercise 8-4: Large Shirts ..................................... 141
Exercise 8-5: Cities ......................................... 141
Return Values.................................................... 141
Returning a Simple Value ..................................... 142
Making an Argument Optional ................................. 142
Returning a Dictionary ....................................... 144
Using a Function with a while Loop .............................. 145
Exercise 8-6: City Names..................................... 146
Exercise 8-7: Album......................................... 146
Exercise 8-8: User Albums .................................... 146
Passing a List.................................................... 147
Modifying a List in a Function .................................. 147
Preventing a Function from Modifying a List ........................ 149
Exercise 8-9: Magicians...................................... 150
Exercise 8-10: Great Magicians ................................ 150
Exercise 8-11: Unchanged Magicians ............................ 150
Passing an Arbitrary Number of Arguments............................... 151
Mixing Positional and Arbitrary Arguments ........................ 152
Using Arbitrary Keyword Arguments ............................. 152
Exercise 8-12: Sandwiches.................................... 154
Exercise 8-13: User Profile .................................... 154
Exercise 8-14: Cars......................................... 154
Storing Your Functions in Modules ..................................... 154
Importing an Entire Module ................................... 154
Importing Specific Functions ................................... 156
Using as to Give a Function an Alias............................. 156
Using as to Give a Module an Alias ............................. 157
Importing All Functions in a Module ............................. 157
Styling Functions ................................................. 158
Exercise 8-15: Printing Models ................................. 159
Exercise 8-16: Imports ....................................... 159
Exercise 8-17: Styling Functions ................................ 159
Summary ...................................................... 159
9
CLASSES 161
Creating and Using a Class.......................................... 162
Creating the Dog Class ...................................... 162
Making an Instance from a Class ............................... 164
Exercise 9-1: Restaurant...................................... 166
Exercise 9-2: Three Restaurants ................................. 166
Exercise 9-3: Users ......................................... 166
Working with Classes and Instances.................................... 167
The Car Class............................................. 167
Setting a Default Value for an Attribute ........................... 168
Modifying Attribute Values .................................... 168
Exercise 9-4: Number Served .................................. 171
Exercise 9-5: Login Attempts ................................... 171
![](https://csdnimg.cn/release/download_crawler_static/10815166/bg12.jpg)
Contents in Detail xvii
Inheritance ..................................................... 172
The __init__() Method for a Child Class ........................... 172
Inheritance in Python 2.7 ..................................... 173
Defining Attributes and Methods for the Child Class .................. 174
Overriding Methods from the Parent Class ......................... 175
Instances as Attributes ....................................... 175
Modeling Real-World Objects ................................. 177
Exercise 9-6: Ice Cream Stand ................................. 178
Exercise 9-7: Admin ........................................ 178
Exercise 9-8: Privileges ...................................... 178
Exercise 9-9: Battery Upgrade ................................. 178
Importing Classes................................................. 179
Importing a Single Class ..................................... 179
Storing Multiple Classes in a Module............................. 180
Importing Multiple Classes from a Module ......................... 181
Importing an Entire Module ................................... 182
Importing All Classes from a Module ............................. 182
Importing a Module into a Module .............................. 183
Finding Your Own Workflow .................................. 184
Exercise 9-10: Imported Restaurant .............................. 184
Exercise 9-11: Imported Admin ................................. 184
Exercise 9-12: Multiple Modules ................................ 184
The Python Standard Library ......................................... 184
Exercise 9-13: OrderedDict Rewrite.............................. 186
Exercise 9-14: Dice ......................................... 186
Exercise 9-15: Python Module of the Week ........................ 186
Styling Classes................................................... 186
Summary ...................................................... 187
10
FILES AND EXCEPTIONS 189
Reading from a File ............................................... 190
Reading an Entire File ....................................... 190
File Paths ................................................ 191
Reading Line by Line ........................................ 193
Making a List of Lines from a File ............................... 194
Working with a File’s Contents ................................. 194
Large Files: One Million Digits ................................. 195
Is Your Birthday Contained in Pi? ............................... 196
Exercise 10-1: Learning Python ................................. 197
Exercise 10-2: Learning C .................................... 197
Writing to a File.................................................. 197
Writing to an Empty File ..................................... 197
Writing Multiple Lines ....................................... 198
Appending to a File......................................... 199
Exercise 10-3: Guest ........................................ 199
Exercise 10-4: Guest Book .................................... 199
Exercise 10-5: Programming Poll................................ 199
Exceptions...................................................... 200
Handling the ZeroDivisionError Exception ......................... 200
Using try-except Blocks ...................................... 200
Using Exceptions to Prevent Crashes ............................. 201
![](https://csdnimg.cn/release/download_crawler_static/10815166/bg13.jpg)
xviii Contents in Detail
The else Block............................................. 202
Handling the FileNotFoundError Exception ......................... 203
Analyzing Text ............................................ 204
Working with Multiple Files ................................... 205
Failing Silently ............................................ 206
Deciding Which Errors to Report................................ 207
Exercise 10-6: Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Exercise 10-7: Addition Calculator .............................. 208
Exercise 10-8: Cats and Dogs ................................. 208
Exercise 10-9: Silent Cats and Dogs ............................. 208
Exercise 10-10: Common Words ............................... 208
Storing Data .................................................... 208
Using json.dump() and json.load()............................... 209
Saving and Reading User-Generated Data ......................... 210
Refactoring............................................... 212
Exercise 10-11: Favorite Number ............................... 214
Exercise 10-12: Favorite Number Remembered ..................... 214
Exercise 10-13: Verify User ................................... 214
Summary ...................................................... 214
11
TESTING YOUR CODE 215
Testing a Function ................................................ 216
Unit Tests and Test Cases ..................................... 217
A Passing Test ............................................ 217
A Failing Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Responding to a Failed Test ................................... 219
Adding New Tests.......................................... 221
Exercise 11-1: City, Country ................................... 222
Exercise 11-2: Population..................................... 222
Testing a Class .................................................. 222
A Variety of Assert Methods ................................... 222
A Class to Test ............................................ 223
Testing the AnonymousSurvey Class ............................. 225
The setUp() Method ......................................... 227
Exercise 11-3: Employee ..................................... 228
Summary ...................................................... 228
PART II: PROJECTS 231
PROJECT 1: ALIEN INVASION
12
A SHIP THAT FIRES BULLETS 235
Planning Your Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Installing Pygame................................................. 236
Installing Python Packages with pip .............................. 237
Installing Pygame on Linux .................................... 238
![](https://csdnimg.cn/release/download_crawler_static/10815166/bg14.jpg)
Contents in Detail xix
Installing Pygame on OS X .................................... 239
Installing Pygame on Windows................................. 240
Starting the Game Project ........................................... 240
Creating a Pygame Window and Responding to User Input ............. 241
Setting the Background Color .................................. 242
Creating a Settings Class ..................................... 243
Adding the Ship Image............................................. 244
Creating the Ship Class ...................................... 245
Drawing the Ship to the Screen................................. 246
Refactoring: the game_functions Module................................. 247
The check_events() Function ................................... 247
The update_screen() Function .................................. 248
Exercise 12-1: Blue Sky ...................................... 249
Exercise 12-2: Game Character ................................ 249
Piloting the Ship.................................................. 249
Responding to a Keypress .................................... 249
Allowing Continuous Movement ................................ 250
Moving Both Left and Right.................................... 252
Adjusting the Ship’s Speed.................................... 253
Limiting the Ship’s Range ..................................... 255
Refactoring check_events() .................................... 255
A Quick Recap .................................................. 256
alien_invasion.py .......................................... 256
settings.py ............................................... 256
game_functions.py ......................................... 256
ship.py ................................................. 257
Exercise 12-3: Rocket ....................................... 257
Exercise 12-4: Keys......................................... 257
Shooting Bullets .................................................. 257
Adding the Bullet Settings..................................... 257
Creating the Bullet Class ..................................... 258
Storing Bullets in a Group .................................... 259
Firing Bullets.............................................. 260
Deleting Old Bullets......................................... 261
Limiting the Number of Bullets.................................. 262
Creating the update_bullets() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Creating the fire_bullet() Function ............................... 264
Exercise 12-5: Sideways Shooter ............................... 264
Summary ...................................................... 264
13
ALIENS! 265
Reviewing Your Project ............................................ 266
Creating the First Alien ............................................. 266
Creating the Alien Class ..................................... 267
Creating an Instance of the Alien ............................... 268
Making the Alien Appear Onscreen ............................. 268
Building the Alien Fleet ............................................. 269
Determining How Many Aliens Fit in a Row ........................ 269
Creating Rows of Aliens...................................... 270
Creating the Fleet .......................................... 271
剩余561页未读,继续阅读
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/8729d0f1ffbc4f34aac58384426ca7b1_weixin_43852445.jpg!1)
weixin_43852445
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- Vue实现iOS原生Picker组件:详细解析与实现思路
- Arduino蓝牙小车:参数调试与功能控制
- 百度Java面试精华:200页精选资源涵盖核心知识点
- Swift使用CoreData填坑指南:CoreData在Swift 3.0的变化
- 微距离无线充电器创新设计及其实验探索
- MTK Android平台开发全攻略:44步详解流程
- RecyclerView全面解析:替代ListView的新选择
- Android开发:自动适配中英文键盘解决方案
- Android调用WebService接口教程
- Android开发:BitmapUtil图片处理全解析与实例
- Android多线程断点续传实现详解
- PCA算法在人脸识别会议签到系统中的应用
- EventBus 3.0:Android事件总线详解与实战应用
- Android FileUtil:全面解析文件操作实用技巧与实例
- RecyclerView添加头部和尾部实战教程
- Android实现微博滑动固定顶部栏实战与优化
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)