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

Code structure and significant indentation


Start your Python 3 interpreter:

> python

If on Windows or:

$ python3

On Mac or Linux.

The control flow structures of Python, such as for-loops, while-loops, and if-statements, are all introduced by statements which are terminated by a colon, indicating that the body of the construct is to follow. For example, for-loops require a body, so if you enter:

>>> for i in range(5):
...

Python will present you with a prompt of three dots to request that you provide the body. One distinctive (and sometimes controversial) aspect of Python is that leading whitespace is syntactically significant.

What this means is that Python uses indentation levels, rather the braces used by other languages, to demarcate code blocks.By convention, contemporary Python code is indented by four spaces for each level.

So when Python present us with the three dot prompt, we provide those four spaces and a statement to form the body of the loop:

...     x = i * 10

Our loop body will contain a second statement, so after pressing Return at the next three dot prompt we'll enter another four spaces followed by a call to the built-in print() function:

...     print(x)

To terminate our block, we must enter a blank line into the REPL:

...

With the block complete, Python executes the pending code, printing out the multiples of 10 less than 50:

0
10
20
30
40

Looking at at screenful of Python code, we can see how the indentation clearly matches — and in fact must match — the structure of the program which is as follows:

Figure 1.1: Whitespaces in the code

Even if we replace the code by gray lines, the structure of the program is clear as shown in the following image:

Figure 2.2 : Replaced code with grey lines

Each statement terminated by a colon starts a new line and introduces an additional level of indentation, which continues until a dedent restores the indentation to a previous level. Each level of indent is typically four spaces, although we'll cover the rules in more detail in a moment.

Python's approach to significant whitespace has three great advantages:

  1. It forces developers to use a single level of indentation in a code-block. This is generally considered good practice in any language because it makes code much more readable.
  2. Code with significant whitespace doesn't need to be cluttered with unnecessary braces, and you never need to have code-standard debates about where the braces should go. All code-blocks in Python code are easily identifiable and everyone writes them the same way.

 

 

  1. Significant whitespace requires that a consistent interpretation must be given to the structure of the code by the author, the Python runtime system and future maintainers who need to read the code. As a result you can never have code that contains a block from Python's point of view, but which doesn't look like it contains a block from a cursory human perspective.

The rules for Python indentation can seem complex, but they are quite straightforward in practice:

  • The whitespace you use can be either spaces or tabs. The general consensus is that spaces are preferable to tabs, and four spaces has become a standard in the Python community.

  • One essential rule is NEVER to mix spaces and tabs. The Python interpreter will complain, and your colleagues will hunt you down.

  • You are allowed to use different amounts of indentation at different times if you wish. The essential rule is that consecutive lines of code at the same indentation level are considered to be part of the same code block.

  • There are some exceptions to these rules, but they almost always have to do with improving code readability in other ways, for example by breaking up necessarily long statements over multiple lines.

This rigorous approach to code formatting is Programming as Guido intended it or, perhaps more appropriately, as Guido indented it! A philosophy of placing a high value on code qualities such as readability gets to the very heart of Python culture, something we'll take a short break to explore now.