Python Essentials

Book description

Modernize existing Python code and plan code migrations to Python using this definitive guide

  • Learn the essentials of Python programming to get you up and coding effectively
  • Get up-to-speed with the most important built-in data structures in Python, using sequences, sets, and mappings
  • Explore typical use cases for various features in Python through this compact guide

In Detail

Python Essentials provides a vital tour of the most critical features of Python. Starting with setup and installation, you will soon dive into exploring built-in-library types, Python's rich collection of operators and built-in functions, variables, assignment and scoping rules.

From this foundation, you will explore functions, a crucial aspect of any programming language, including considerable sophistication in defining parameters to a function and providing argument values. Explore advanced functional programming using generator expressions, comprehensions, and generator functions. Handle file input and output using web services and context managers, exception handling and explore wider, popular frameworks.

Through this concise and practical guide, you will explore all you need to know to leverage this powerful, and industry-standard, programming language.

What You Will Learn

  • Use Python interactively and master the art of writing efficient Python scripts
  • Learn techniques for working with string data, including how to use regular expressions to parse strings
  • Get to know how the short-circuit and if-else logic operators work
  • Explore mutable data structures including list, set, and dict
  • Define functions with positional and optional parameters, and learn to use keyword argument values when calling a function
  • Write generator functions, generator expressions, and comprehensions
  • Master the use of exceptions for handling errors and other unexpected conditions
  • Understand the importance of working with files, context manager, and the with statement

