The Writing Idiomatic Python Book


Idiomatic Python Code is What Separates Novices From Experts

Luckily, learning how to write it just got a whole lot easier...

The Writing Idiomatic Python book is finally here! Chock full of code samples, you'll learn the "Pythonic" way to accomplish common tasks. Each idiom comes with a detailed description, example code showing the "wrong" way to do it, and code for the idiomatic, "Pythonic" alternative.


This book is perfect for you

You'll find Writing Idiomatic Python a valuable tool. If you don't, I'll refund you the full price of the book (which is still yours to keep). Why offer a money back guarantee on a book? I like having that extra level of comfort, and I'm guessing you do too. If you're not satisfied for any reason, just email me at jeff@jeffknupp.com within 30 days of your purchase.

Note The book is still in the "beta" stage, with corrections and new content added frequently. If you order now (at the lower, preorder price), you'll receive the 74 page PDF or ePub today and you'll get every update as soon as it's written, automatically

Start writing better Python right now

Choose the version that's right for you

Writing Idiomatic Python comes in two versions: one for Python 2.7.3+ and one for Python 3.3+. Each has a number of idioms unique to their Python version.

Also available on:
Amazon (Kindle)

Writing Idiomatic Python 2.7.3
Writing Idiomatic Python 3.3

Google Play Books

Writing Idiomatic Python 3.3
Writing Idiomatic Python 2.7.3


Here's a peek inside the book...

What is "Idiomatic" Python?

1.1.1 Use the enumerate function in loops instead of creating an “index” variable

Programmers coming from other languages are used to explicitly declaring a variable to track the index of a container in a loop. For example, in C++:

    for (int i=0; i < container.size(); ++i)
    {
        // Do stuff
    }

In Python, the enumerate built-in function handles this role.

1.1.1.1 Harmful
    my_container = ['Larry', 'Moe', 'Curly']
    index = 0
    for element in my_container:
        print ('{} {}'.format(index, element))
        index += 1
1.1.1.2 Idiomatic
    my_container = ['Larry', 'Moe', 'Curly']
    for index, element in enumerate(my_container):
        print ('{} {}'.format(index, element))


The Table of Contents

  • Control Structures and Functions
    • For loops
    • Functions
    • If Statements
  • Working with Data
    • Classes
    • Dictionaries
    • Lists
    • Strings
    • Tuples
    • Variables
    • Generators
    • Context Managers
    • Sets
  • Organizing Your Code
    • Formatting
    • Imports
    • Modules and Packages
    • Executable Scripts
  • General Advice
    • Avoid Reinventing the Wheel
    • Modules of Note

Every programming language has its own idioms. Programming language idioms are nothing more than the generally accepted way of writing a certain piece of code. In Python, for example, there are many ways to write code that loops over the elements of a list. You could write:


        i = 0
        while i < len(my_list):
            # do stuff...
            i += 1
            

This would work just fine. But it's not idiomatic (or "Pythonic"). The idiomatic version is:

for i in my_list: 
OK, But Why Should I Care?

Consistently writing idiomatic code has a number of important benefits:

  • Others can read and understandyour code easily
  • Others can maintain and enhance your code with minimal effert
  • Your code will contain fewer bugs
  • Your code will teach others to write correct code without any effort on your part
Sounds great, but I'm new to Python. How do I learn to write idiomatic code?

This was always the toughest part. Previously, the only ways to learn idiomatic Python were to

  • Read existing code (but how do you know if it's idiomatic?)
  • Get feedback during a code review (which only helps if you're working with others, you do code reviews, and they know what is idiomatic)
  • Buy a general book on the language (which may use idioms, but probably won't point them out or explain why they're important)
There should be an easier way!

There is! Writing Idiomatic Python contains the most common and important Python idioms in a format that maximizes identification and understanding. Each idiom is presented as a recommendation to write some commonly used piece of code (like the for loop above). It is followed by an explanation of why the idiom is important. It also contains two code samples: the "Harmful" way to write it and the "Idiomatic" way.

  • The "Harmful" way helps you identify the idiom in your own code.
  • The "Idiomatic" way shows you how to easily translate that code into idiomatic Python.

Can't afford the book?

If your financial situation doesn't afford you the opportunity to purchase the book, email me at jeff@jeffknupp.com and I'll happily send you a free copy. Just be sure to note in your email which version you'd like.

Questions or comments about the book?

I'm always eager to hear feedback, both positive and negative. I encourage you to email me at jeff@jeffknupp.com. I do my best to respond to all email within 24 hours.


About the author

I'm Jeff Knupp, professional software developer and frequent blogger. Check out my blog or follow me on twitter.

Keep up to date!

Sign up for the free jeffknupp.com email newsletter to get updates about Writing Idiomatic Python. Sent roughly once a month, it also includes articles on Python programming, scalable web development, and growing your freelance consultancy. And of course, you'll never be spammed, your privacy is protected, and you can opt out at any time.