Leanpub Header

Skip to main content

The Python Apprentice

Our goal with this book is to give you a practical and thorough introduction to the Python programming language, providing you with the tools and insight you need to be a productive Python developer. We want to help you build solid foundations, orient you in the universe of Python, and put you in a position to direct your own continued learning.

Minimum price

$14.99

$19.99

You pay

$19.99

Authors earn

$15.99
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
4,349
Readers
About

About

About the Book

The Python Apprentice together with The Python Journeyman and The Python Master comprise a trilogy of books on the Python programming language and the Python Standard Library. The material in these books is derived from Sixty North's popular and battle-tested training courses which have led thousands of programmers on a journey to being productive with Python.

Our books, which use Python 3, follow a spiral curriculum: We introduce topics gently and then revisit them on multiple occasions to add the depth required to support your progression as a Python developer. We've worked hard to structure the syllabus to avoid forward references. On only a few occasions do we require you to accept techniques on trust, before explaining them later; where we do, it's to deliberately establish good habits.

Share this book

Categories

Author

About the Authors

Robert Smallshire

Robert Smallshire is a founding director of Sixty North, a software consulting and training business in Norway providing services throughout Europe, and which uses Python extensively. Robert has worked in senior architecture and technical management roles for several software companies providing tools in the energy sector. He has dealt with understanding, designing, advocating and implementing effective architectures for sophisticated scientific and enterprise software in Python, C++, C# and F# and Javascript. Robert is a regular speaker at conferences, meetups and corporate software events and can be found speaking about topics as diverse as behavioural microeconomics in software development to implementing web services on 8-bit microcontrollers. He is organiser of the Oslo Python group and holds a Ph.D. in a natural science.

Austin Bingham

Austin is a founding director of Sixty North, a software consulting, training, and application development company. A native of Texas, in 2008 Austin moved to Stavanger, Norway where he helped develop industry-leading oil reservoir modeling software in C++ and Python. Prior to that he worked at National Instruments developing LabVIEW, at Applied Research Labs (Univ. of Texas at Austin) developing sonar systems for the U.S. Navy, and at a number of telecommunications companies. He is an experienced presenter and teacher, having spoken at numerous conferences, software groups, and internal corporate venues. Austin is also an active member of the open source community, contributing regularly to various Python and Emacs projects, and he’s the founder of Stavanger Software Developers, one of the largest and most active social software groups in Stavanger. Austin holds a Master of Science in Computer Engineering from the University of Texas at Austin.

Sixty North

Sixty North AS. Software Consulting and Training.

Translations

Translations

Languages

Contents

Table of Contents

Preface

  1. Errata and Suggestions
  2. Conventions Used in This Book
  3. Code examples

Welcome Apprentice!

Python Promo

Overview

What is Python?

  1. It’s a programming language!
  2. It’s a standard library!
  3. It’s a philosophy

The journey of a thousand miles…

Chapter 1 – Getting started

  1. Obtaining and installing Python 3
  2. Starting Python command line REPL
  3. Leaving the REPL
  4. Code structure and significant indentation
  5. Python culture
  6. Importing standard library modules
  7. Getting help()
  8. Scalar data types: integers, floats, None and bool
  9. Relational operators
  10. Control flow: if-statements and while-loops
  11. Summary

Chapter 2 – Strings and collections

  1. str – an immutable sequence of Unicode code points
  2. Moment of zen
  3. bytes – an immutable sequence of bytes
  4. String differences between Python 3 and Python 2
  5. list – a sequence of objects
  6. dict – associating keys with values
  7. For-loops – iterating over series of items
  8. Putting it all together
  9. HTTP access problems?
  10. Summary

Chapter 3 – Modularity

  1. Organizing code in a .py file
  2. Defining functions
  3. Organizing our module into functions
  4. Speaking Python aloud
  5. The Python execution model
  6. Setting up a main function with command line argument
  7. Moment of zen
  8. Docstrings
  9. Comments
  10. Shebang
  11. Summary

Chapter 4 – Built-in types and the object model

  1. The nature of Python object references
  2. Argument passing semantics – pass by object-reference
  3. Python return semantics
  4. Function arguments in detail
  5. The Python type system
  6. Variable declaration and scoping
  7. Moment of zen
  8. Everything is an object
  9. Summary

Chapter 5 – Exploring built-in collection types

  1. tuple – an immutable sequence of objects
  2. The tuple constructor
  3. Strings in action
  4. Moment of zen
  5. range – a collection of evenly spaced integers
  6. list in action
  7. Dictionaries
  8. set – an unordered collection of unique elements
  9. Collection protocols
  10. Summary

Chapter 6 – Exceptions

  1. Exceptions and control flow
  2. Handling exceptions
  3. Handling multiple exceptions
  4. Programmer errors
  5. Empty blocks – the pass statement
  6. Exception objects
  7. Imprudent return codes
  8. Re-raising exceptions
  9. Exceptions are part of your function’s API
  10. Guard clauses
  11. Exceptions, APIs, and protocols
  12. Choosing not to guard against TypeError
  13. Pythonic style – EAFP versus LBYL
  14. Clean-up actions
  15. Moment of zen
  16. Platform-specific code
  17. Summary

Chapter 7 - Comprehensions, iterables, and generators

  1. Comprehensions
  2. Moment of zen
  3. Iteration protocols
  4. Generator functions
  5. Generator expressions
  6. “Batteries included” iteration tools
  7. Pulling it all together
  8. Summary

Chapter 8 – Defining new types with classes

  1. Defining classes
  2. Instance methods
  3. Instance initializers
  4. Validation and invariants
  5. Adding a second class
  6. Collaborating classes
  7. Moment of zen
  8. Booking seats
  9. Naming methods for implementation details
  10. Sometimes the only object you need is a function
  11. Polymorphism and duck-typing
  12. Inheritance and implementation sharing
  13. Summary

Chapter 9 – Files and resource management

  1. Files
  2. Context managers
  3. Moment of zen
  4. Binary files
  5. File-like objects
  6. Other resources
  7. Summary

Chapter 10 – Unit testing with the Python standard library

  1. Test cases
  2. Fixtures
  3. Assertions
  4. Unit testing example: text analysis
  5. Using fixtures to create temporary files
  6. Using the new fixtures
  7. Using assertions to test behavior
  8. Testing for exceptions
  9. Testing for file existence
  10. Moment of zen
  11. Summary

Chapter 11 – Debugging with PDB

  1. Debugging commands
  2. Palindrome debugging
  3. Summary

Afterword: Just the Beginning

Appendix A – Virtual environments

  1. Creating a virtual environment
  2. Activating a virtual environment
  3. Deactivating a virtual environment
  4. Other tools for working with virtual environments

Appendix B – Packaging and distribution

  1. Configuring a package with distutils
  2. Installing with distutils
  3. Packaging with distutils

Appendix C – Installing third-party packages

  1. Introducing pip
  2. The Python Package Index
  3. Installing local packages with pip
  4. Uninstalling packages

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub