Learn Python

Learn Python Book Written by Fabrizio Romano

Author: Fabrizio Romano
Reviewers: Simone Burol,Julio Vicente,Trigo Guijarro,Veit Heller
Commissioning Editor: Akram Hussain
Acquisition Editor: Indrajit Das
Content Development Editors: Samantha Gonsalves,Adrian Raposo
Technical Editor: Siddhi Rane
Copy Editors:Janbal Dharmaraj,Kevin McGowan
Project Coordinator: Kinjal Bari
Proofreader: Safis Editing
Indexer: Priya Sane
Graphics: Kirk D’Penha, Abhinash Sahu
Production Coordinator: Melwyn D’sa
Cover Work: Melwyn D’sa

Download Learn Python Book Now

Chapter 1: Introduction and First Steps – Take a Deep Breath 1
A proper introduction 2
Enter the Python 4
About Python 5
Portability 5
Coherence 5
Developer productivity 6
An extensive library 6
Software quality 6
Software integration 6
Satisfaction and enjoyment 7
What are the drawbacks? 7
Who is using Python today? 8
Setting up the environment 8
Python 2 versus Python 3 – the great debate 8
Installing Python 9
Setting up the Python interpreter 10
About virtualenv 12
Your first virtual environment 14
Your friend, the console 17
How you can run a Python program 17
Running Python scripts 18
Running the Python interactive shell 18
Running Python as a service 20
Running Python as a GUI application 20
How is Python code organized 21
How do we use modules and packages 22
Python’s execution model 25
Names and namespaces 25
Scopes 27
Object and classes 30
Guidelines on how to write good code 33
The Python culture 34
A note on the IDEs 35
Summary 36
Chapter 2: Built-in Data Types 37
Everything is an object 37
Mutable or immutable? That is the question 38
Numbers 40
Integers 40
Booleans 42
Reals 43
Complex numbers 44
Fractions and decimals 45
Immutable sequences 46
Strings and bytes 46
Encoding and decoding strings 47
Indexing and slicing strings 48
Tuples 49
Mutable sequences 50
Lists 50
Byte arrays 54
Set types 55
Mapping types – dictionaries 57
The collections module 62
Named tuples 62
Defaultdict 64
ChainMap 65
Final considerations 66
Small values caching 66
How to choose data structures 67
About indexing and slicing 68
About the names 70
Summary 70
Chapter 3: Iterating and Making Decisions 73
Conditional programming 74
A specialized else: elif 75
The ternary operator 77
Looping 78
The for loop 78
Iterating over a range 79
Iterating over a sequence 80
Iterators and iterables 81
Iterating over multiple sequences 83
The while loop 85
The break and continue statements 88
A special else clause 90
Putting this all together 91
Example 1 – a prime generator 92
Example 2 – applying discounts 94
A quick peek at the itertools module 97
Infinite iterators 98
Iterators terminating on the shortest input sequence 98
Combinatoric generators 99
Summary 100
Chapter 4: Functions, the Building Blocks of Code 101
Why use functions? 102
Reduce code duplication 103
Splitting a complex task 103
Hide implementation details 104
Improve readability 105
Improve traceability 106
Scopes and name resolution 107
The global and nonlocal statements 108
Input parameters 110
Argument passing 111
Assignment to argument names don’t affect the caller 112
Changing a mutable affects the caller 112
How to specify input parameters 113
Positional arguments 113
Keyword arguments and default values 114
Variable positional arguments 115
Variable keyword arguments 116
Keyword-only arguments 117
Combining input parameters 118
Avoid the trap! Mutable defaults 120
Return values 121
Returning multiple values 123
A few useful tips 124
Recursive functions 125
Anonymous functions 126
Function attributes 127
Built-in functions 128
One final example 129
Documenting your code 130
Importing objects 131
Relative imports 133
Summary 134
Chapter 5: Saving Time and Memory 135
map, zip, and filter 137
map 137
zip 140
filter 141
Comprehensions 142
Nested comprehensions 143
Filtering a comprehension 144
dict comprehensions 146
set comprehensions 147
Generators 148
Generator functions 148
Going beyond next 151
The yield from expression 155
Generator expressions 156
Some performance considerations 159
Don’t overdo comprehensions and generators 162
Name localization 167
Generation behavior in built-ins 168
One last example 169
Summary 171
Chapter 6: Advanced Concepts – OOP, Decorators and Iterators 173
Decorators 173
A decorator factory 180
Object-oriented programming 182
The simplest Python class 182
Class and object namespaces 183
Attribute shadowing 184
I, me, and myself – using the self variable 186
Initializing an instance 187
OOP is about code reuse 187
Inheritance and composition 188
Accessing a base class 192
Multiple inheritance 194
Method resolution order 197
Static and class methods 200
Static methods 200
Class methods 202
Private methods and name mangling 204
The property decorator 206
Operator overloading 208
Polymorphism – a brief overview 209
Writing a custom iterator 210
Summary 211
Chapter 7: Testing, Profiling, and Dealing with Exceptions 213
Testing your application 214
The anatomy of a test 216
Testing guidelines 217
Unit testing 218
Writing a unit test 219
Mock objects and patching 220
Assertions 221
A classic unit test example 221
Making a test fail 224
Interface testing 225
Comparing tests with and without mocks 225
Boundaries and granularity 228
A more interesting example 229
Test-driven development 233
Exceptions 235
Profiling Python 241
When to profile? 244
Summary 245
Chapter 8: The Edges – GUIs and Scripts 247
First approach – scripting 250
The imports 250
Parsing arguments 251
The business logic 253
Second approach – a GUI application 258
The imports 260
The layout logic 261
The business logic 265
Fetching the web page 265
Saving the images 267
Alerting the user 271
How to improve the application? 272
Where do we go from here? 273
The tkinter.tix module 273
The turtle module 274
wxPython, PyQt, and PyGTK 274
The principle of least astonishment 275
Threading considerations 275
Summary 276
Chapter 9: Data Science 277
IPython and Jupyter notebook 278
Dealing with data 281
Setting up the notebook 282
Preparing the data 283
Cleaning the data 287
Creating the DataFrame 289
Unpacking the campaign name 291
Unpacking the user data 293
Cleaning everything up 297
Saving the DataFrame to a file 298
Visualizing the results 299
Where do we go from here? 307
Summary 308
Chapter 10: Web Development Done Right 309
What is the Web? 309
How does the Web work? 310
The Django web framework 311
Django design philosophy 311
The model layer 312
The view layer 312
The template layer 313
The Django URL dispatcher 313
Regular expressions 314
A regex website 314
Setting up Django 315
Starting the project 315
Creating users 317
Adding the Entry model 318
Customizing the admin panel 320
Creating the form 322
Writing the views 323
The home view 325
The entry list view 326
The form view 326
Tying up URLs and views 328
Writing the templates 329
The future of web development 336
Writing a Flask view 336
Building a JSON quote server in Falcon 338
Summary 340
Chapter 11: Debugging and Troubleshooting 341
Debugging techniques 342
Debugging with print 342
Debugging with a custom function 343
Inspecting the traceback 345
Using the Python debugger 348
Inspecting log files 351
Other techniques 353
Profiling 354
Assertions 354
Where to find information 354
Troubleshooting guidelines 355
Using console editors 355
Where to inspect 355
Using tests to debug 356
Monitoring 356
Summary 356
Chapter 12: Summing Up – A Complete Example 357
The challenge 357
Our implementation 358
Implementing the Django interface 358
The setup 358
The model layer 360
A simple form 364
The view layer 364
Imports and home view 364
Listing all records 365
Creating records 365
Updating records 367
Deleting records 369
Setting up the URLs 369
The template layer 370
Home and footer templates 372
Listing all records 372
Creating and editing records 377
Talking to the API 379
Deleting records 383
Implementing the Falcon API 385
The main application 385
Writing the helpers 386
Coding the password validator 387
Coding the password generator 390
Writing the handlers 391
Coding the password validator handler 392
Coding the password generator handler 393
Running the API 394
Testing the API 395
Testing the helpers 395
Testing the handlers 400
Where do you go from here? 402
Summary 403
Index 405

Download Book Now

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s