• Featured,  General

    How to get a job programming Python

    One of the many often asked questions on forums and websites in the Python community is how one can get a job programming Python. So here are three steps you can take to find yourself a job programming Python.Step #1: Start using Python!This is the first step and the most important one. Unless you are very lucky your future employer will want some experience or at the very least a big show of interest. So start using Python for just about everything.Do you like to read slashdot reddit in the morning? Write a scraper that e-mails you the top links for your favourite subreddits. Does your obsessive compulsive boss require a certain report…

  • Python string formatting
    Code Mastery,  Featured

    String formatting like a pro

    Just about every Python programmer has used the ‘%’ operator for string formatting. But another formatting method is available using the [crayon-5c6f850a1456d831554420-i/]  method of the string object. This allows for much cleaner and more readable code than the incantations written using ‘%’ operators.

  • Code Mastery

    Dictionary creation using dict comprehensions

    In a previous Pythonic post we saw list comprehensions, an efficient Pythonic way to create lists.  I hope you got hooked and started using list comprehensions where your code could benefit from them. Similar to list comprehensions, Python has a similar feature that’s applicable to dictionaries. The fancy name? Yes, dictionary comprehensions or dict comprehenions. Without much babbling and tralala, here are dict comprehensions: [crayon-5c6f850a1481a002443086/] As you can see the basic construct for a dict comprehension is identical to a list comprehension. But instead of wrapping our construct in square brackets we surround it with curly braces. The value returned by our expression inside the dict comprehension should also return…

  • Code Mastery

    Enumerate your iterables!

    Whenever you write code similar to the following: [crayon-5c6f850a14914931722517/] you should consider making your code a bit more Python by using the builtin [crayon-5c6f850a14917748434545-i/]  function: [crayon-5c6f850a14919869387642/] The [crayon-5c6f850a1491a275709696-i/]  function takes an iterable and an optional parameter [crayon-5c6f850a1491a341793613-i/]  (defaulting to 0). The return value is a tuple containing the index of the element and the element itself. And remember, by taking an iterable the enumerate function will also work on builtin types such as dictionaries or your own objects that implement the iterable methods. More information about the [crayon-5c6f850a1491b076463147-i/]  function can be found in the Python documentation.

  • Code Mastery

    Build your lists using list comprehensions

    A common way to build lists in a program that we see is this construct [crayon-5c6f850a14a33748235168/] The basic construct here is that by looping over something (an iterable or a condition) we add items to our list. I’m sure you’ve been there and have already used this construct in some of your code. There is of course a more Pythonic way to do this, otherwise I wouldn’t have made a post about it. Let’s meet list comprehensions. At first they will seem complex and difficult to read and sometimes I write one or see one that still is! But they are actually very neat and easy to use.