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

list in action


We've already covered lists a little, and we've been making good use of them. We know how to create lists using the literal syntax, add to them using the append() method, and get at and modify their contents using the square brackets indexing with positive, zero-based indexes.

Zero and positive integers index from the front of a list, so index four is the fifth element in the list:

Figure 5.5:  Zero and positive integers index

Now we'll take a deeper look.

Negative indexing for lists (and other sequences)

One very convenient feature of lists (and other Python sequences, for this applies to tuples too) is the ability to index from the end, rather than from the beginning. This is achieved by supplying negative indices. For example:

>>> r = [1, -4, 10, -16, 15]
>>> r[-1]
15
>>> r[-2]
-16

Negative integers are −1 based backwards from the end, so index −5 is the last but fourth element as shown in the following diagram:

Figure 5.6: Reverse index

This is much...