The Python Apprentice

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"

Learn the Python skills and culture you need to become a productive member of any Python project.

About This Book

  • Taking a practical approach to studying Python
  • A clear appreciation of the sequence-oriented parts of Python
  • Emphasis on the way in which Python code is structured
  • Learn how to produce bug-free code by using testing tools

Who This Book Is For

The Python Apprentice is for anyone who wants to start building, creating and contributing towards a Python project. No previous knowledge of Python is required, although at least some familiarity with programming in another language is helpful.

What You Will Learn

  • Learn the language of Python itself
  • Get a start on the Python standard library
  • Learn how to integrate 3rd party libraries
  • Develop libraries on your own
  • Become familiar with the basics of Python testing

In Detail

Experienced programmers want to know how to enhance their craft and we want to help them start as apprentices with Python. We know that before mastering Python you need to learn the culture and the tools to become a productive member of any Python project. Our goal with this book is to give you a practical and thorough introduction to Python programming, providing you with the insight and technical craftsmanship you need to be a productive member of any Python project. Python is a big language, and it’s not our intention with this book to cover everything there is to know. We just want to make sure that you, as the developer, know the tools, basic idioms and of course the ins and outs of the language, the standard library and other modules to be able to jump into most projects.

Style and approach

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.

Author(s): Robert Smallshire, Austin Bingham
Publisher: Packt Publishing
Year: 2017

Language: English
Pages: 352

Table of Contents
Preface
Errata and Suggestions
Conventions Used in This Book
Welcome Apprentice!
Python Promo
Overview
What is Python?
It's a programming language!
It's a standard library!
It's a philosophy
The journey of a thousand miles…
Chapter 1 – Getting started
Obtaining and installing Python 3
Starting Python command line REPL
Leaving the REPL
Code structure and significant indentation
Python culture
Importing standard library modules
Getting help()
Scalar data types: integers, floats, None and bool
Relational operators
Control flow: if-statements and while-loops
Summary
Chapter 2 – Strings and collections
str – an immutable sequence of Unicode code points
Moment of zen
bytes – an immutable sequence of bytes
list – a sequence of objects
dict – associating keys with values
For-loops – iterating over series of items
Putting it all together
Summary
Chapter 3 – Modularity
Organizing code in a .py file
Defining functions
Organizing our module into functions
The Python execution model
Setting up a main function with command line argument
Moment of zen
Docstrings
Comments
Shebang
Summary
Chapter 4 – Built-in types and the object model
The nature of Python object references
Argument passing semantics – pass by object-reference
Python return semantics
Function arguments in detail
The Python type system
Variable declaration and scoping
Moment of zen
Everything is an object
Summary
Chapter 5 – Exploring built in collection types
tuple – an immutable sequence of objects
The tuple constructor
Strings in action
Moment of zen
range – a collection of evenly spaced integers
list in action
Dictionaries
set – an unordered collection of unique elements
Collection protocols
Summary
Chapter 6 – Exceptions
Exceptions and control flow
Handling exceptions
Handling multiple exceptions
Programmer errors
Empty blocks – the pass statement
Exception objects
Imprudent return codes
Re-raising exceptions
Exceptions are part of your function's API
Guard clauses
Exceptions, APIs, and protocols
Choosing not to guard against TypeError
Pythonic style – EAFP versus LBYL
Clean-up actions
Moment of zen
Platform-specific code
Summary
Chapter 7 - Comprehensions, iterables, and generators
Comprehensions
Moment of zen
Iteration protocols
Generator functions
Generator expressions
``Batteries included'' iteration tools
Pulling it all together
Summary
Chapter 8 – Defining new types with classes
Defining classes
Instance methods
Instance initializers
Validation and invariants
Adding a second class
Collaborating classes
Moment of zen
Booking seats
Naming methods for implementation details
Sometimes the only object you need is a function
Polymorphism and duck-typing
Inheritance and implementation sharing
Summary
Chapter 9 – Files and resource management
Files
Context managers
Moment of zen
Binary files
File-like objects
Other resources
Summary
Chapter 10 – Unit testing with the Python standard library
Test cases
Fixtures
Assertions
Unit testing example: text analysis
Using fixtures to create temporary files
Using the new fixtures
Using assertions to test behavior
Testing for exceptions
Testing for file existence
Moment of zen
Summary
Chapter 11 – Debugging with PDB
Debugging commands
Palindrome debugging
Summary
Afterword: Just the Beginning
Appendix A – Virtual environments
Creating a virtual environment
Activating a virtual environment
Deactivating a virtual environment
Other tools for working with virtual environments
Appendix B – Packaging and distribution
Configuring a package with distutils
Installing with distutils
Packaging with distutils
Appendix C – Installing third-party packages
Introducing pip
The Python Package Index
Installing local packages with pip
Uninstalling packages