📄 Page
1
(This page has no text content)
📄 Page
2
Python for Professionals Learning Python as a Second Language by Matt Telles
📄 Page
3
ii FIRST EDITION 2020 Copyright © BPB Publications, India All Rights Reserved. No part of this publication may be reproduced or distributed in any form or by any means or stored in a database or retrieval system, without the prior written permission of the publisher with the exception to the program listings which may be entered, stored and executed in a computer system, but they can not be reproduced by the means of publication. LIMITS OF LIABILITY AND DISCLAIMER OF WARRANTY The information contained in this book is true to correct and the best of author’s & publisher’s knowledge. The author has made every effort to ensure the accuracy of these publications, but cannot be held responsible for any loss or damage arising from any information in this book. All trademarks referred to in the book are acknowledged as properties of their respective owners. Distributors: BPB PUBLICATIONS 20, Ansari Road, Darya Ganj New Delhi-110002 Ph: 23254990/23254991 MICRO MEDIA Shop No. 5, Mahendra Chambers, 150 DN Rd. Next to Capital Cinema, V.T. (C.S.T.) Station, MUMBAI-400 001 Ph: 22078296/22078297 DECCAN AGENCIES 4-3-329, Bank Street, Hyderabad-500195 Ph: 24756967/24756400 BPB BOOK CENTRE 376 Old Lajpat Rai Market, Delhi-110006 Ph: 23861747 Published by Manish Jain for BPB Publications, 20 Ansari Road, Darya Ganj, New Delhi-110002 and Printed by him at Repro India Ltd, Mumbai ISBN: 978-93-89423-754
📄 Page
4
iii Dedicated to My wife Teresa
📄 Page
5
iv About the Author Matt Telles is a 35-year veteran in the software industry. He has worked with virtually all programming languages, and has been a developer, manager, tester, and designer. He’s been working on Python for several years and is constantly extending his knowledge in the field. Matt is married with three children, living in New York, the United States. He has a menagerie of cats, dogs and a turtle, and loves reading books on his Microsoft Surface on the train to work every morning.
📄 Page
6
v About the Reviewer Tarun Behal is a computer scientist and software engineer with almost 8 years of experience in software development. He completed his graduation in computer science and software engineering from UPTU, India, in 2012. Currently pursuing masters from BITS Pilani, India. Since then, he has worked with several technologies and languages, including Odoo, Django, JavaScript, Ruby, PHP and Python. He currently resides in Noida, India and works for Adobe Inc. Some of the applications covered by Tarun during his career include RESTful APIs, ERPs, billing platforms, hotel management, financial systems and e-commerce websites. He has been using Python on both personal and professional projects since 2012, and he is passionate about software design, software quality, and coding standards. I would like to thank my parents for their love, good advice, and continuous support. I would also like to thank all my friends that I met along the way, who enriched my life, for motivating me and helping me progress.
📄 Page
7
vi Acknowledgement First and foremost, I would like to thank all of the employers who have allowed me to learn my skills while working for them. Thank you for that opportunity. Secondly, I would like to thank my family, who have put up with me while writing this book. Finally, I’d like to thank everyone who reads the book, and finds anything in it to be useful in their daily lives. I’ve spend a lot of time over the years learning from others, this is my chance to give back to the community. – Matt Telles
📄 Page
8
vii Preface Python has become very popular among programmers, testers, and web developers. The majority of Python is easy to pick up and run with right away, but the details of the language are sometimes a bit difficult to understand without good examples. The purpose of this book is to help a professional programmer get started quickly with the language. The target audience of this book is someone who has written programs in the past, not necessarily in Python though. This book is divided into 10 chapters and it provides a detailed description of the core concepts of Python programming. Chapter 1 introduces the history and installation of Python, as well as the differences between the current versions to help you determine which one to install. Chapter 2 addresses the basic data types in Python, as well as the collections and iterables. Chapter 3 discusses the nuts and bolts of Pythons, with a focus on conditional statements, loops, and objects, with their attributes. Chapter 4 discusses how to work with Python, from the immediate mode of the interpreter to the importing of pre-built packages and modules for use in your application. Chapter 5 is involved with object-oriented programming as it applies to Python. You will learn about the pillars of object orientation and how they are implemented using the Python programming language. Chapter 6 addresses the concepts of advanced manipulations in Python. You will learn about comprehensions, generators and slices. Chapter 7 is all about input and output in Python, from writing to the console to reading and writing files. You’ll learn about JSON and text files, as well as working with binary file formats. Chapter 8 describes imports and exports, how to use other people’s code and make your own code available for the community or your own company. Chapter 9 addresses a variety of miscellaneous topics, from decorators and properties to documentation and metaclasses.
📄 Page
9
viii Chapter 10 will help you to focus on not reinventing the wheel by exploring some of the more popular Python packages available to you, and looking at how to use them in your own programs. Chapter 11 will present a series of tips and tricks that you can use to immediately upgrade your Python programming skills.
📄 Page
10
ix Errata We take immense pride in our work at BPB Publications and follow best practices to ensure the accuracy of our content to provide with an indulging reading experience to our subscribers. Our readers are our mirrors, and we use their inputs to reflect and improve upon human errors if any, occurred during the publishing processes involved. To let us maintain the quality and help us reach out to any readers who might be having difficulties due to any unforeseen errors, please write to us at : errata@bpbonline.com Your support, suggestions and feedbacks are highly appreciated by the BPB Publications’ Family.
📄 Page
11
x Table of Contents 1. The History and Installation of Python .......................................................... 1 Introduction .................................................................................................... 1 Structure .......................................................................................................... 1 Objectives ........................................................................................................ 2 Python: the language ..................................................................................... 2 History ............................................................................................................. 2 Selecting a Python version ............................................................................ 4 Why not use 2.7? ....................................................................................... 4 Which 3.x to use? ....................................................................................... 5 Installing .......................................................................................................... 5 Testing your installation ............................................................................ 8 The Zen of Python.......................................................................................... 9 Keeping it simple ...................................................................................... 10 Keeping it readable ....................................................................................11 Never is better than right now ..................................................................11 Using pip ....................................................................................................... 12 Using virtual environments ........................................................................ 15 Python IDE’s and command line work..................................................... 18 Hello world ................................................................................................... 21 Conclusion .................................................................................................... 22 Questions ....................................................................................................... 23 2. Python Types and Constructs .......................................................................... 25 Introduction .................................................................................................. 25 Structure ........................................................................................................ 25 Objectives ...................................................................................................... 26 Integers .......................................................................................................... 26 Floating point numbers ............................................................................... 29 Boolean .......................................................................................................... 31 Complex values ............................................................................................ 32 Variable naming ........................................................................................... 33
📄 Page
12
xi Strings ............................................................................................................ 34 Finding substrings ................................................................................... 36 Multiple line strings ..................................................................................... 38 Concatenating .......................................................................................... 39 Other methods .......................................................................................... 39 Python Collections ....................................................................................... 41 Lists .......................................................................................................... 41 Dictionaries .............................................................................................. 45 Getting the value of a key .............................................................................. 46 Testing if a key is in a dictionary .................................................................. 46 Iterating ........................................................................................................ 47 Length of a dictionary ................................................................................... 48 Adding new items ......................................................................................... 48 Nested dictionaries ........................................................................................ 49 Sets ........................................................................................................... 51 Tuples ....................................................................................................... 55 Iterators and iterables .................................................................................. 56 Sorted ............................................................................................................. 66 The zip function ........................................................................................... 70 Booleans and truthiness .............................................................................. 71 Comments ..................................................................................................... 73 Conclusion .................................................................................................... 74 Questions ....................................................................................................... 74 3. The Nuts and Bolts ............................................................................................ 75 Introduction .................................................................................................. 75 Structure ........................................................................................................ 75 Objectives ...................................................................................................... 76 Conditionals .................................................................................................. 76 The “walrus” operator ............................................................................. 77 ANDs, ORs, NOTs, and logicals ............................................................. 78 Indentation .................................................................................................... 81 The pass statement ................................................................................... 82 Loops ........................................................................................................ 83 Functions ....................................................................................................... 91
📄 Page
13
xii Parameters................................................................................................ 92 Return values ........................................................................................... 92 Required vs optional arguments .............................................................. 95 Keyword arguments ................................................................................. 95 Variable length arguments ....................................................................... 96 Lambdas ................................................................................................... 97 Classes ........................................................................................................... 99 Scoping ................................................................................................... 105 Objects ......................................................................................................... 108 Conclusion .................................................................................................. 109 Questions ..................................................................................................... 109 4. Organizational Skills .......................................................................................111 Introduction .................................................................................................111 Structure .......................................................................................................111 Objectives .....................................................................................................112 Immediate mode .........................................................................................112 Modules ........................................................................................................115 Packages ...................................................................................................... 121 Importing .................................................................................................... 125 Paths ............................................................................................................. 127 Dot notation in naming ............................................................................. 128 Installing packages using pip ................................................................. 129 Insuring requirements with pip ............................................................. 134 User installs vs system installs .............................................................. 135 Conclusion .................................................................................................. 136 Questions ..................................................................................................... 136 5. Object-Oriented Programming ..................................................................... 137 Introduction ................................................................................................ 137 Structure ...................................................................................................... 137 Objective ...................................................................................................... 138 Object-oriented programming with Python .......................................... 138 Abstraction ............................................................................................. 138 Encapsulation ......................................................................................... 138
📄 Page
14
xiii Inheritance ............................................................................................. 139 Multiple inheritance ........................................................................................... 143 Polymorphism, the Python way ............................................................. 147 Overloading of methods ........................................................................... 150 Overloading of operators .......................................................................... 153 Comparisons and overloading ................................................................ 158 Read-only attributes ............................................................................... 160 The __new__operator ............................................................................. 163 Classes and Iteratables .............................................................................. 165 Chaining of operations .............................................................................. 167 Initialization ................................................................................................ 172 Conclusion .................................................................................................. 176 Questions ..................................................................................................... 176 6. Advanced Manipulations ............................................................................... 177 Introduction ................................................................................................ 177 Structure ...................................................................................................... 177 Objectives .................................................................................................... 178 List comprehensions .................................................................................. 178 Dictionary comprehensions ...................................................................... 183 Nested dictionary comprehensions ......................................................... 186 Applying functions ..................................................................................... 187 Restrictions on dictionary comprehensions ................................................ 188 Set comprehensions ................................................................................... 188 Generators ................................................................................................... 191 Building a string from a list ...................................................................... 196 Searching a string ....................................................................................... 199 Searching a collection ................................................................................ 201 Using a set of functions to create an extensible state machine ............ 204 Filtering vs removing ................................................................................ 207 Slicing........................................................................................................... 209 Lambda expressions ...................................................................................211 The ‘splat’ operator and unpacking ........................................................ 212 Conclusion .................................................................................................. 214 Questions ..................................................................................................... 214
📄 Page
15
xiv 7. File Input and Output ..................................................................................... 215 Introduction ................................................................................................ 215 Structure ...................................................................................................... 215 Objectives .................................................................................................... 215 Files .............................................................................................................. 216 Working with files .................................................................................. 216 Using the with statement .......................................................................... 219 Reading fixed length data from files in Python ..................................... 220 Reading a text file by lines in Python .................................................... 223 A readlines real-world example ............................................................... 225 Python and binary files ............................................................................. 226 JSON parsing .............................................................................................. 229 JSON writing .............................................................................................. 231 Serializing complex objects in JSON ....................................................... 232 Reading in text vs reading in lines .......................................................... 235 Writing out lines .................................................................................... 235 Output formatting ................................................................................. 236 Pickling ........................................................................................................ 240 Conclusion .................................................................................................. 242 Questions ..................................................................................................... 242 8. Imports and Reuse ........................................................................................... 243 Introduction ................................................................................................ 243 Structure ...................................................................................................... 243 Objectives .................................................................................................... 244 Import and reuse of code .......................................................................... 244 Importing ............................................................................................... 245 Importing modules ................................................................................. 248 Importing packages ................................................................................ 250 Dynamic imports ................................................................................... 252 Working with the os module .................................................................. 254 Directory and file information ............................................................... 256 Listing installed packages ......................................................................... 256 Reflection ..................................................................................................... 258 Using Reflection ..................................................................................... 262
📄 Page
16
xv Conclusion .................................................................................................. 266 Questions ..................................................................................................... 266 9. Miscellaneous ................................................................................................... 267 Introduction ................................................................................................ 267 Structure ...................................................................................................... 267 Objectives .................................................................................................... 268 Decorators ................................................................................................... 268 Variable arguments .................................................................................... 275 Character encoding .................................................................................... 278 Properties .................................................................................................... 280 Description strings ..................................................................................... 283 Namespaces ................................................................................................ 284 Context managers ...................................................................................... 285 Metaclasses ................................................................................................. 287 Dynamic classes and functions ................................................................ 289 Deep vs shallow copying .......................................................................... 290 Exception handling .................................................................................... 292 Conclusion .................................................................................................. 295 Questions ..................................................................................................... 295 10. Not Reinventing the Wheel ........................................................................... 297 Introduction ................................................................................................ 297 Structure ...................................................................................................... 297 Objectives .................................................................................................... 298 Not reinventing the wheel ........................................................................ 298 Itertools ........................................................................................................ 299 Flask ............................................................................................................. 303 Adding authentication ........................................................................... 309 Numpy ..........................................................................................................311 Installing Numpy................................................................................... 312 Getting started: The basic array ............................................................. 313 Accessing Numpy Data ......................................................................... 314 Data types .............................................................................................. 314 Modifying arrays with Numpy .............................................................. 316
📄 Page
17
xvi Numpy mathematical functions ............................................................ 317 Logging ........................................................................................................ 320 Unit test ................................................................................................. 323 Setup and teardown ............................................................................... 326 Mocking ....................................................................................................... 326 Concurrency ................................................................................................ 330 The emoji package ..................................................................................... 333 The pprint package .................................................................................... 334 The requests package................................................................................. 335 Conclusion .................................................................................................. 338 Questions ..................................................................................................... 338 11. General Tips and Tricks ................................................................................. 339 Introduction ................................................................................................ 339 Structure ...................................................................................................... 339 Objectives .................................................................................................... 340 Implementing a switch statement with dictionaries............................. 340 Remove duplicates from a list .................................................................. 345 Determine the size of your objects in memory ...................................... 347 Find the most frequent item in a list ....................................................... 352 Creating an enum in a class ...................................................................... 354 Detect Python version ............................................................................... 356 Using the _ (underscore) operator ........................................................... 357 Discovering where a module is imported from .................................... 358 Swapping two values without an intermediate temporary ................. 359 Using the classmethod decorator to create static methods .................. 361 Using the **kwargs to pass a named list of parameters ....................... 362 Type hints .................................................................................................... 364 Finding the day of the week using the calendar module ..................... 366 Working with regular expressions ........................................................... 367 Conclusion .................................................................................................. 369 Questions ..................................................................................................... 370
📄 Page
18
Chapter 1 The History and Installation of Python Introduction Every professional programmer who has been in the industry for any length of time has seen changes. Whether it is a new job, or a new boss, or simply a new approach at work, change is the only constant in the programming world. New operating systems, new frameworks, new devices, and, of course, new programming languages. For those of us that came up in the Unix world, or using MS-DOS, change has been dramatic. If you’ve worked mostly in the Linux world, the change may have been less dramatic. Changes in your development language, however, are always both exiting and traumatic. Moving to a new language is like moving to a new house with all new things to get used to and to accept. If you are coming from the Java, C# or C++ world, transitioning to Python might be confusing. Not only has the syntax changed, but the very way of thinking has changed. The purpose of this book is to ease that transition, and to help you think like a Pythonista. A Pythonista, of course, is one that has adopted Python as their primary language, and struggles to master new concepts in Python at all times. Structure • History • Selecting a Python version
📄 Page
19
2 Python for Professionals • The Zen of Python • Keeping it simple • Using virtual environments • Using pip • Python IDE’s and command line work Objectives By the end of this chapter, you should understand how to select a Python version to use, how to install the Python system, and how to use some of the general tools that Python developers need in their day to day experience. You will learn about the history of Python, the Zen of Python, and how to keep it simple. Python: the language One of the most frustrating things for professional developers is finding a good transition methodology. Let’s be honest, you don’t want to learn about what a memory location is, or how the compiler/interpreter works to do your job. You want to start out with that nice simple Hello world example, see how the types and statement work, and what the gotcha’s are in the language. You already know what loops are, what assignment statements are, and how to use functions and classes from your previous work. What you don’t know is how that new language implements all these things, and that’s what you will learn here. We will look at the basics of Python in this chapter, as well as introducing some basic concepts that you’ll need to think about when writing in Python. You’ll find out a little bit of trivia, because what developer doesn’t love to know trivia about his or her language? You’ll find out the philosophy behind Python and why things are the way they are. Then we’ll get into the nuts and bolts of getting you a functional development environment, and show you how to create your first Python program. Welcome to Python! May your journey be fruitful and your code concise. History If you’ve never seen Python before, it might surprise you to know that it has been around for a very long time. Older than Java and C#, Python was first created in the late 1980s. It was created by Guido Van Rossum at CWI in the Netherlands. The first cut at the language didn’t get much traction in the real world of software development though. It wasn’t until the turn of the millennium, the year 2000, when Python 2.0 came out and people began to use it in earnest. Where the original Python language was a very simple interpreted language best suited for UNIX scripting, the second version of the language was much more
📄 Page
20
The History and Installation of Python 3 robust. Supporting memory management and garbage collection, not to mention full object-oriented programming (OOP) concepts like classes and inheritance, it made writing complex tasks very easy. Much like Visual Basic did for Windows, Python did for the early days of Linux, making it easy for non-developers to get quickly into programming. Unlike Visual Basic, however, Python was well thought out, enough so that advanced programming was done quickly. Because it was interpreted, the development cycle for Python was rapid, and thus was adopted in many companies for that reason alone. As the language adoption increased, so did the packages, code libraries that accomplished complex tasks. We’ll look at those later in the book, but Python today is used for such diverse tasks as writing web servers and doing artificial intelligence. It has excellent mathematical libraries that make it ideal for doing statistical work as well. Because of its small footprint, Python is available on virtually every platform in existence. With the second release came support for Unicode, giving it international acceptance as well. Python is a byte-code language, like Java or C#. That means that the interpreter will compile your scripts into a simplistic byte code that can be quickly and easily interpreted. This is good, in that it is fast and easy to use. At the same time, there is a cost, as there always is. Python interprets things as it reaches them, meaning that syntax errors aren’t caught at compile time, but at run-time. The biggest reason that people gravitate to Python is its simplicity. Rather than some languages which have a dozen different constructs for looping, Python has but two; the for statement and the while statement. A language like Java requires a dozen lines to open a file and write to it, Python requires three. In C# or C++, you have to structure your code in a specific manner that the compiler accepts, in Python; you can implement a full program in a single line in a single file. Functional programming used to require hideously complicated languages like Scala, which in turn required the Java system; Python can do it in a few lines of code. That’s not to say that Python can replace all existing programming languages, it most certainly can’t. Being an interpreted language, it is naturally slower than a compiled language. It lacks the GUI libraries that many languages come with, and the security built into other byte- code languages like C# or Java. Oh, and finally, the fun facts. Python isn’t named after a snake. Rather, it is named after the British comedy show Monty Python. It has become so popular that it is the de facto standard programming language at Google, a somewhat large multi-national software company. Python is open source, meaning that it isn’t controlled by a single company or a single person. Anyone can contribute to the Python system, there is a committee that approves language changes, but if you want your own personal version, you can do so. Finally, in a study done by Ocado Technology, Python is a more popular language than French in schools. Take that Napoleon!