Table of contents

  1. Python Essentials
    1. Table of Contents
    2. Python Essentials
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Getting Started
      1. Installation or upgrade
        1. Installing Python on Windows
          1. Considering some alternatives
        2. Upgrading to Python 3.4 in Mac OS X
          1. Adding the Tkinter package
        3. Upgrading to Python 3.4 in Linux
      2. Using the Read-Evaluate-Print Loop (REPL)
        1. Confirming that things are working
        2. Doing simple arithmetic
        3. Assigning results to variables
        4. Using import to add features
      3. Interacting with the help subsystem
        1. Using the pydoc program
      4. Creating simple script files
        1. Simplified syntax rules
      5. The Python ecosystem
        1. The idea of extensibility via add-ons
        2. Using the Python Package Index – PyPI
          1. Using pip to gather modules
          2. Using easy_install to add modules
          3. Installing modules manually
      6. Looking at other Python interpreters
      7. Summary
    9. 2. Simple Data Types
      1. Introducing the built-in operators
        1. Making comparisons
        2. Using integers
          1. Using the bit-oriented operators
        3. Using rational numbers
        4. Using decimal numbers
        5. Using floating-point numbers
        6. Using complex numbers
      2. The numeric tower
      3. The math libraries
      4. Using bits and Boolean values
      5. Working with sequences
        1. Slicing and dicing a sequence
      6. Using string and bytes values
        1. Writing string literals
        2. Using raw string literals
        3. Using byte string literals
        4. Using the string operators
        5. Converting between Unicode and bytes
        6. Using string methods
        7. Accessing the details of a string
        8. Parsing strings into substrings
      7. Using the tuple collection
      8. The None object
      9. The consequences of immutability
      10. Using the built-in conversion functions
      11. Summary
    10. 3. Expressions and Output
      1. Expressions, operators, and data types
        1. Using operators on non-numeric data
      2. The print() function
      3. Examining syntax rules
      4. Splitting, partitioning, and joining strings
        1. Using the format() method to make more readable output
      5. Summary of the standard string libraries
        1. Using the re module to parse strings
          1. Using regular expressions
          2. Creating a regular expression string
          3. Working with Unicode, ASCII, and bytes
        2. Using the locale module for personalization
      6. Summary
    11. 4. Variables, Assignment and Scoping Rules
      1. Simple assignment and variables
      2. Multiple assignment
        1. Using repeated assignment
      3. Using the head, *tail assignment
      4. Augmented assignment
      5. The input() function
      6. Python language concepts
        1. Object types versus variable declarations
        2. Avoiding confusion when naming variables
        3. Garbage collection via reference counting
        4. The little-used del statement
      7. The Python namespace concept
        1. Globals and locals
      8. Summary
    12. 5. Logic, Comparisons, and Conditions
      1. Boolean data and the bool() function
      2. Comparison operators
        1. Combining comparisons to simplify the logic
        2. Testing float values
        3. Comparing object IDs with the is operator
        4. Equality and object hash values
      3. Logic operators – and, or, not, if-else
        1. Short-circuit (or non-strict) evaluation
      4. The if-elif-else statement
        1. Adding elif clauses
      5. The pass statement as a placeholder
      6. The assert statement
      7. The logic of the None object
      8. Summary
    13. 6. More Complex Data Types
      1. The mutability and immutability distinction
      2. Using the list collection
        1. Using list operators
        2. Mutating a list with subscripts
        3. Mutating a list with method functions
        4. Accessing a list
      3. Using collection functions
      4. Using the set collection
        1. Using set operators
        2. Mutating a set with method functions
        3. Using augmented assignment with sets
        4. Accessing a set with operators and method functions
      5. Mappings
        1. Using dictionary operators
        2. Using dictionary mutators
        3. Using methods for accessing items in a mapping
        4. Using extensions from the collections module
      6. Processing collections with the for statement
        1. Using literal lists in a for statement
        2. Using the range() and enumerate() functions
      7. Iterating with the while statement
      8. The continue and break statements
        1. Breaking early from a loop
      9. Using the else clause on a loop
      10. Summary
    14. 7. Basic Function Definitions
      1. Looking at the five kinds of callables
      2. Defining functions with positional parameters
        1. Defining multiple parameters
        2. Using the return statement
        3. Evaluating a function with positional or keyword arguments
        4. Writing a function's docstring
      3. Mutable and immutable argument values
      4. Defining optional parameters via default values
        1. A warning about mutable default values
      5. Using the "everything else" notations of * and **
        1. Using sequences and dictionaries to fill in *args and *kw
      6. Nested function definitions
      7. Working with namespaces
        1. Assigning a global variable
        2. Assigning a non-local variable
      8. Defining lambdas
      9. Writing additional function annotations
      10. Summary
    15. 8. More Advanced Functions
      1. Using the for statement with iterable collections
        1. Iterators and iterable collections
      2. Consequences and next steps
      3. Using generator expressions and comprehensions
        1. Limitations of generator expressions
        2. Using multiple loops and conditions
        3. Writing comprehensions
      4. Defining generator functions with the yield statement
      5. Using the higher-order functions
        1. Writing our own higher-order functions
      6. Using the built-in reductions – max, min, and reduce
      7. Three ways to sort a sequence
        1. Sorting via a key function
        2. Sorting via wrapping and unwrapping
      8. Functional programming design patterns
      9. Summary
    16. 9. Exceptions
      1. The core exception concept
        1. Examining the exception object
      2. Using the try and except statements
        1. Using nested try statements
      3. Matching exception classes in an except clause
        1. Matching more general exceptions
        2. The empty except clause
      4. Creating our own exceptions
      5. Using a finally clause
      6. Use cases for exceptions
      7. Issuing warnings instead of exceptions
      8. Permission versus forgiveness – a Pythonic approach
      9. Summary
    17. 10. Files, Databases, Networks, and Contexts
      1. The essential file concept
        1. Opening text files
        2. Filtering text lines
        3. Working with raw bytes
        4. Using file-like objects
      2. Using a context manager via the with statement
        1. Closing file-like objects with contextlib
      3. Using the shelve module as a database
        1. Using the sqlite database
        2. Using object-relational mapping
      4. Web services and Internet protocols
      5. Physical format considerations
      6. Summary
    18. 11. Class Definitions
      1. Creating a class
      2. Writing the suite of statements in a class
      3. Using instance variables and methods
      4. Pythonic object-oriented programming
        1. Trying to do type casting
        2. Designing for encapsulation and privacy
      5. Using properties
      6. Using inheritance to simplify class definitions
        1. Using multiple inheritance and the mixin design pattern
      7. Using class methods and attributes
        1. Using mutable class variables
        2. Writing static methods
      8. Using __slots__ to save storage
      9. The ABCs of abstract base classes
        1. Writing a callable class
      10. Summary
    19. 12. Scripts, Modules, Packages, Libraries, and Applications
      1. Script file rules
      2. Running a script by the filename
        1. Running a script by its module name
        2. Running a script using OS shell rules
        3. Choosing good script names
      3. Creating a reusable module
      4. Creating a hybrid library/application module
      5. Creating a package
      6. Designing alternative implementations
      7. Seeing the package search path
      8. Summary
    20. 13. Metaprogramming and Decorators
      1. Simple metaprogramming with decorators
      2. Defining our own decorator
      3. More complex metaprogramming with metaclasses
      4. Summary
    21. 14. Fit and Finish – Unit Testing, Packaging, and Documentation
      1. Writing docstrings
      2. Writing unit tests with doctest
      3. Using the unittest library for testing
        1. Combining doctest and unittest
      4. Using other add-on test libraries
      5. Logging events and conditions
        1. Configuring the logging system
      6. Writing documentation with RST markup
        1. Creating HTML documentation from an RST source
        2. Using the Sphinx tool
      7. Organizing Python code
      8. Summary
    22. 15. Next Steps
      1. Leveraging the standard library
      2. Leveraging PyPI – the Python Package Index
      3. Types of applications
      4. Building CLI applications
        1. Getting command-line arguments with argparse
        2. Using the cmd module for interactive applications
      5. Building GUI applications
        1. Using more sophisticated packages
      6. Building web applications
        1. Using a web framework
        2. Building a RESTful web service with Flask
      7. Plugging into a MapReduce framework
      8. Summary
    23. Index

Product information

  • Title: Python Essentials
  • Author(s): Steven F. Lott
  • Release date: June 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781784390341