Book Image

The Python Apprentice

By : Robert Smallshire, Austin Bingham
Book Image

The Python Apprentice

By: Robert Smallshire, Austin Bingham

Overview of this book

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.
Table of Contents (21 chapters)
Title Page
Credits
About the Authors
www.PacktPub.com
Customer Feedback
Preface
12
Afterword – Just the Beginning

Importing standard library modules


As mentioned earlier, Python comes with an extensive standard library, an aspect of Python that is often referred to as batteries included. The standard library is structured as modules, a topic we'll discuss in depth later. What's important at this stage is to know that you gain access to standard library modules by using the import keyword.

The basic form of importing a module is simply the import keyword followed by a space and the name of the module. For example, lets see how we can use the standard library's math module to compute square roots. At the triple-arrow prompt we type the following command:

>>> import math

Since import is a statement which doesn't return a value, Python doesn't print anything if the import succeeds, and we're immediately returned to the prompt. We can access the contents of the imported module by using the name of the module, followed by a dot, followed by the name of the attribute in the module that you need. Like many object oriented languages the dot operator is used to drill down into object structures. Being expert Pythonistas, we have inside knowledge that the math module contains a function called sqrt(). Let's try to use the following command:

>>> math.sqrt(81)
9.0