没有合适的资源?快使用搜索试试~ 我知道了~
首页Python入门速成:实战与项目驱动的编程指南
"《Python Crash Course》是一本由Eric Matthes编写的快速入门书籍,旨在帮助读者掌握Python编程。该书以其实践导向和项目驱动的学习方式,引导读者从基本编程概念如列表、字典、类和循环入手,通过针对每个主题的练习,培养编写整洁、可读代码的能力。书中强调互动性,教导读者如何安全测试代码再将其应用于项目。 本书的核心内容包括使用Python的强大库和工具,如matplotlib、NumPy和Pygal,这些将帮助读者制作响应键盘输入和鼠标点击的2D游戏,同时游戏难度会随着进度提升。此外,书中还介绍了如何利用数据进行交互式可视化,以及如何创建和定制简单的web应用,并且确保安全地在线部署。 在学习过程中,读者将学会如何处理错误,从而解决自己的编程问题,这对于成为一名独立开发者来说至关重要。这本书适合对编程有热情的人,它能迅速提升读者的编程技能,让他们能够迅速写出实际运行的程序。《Python Crash Course》是一本非常适合初学者的实用教程,无论你是想要踏入编程世界的新手,还是希望提升现有技能的专业人士,都将从中获益良多。"
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/9388275/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/9388275/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/9388275/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/9388275/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/9388275/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
www.allitebooks.com
剩余561页未读,继续阅读
![rar](https://img-home.csdnimg.cn/images/20210720083606.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://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/a85f9b5ddf8441b8b4d02a19e1ce0d8e_vanridin.jpg!1)
vanridin
- 粉丝: 108
- 资源: 1191
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
会员权益专享
最新资源
- Simulink在电机控制仿真中的应用
- 电子警察:功能、结构与抓拍原理详解
- TESSY 4.1 英文用户手册:Razorcat Development GmbH
- 5V12V直流稳压电源设计及其实现
- 江西建工四建来宾市消防支队高支模施工方案
- 三维建模教程:创建足球模型
- 宏福苑南二区公寓楼施工组织设计
- 福建外运集团信息化建设技术方案:网络与业务平台设计
- 打造理想工作环境:详尽的6S推行指南
- 阿里巴巴数据中台建设与实践
- 欧姆龙CP1H PLC操作手册:SYSMACCP系列详解
- 中国移动统一DPI设备技术规范:LTE数据合成服务器关键功能详解
- 高校竞赛信息管理系统:软件设计与体系详解
- 面向对象设计:准则、启发规则与系统分解
- 程序设计基础与算法解析
- 算法与程序设计基础概览
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](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)