Learn Python Programming

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Author(s): Fabrizio Romano
Edition: Second Edition
Publisher: Packt
Year: 2018

Language: English
Pages: 495

Cover
......Page 1
Title Page......Page 2
Copyright and Credits......Page 3
Dedication......Page 4
Packt Upsell......Page 5
Foreword......Page 6
Contributors......Page 7
Table of Contents......Page 9
Preface......Page 18
Chapter 1: A Gentle Introduction to Python
......Page 25
A proper introduction......Page 27
Portability......Page 29
Software quality......Page 30
What are the drawbacks?......Page 31
Python 2 versus Python 3......Page 32
Setting up the Python interpreter......Page 34
About virtualenv......Page 36
Your first virtual environment......Page 37
Running Python scripts......Page 40
Running the Python interactive shell......Page 41
Running Python as a service......Page 42
How is Python code organized?......Page 43
How do we use modules and packages?......Page 45
Names and namespaces......Page 48
Scopes......Page 50
Objects and classes......Page 53
Guidelines on how to write good code......Page 56
The Python culture......Page 57
A note on IDEs......Page 58
Summary......Page 59
Chapter 2: Built-in Data Types
......Page 60
Everything is an object......Page 61
Mutable or immutable? That is the question......Page 62
Integers......Page 63
Booleans......Page 65
Real numbers......Page 66
Complex numbers......Page 67
Fractions and decimals......Page 68
Strings and bytes......Page 69
Encoding and decoding strings......Page 70
Indexing and slicing strings......Page 71
String formatting......Page 72
Tuples......Page 73
Lists......Page 74
Byte arrays......Page 78
Set types......Page 79
Mapping types – dictionaries......Page 81
The collections module......Page 85
namedtuple......Page 86
defaultdict......Page 87
ChainMap......Page 88
Enums......Page 89
Small values caching......Page 90
How to choose data structures......Page 91
About indexing and slicing......Page 93
About the names......Page 94
Summary......Page 95
Chapter 3: Iterating and Making Decisions
......Page 96
Conditional programming......Page 97
A specialized else – elif......Page 98
The ternary operator......Page 100
The for loop......Page 101
Iterating over a sequence......Page 102
Iterators and iterables......Page 104
Iterating over multiple sequences......Page 105
The while loop......Page 107
The break and continue statements......Page 110
A special else clause......Page 112
A prime generator......Page 114
Applying discounts......Page 116
A quick peek at the itertools module......Page 119
Infinite iterators......Page 120
Iterators terminating on the shortest input sequence......Page 121
Summary......Page 122
Chapter 4: Functions, the Building Blocks of Code
......Page 124
Why use functions?......Page 125
Splitting a complex task......Page 126
Hiding implementation details......Page 127
Improving readability......Page 128
Improving traceability......Page 129
Scopes and name resolution......Page 130
The global and nonlocal statements......Page 131
Argument-passing......Page 133
Changing a mutable affects the caller......Page 135
Positional arguments......Page 136
Keyword arguments and default values......Page 137
Variable positional arguments......Page 138
Variable keyword arguments......Page 139
Combining input parameters......Page 141
Additional unpacking generalizations......Page 143
Avoid the trap! Mutable defaults......Page 144
Return values......Page 145
Returning multiple values......Page 147
A few useful tips......Page 148
Anonymous functions......Page 149
Function attributes......Page 151
One final example......Page 152
Documenting your code......Page 153
Importing objects......Page 154
Relative imports......Page 156
Summary......Page 157
Chapter 5: Saving Time and Memory
......Page 158
map......Page 160
zip......Page 163
filter......Page 164
Comprehensions......Page 165
Nested comprehensions......Page 166
Filtering a comprehension......Page 167
dict comprehensions......Page 169
set comprehensions......Page 170
Generator functions......Page 171
Going beyond next......Page 174
The yield from expression......Page 178
Generator expressions......Page 179
Some performance considerations......Page 181
Don't overdo comprehensions and generators......Page 184
Name localization......Page 188
One last example......Page 190
Summary......Page 192
Decorators......Page 194
A decorator factory......Page 200
Object-oriented programming (OOP)......Page 202
The simplest Python class......Page 203
Class and object namespaces......Page 204
Attribute shadowing......Page 205
Me, myself, and I – using the self variable......Page 206
Initializing an instance......Page 207
Inheritance and composition......Page 208
Accessing a base class......Page 213
Multiple inheritance......Page 215
Method resolution order......Page 218
Static methods......Page 220
Class methods......Page 222
Private methods and name mangling......Page 224
The property decorator......Page 226
Operator overloading......Page 228
Polymorphism – a brief overview......Page 229
Data classes......Page 230
Writing a custom iterator......Page 231
Summary......Page 232
Chapter 7: Files and Data Persistence
......Page 233
Opening files......Page 234
Reading and writing to a file......Page 236
Reading and writing in binary mode......Page 237
Checking for file and directory existence......Page 238
Manipulating files and directories......Page 239
Manipulating pathnames......Page 241
Temporary files and directories......Page 242
Directory content......Page 243
Data interchange formats......Page 244
Working with JSON......Page 245
Custom encoding/decoding with JSON......Page 248
Using an in-memory stream......Page 252
Making HTTP requests......Page 253
Serializing data with pickle......Page 256
Saving data with shelve......Page 258
Saving data to a database......Page 259
Summary......Page 266
Chapter 8: Testing, Profiling, and Dealing with Exceptions
......Page 267
Testing your application......Page 268
The anatomy of a test......Page 270
Testing guidelines......Page 271
Writing a unit test......Page 273
Assertions......Page 275
Testing a CSV generator......Page 276
Testing the export function......Page 285
Final considerations......Page 288
Test-driven development......Page 290
Exceptions......Page 292
Profiling Python......Page 298
When to profile?......Page 300
Summary......Page 302
The need for cryptography......Page 303
Useful guidelines......Page 304
Hashlib......Page 305
Random numbers......Page 307
Token generation......Page 308
Digest comparison......Page 310
JSON Web Tokens......Page 311
Time-related claims......Page 314
Auth-related claims......Page 316
Using asymmetric (public-key) algorithms......Page 317
Useful references......Page 318
Summary......Page 319
Chapter 10: Concurrent Execution
......Page 320
Threads and processes – an overview......Page 321
Killing threads......Page 322
Context-switching......Page 323
Race conditions and deadlocks......Page 324
Scenario B – race condition happening......Page 325
Deadlocks......Page 326
Quick anatomy of a process......Page 327
Multithreading or multiprocessing?......Page 328
Concurrent execution in Python......Page 329
Starting a thread......Page 330
Stopping threads and processes......Page 332
Spawning multiple threads......Page 334
Dealing with race conditions......Page 335
A thread's local data......Page 337
Thread and process communication......Page 338
Thread communication......Page 339
Sending events......Page 340
Inter-process communication with queues......Page 341
Thread and process pools......Page 342
Using a process to add a timeout to a function......Page 345
Example one – concurrent mergesort......Page 347
Single-thread mergesort......Page 348
Single-thread multipart mergesort......Page 349
Multithreaded mergesort......Page 350
Multiprocess mergesort......Page 351
What is Sudoku?......Page 353
Implementing a sudoku-solver in Python......Page 354
Solving sudoku with multiprocessing......Page 359
Example three – downloading random pictures......Page 362
Downloading random pictures with asyncio......Page 364
Summary......Page 368
Chapter 11: Debugging and Troubleshooting
......Page 369
Debugging with a custom function......Page 371
Inspecting the traceback......Page 373
Using the Python debugger......Page 376
Inspecting log files......Page 379
Other techniques......Page 381
Where to find information......Page 382
Where to inspect......Page 383
Summary......Page 384
Chapter 12: GUIs and Scripts
......Page 386
First approach – scripting......Page 388
The imports......Page 389
Parsing arguments......Page 390
The business logic......Page 392
Second approach – a GUI application......Page 396
The layout logic......Page 399
The business logic......Page 403
Fetching the web page......Page 404
Saving the images......Page 406
Alerting the user......Page 409
How can we improve the application?......Page 410
Where do we go from here?......Page 411
wxPython, PyQt, and PyGTK......Page 412
Threading considerations......Page 413
Summary......Page 414
Chapter 13: Data Science
......Page 415
IPython and Jupyter Notebook......Page 416
Starting a Notebook......Page 419
Setting up the Notebook......Page 420
Preparing the data......Page 421
Cleaning the data......Page 425
Creating the DataFrame......Page 427
Unpacking the campaign name......Page 430
Unpacking the user data......Page 432
Cleaning everything up......Page 436
Saving the DataFrame to a file......Page 437
Visualizing the results......Page 438
Where do we go from here?......Page 445
Summary......Page 447
What is the web?......Page 448
How does the web work?......Page 449
Django design philosophy......Page 450
The model layer......Page 451
The template layer......Page 452
Regular expressions......Page 453
Starting the project......Page 454
Creating users......Page 456
Adding the Entry model......Page 457
Customizing the admin panel......Page 459
Creating the form......Page 461
The home view......Page 463
The entry list view......Page 464
The form view......Page 466
Tying up URLs and views......Page 468
Writing the templates......Page 470
The future of web development......Page 476
Writing a Flask view......Page 477
Building a JSON quote server in Falcon......Page 479
Summary......Page 481
Farewell......Page 482
Other Books You May Enjoy......Page 483
Index......Page 486