Jump to content

How To Tackle Python 'Head First'

+ 1
  ggiles2's Photo
Posted Nov 16 2010 01:41 PM

Five Tips for Python

By Paul Barry, author of Head First Python, publishing Nov. 17 (O'Reilly Media; $49.99 USD).

1. Don't work too hard--let Python do the work for you. When programmers of other programming languages encounter Python for the first time, they tend to write more code than is strictly necessary because they're "thinking in Java" or "thinking in C++." Python comes with so much stuff built in to its standard library that it's really often the case that the code you need has already been written and you don't need to write it again; you need to call it instead. Getting to the point where you are "thinking in Python" is the goal, because then you are starting to wield Python's full power to your advantage. And, as is so often the case with Python, you can end up writing less code to do more.

2. Don't write a loop when a comprehension will do. Python provides a thing called "comprehension syntax" for its built-in lists, dictionaries and sets which allows programmers to transform one data structure into another version of itself without resorting to iteration. Most programmers use some sort of loop for this task, but Python provides an alternative: the comprehension. Comprehensions in Python let you specify what it is you want to happen, as opposed to having to spell out the individual steps the program needs to take. This syntax is very powerful and is well worth learning. This, added to the fact that most comprehensions can be specified on a single line, makes comprehensions a stand-out Python feature.

3. Learn as you go, not all at once. There's a lot in Python, but you don't need to know all of it to be productive. In fact, the Head First format exploits this fact by concentrating on the essential stuff. It's not important that you know every single nook 'n' cranny of Python syntax; what's important is that you know when to use one Python technique or feature over another. You can always grab a good reference to get at the details (or search the web), but the concepts are the important things. So, it is important to learn as you go and learn constantly. As an example of this idea, check out Doug Hellmann's Python Module of the Week which takes a look at a specific built-in library module each week. Doug gives a quick description of the module, presents example usages and provides great explanations. It's a great example of a way to learn as you go.

4. IDLE is often good for quick jobs. A lot of Python programmers forget that Python comes with a simple IDE as standard, called IDLE. It may not be as feature-full as "real" programming development environments, but it's good in a pinch, especially if you're working on someone else's computer and just "doing something quick." IDLE is there, waiting to serve you and I always feel it's a good idea to be familiar with its basic features. IDLE really isn't all that bad and has some useful tools.

5. Don't reinvent the wheel. Check the Python Package Index (PyPI) first. The PyPI is an online treasure trove of Python code and can be found here: There are over 11,500 contributions to this code repository and chances are that there's something already in there that matches your requirements. Rather that starting from scratch, check for a match or a "nearly match" and work from there. And if you do need to invent your very own wheel for a specific requirement, consider turning it into a Python module and uploading it to PyPI. After all, it's good to share.

Paul Barry presents Python 'pearls' during a free O'Reilly Webcast on Wed., Dec. 1, at 1pm PST.

Cover of Head First Python
Learn more about this topic from Head First Python. 

Ever wished you could learn Python from a book? Head First Python is a complete learning experience for Python that helps you learn the language through a unique method that goes beyond syntax and how-to manuals, helping you understand how to be a great Python programmer. You'll quickly learn the language's fundamentals, then move onto persistence, exception handling, web development, SQLite, data wrangling, and Google App Engine. You'll also learn how to write mobile apps for Android, all thanks to the power that Python gives you.

Learn More Read Now on Safari


Tags:
0 Subscribe


0 Replies