10 Time-saving Tips for Pythonists

By Abder-Rahman Ali
We teamed up with SiteGround
To bring you the latest from the web and tried-and-true hosting, recommended for designers and developers. SitePoint Readers Get Up To 65% OFF Now

time-saving tips for pythonists

Python is a beautiful language, able to inspire love in its users. So if you're looking to get into programming, or if you're a bit tired of C++, Perl, Java and the rest, I recommend you give Python a try.

Python has many features that make it attractive to programmers. It's easy to learn, object oriented, byte-compiled, free and open-source. It also has run-time type checking, full and fast support, and extensive libraries for performing various tasks.

Efficiency with Python

In this article, I want to highlight some of the ways you can save time in Python and thus maximize productivity.

In preparation, I quizzed several Pythonists on their best time-saving tips. Here are the results …

1. Don’t Use Semicolons

Since using semicolons is considered optional in Python—as opposed to other object-oriented programming languages—you don’t need to end each statement of your code with a semicolon.

This may seem simple and not really time wasting; but once your code stretches into thousands of lines, all these semicolons can get a bit distracting, and it's a lot of unnecessary typing.

2. Get a Good Code Editor

Choosing the right Python editor can be a huge time saver. Many new Python programmers are confused over which code editor to pick—especially with so many available.

It's very disruptive to get used to one editor and then have to change to another, so it's best to start with a good one. Make sure whichever you choose does flake8 and PEP8 in real time.

For guidance on which editor to choose, see my previous article on Which Code Editors Do Pythonists Use?

3. Follow the Python Code Style

example of Python styleFollowing the Python code style can enhance the readability of the code, and thus saves time while walking through your code. (The design philosophy of Python emphasizes the issue of code readability.)

4. Use the help() Function

Python's help() is a very handy, built-in function that can save a lot of time—for example, when searching for descriptions of other functions. You can simply run this function directly from your interpreter console.

The Python documentation describes the ways you can put this function to good use.

5. Use Libraries

There are lots of Python libraries that save you from reinventing the wheel every time.

For example, you can choose from a range of packages offered by PyPI (the Python Package Index), a repository of Python software.

scikit-image home page

A nice example of a Python library is scikit-image. It enables image processing tasks like blurring, enhancing contrast and zooming—simply by calling functions.

6. Use Cookiecutter

cookiecutter logoCookiecutter is a command-line utility that enables you to create all your Python project boilerplates from project templates, and this can be a big time saver.

7. Comment Rigorously

Getting in the habit of commenting your code will save you—and others—a lot of time, especially down the track. (Yes, we hear this a lot, but many programmers still need to be reminded, it seems!)

Python commenting

Commenting is particularly vital when working on teams—especially ones that change a lot.

8. Test Often

Try to test every component in your program. While it may sound time-consuming, testing as you code saves a lot of time in the long run, and reassures you that there are no hidden bugs. It also reinforces your understanding of what each piece of code is actually doing.

A REPL—a read-eval-print loop—is a common means of testing your code as you go, and is often employed by Pythonists.

9. Focus and Specialize

focus and specializeA common recommendation of accomplished Pythonists is to have a specialized focus or area of expertise. There are lots of things you can do with Python—from coding for web cams to dealing with computations and mathematics.

There are great libraries available to help with these tasks, such as SimpleCV, dealing with computer vision; Biopython, a library for biological computation; and SymPy, a library for dealing with symbolic mathematics.

Digging into areas like these, and mastering a particular framework, helps you learn Python at a deeper level, master a particular style of coding (mentioned in point 3), and deal with specialized types of problems.

10. Write Code Every Day

When you get in the habit of writing Python code every day, solving problems using Python will start to become second nature. You'll start to think in Python, so to speak, and this ultimately helps you to navigate and solve issues faster.

Wrapping Up

In this brief article, I've listed the main tips I gathered while talking with Pythonists about time-saving tricks. Here are a few others that I could have added to the list.

Attend Python Events and Meetups

people at conferenceMake sure to attend events and meetups where possible. They're great for sharing experiences, best practices, tools, and other interesting topics.

It may not be obvious that this is a time-saving strategy, but learning from the experiences of others—through advice, tips and hacks—is another way to avoid reinventing the wheel.

A great event to check out is the annual PyConf.

Think on Paper

Thinking on paper—before diving straight into code—will give you the flexibility to change. Going straight to code forces you to get involved in implementation details from the outset, which is not always the best use of your time when starting a project. Paper's distraction-free mode is great for brainstorming and problem solving!

Master the Fundamentals

Lastly, it may seem obvious, but make sure to invest time in learning the fundamentals of Python. This will eventually save you a lot of time, as you'll be better prepared to tackling more complex topics.

Some good books to help with this include

It's also important to keep abreast of news as it happens, by reading blogs and articles. A great blog to follow is The Mouse Vs. The Python.

Well, I'm sure there are more things that could be added to the list. What are your best time-saving tips when programming Python? Let me know in the comments!

We teamed up with SiteGround
To bring you the latest from the web and tried-and-true hosting, recommended for designers and developers. SitePoint Readers Get Up To 65% OFF Now
  • Mayucus Florizandes

    Python is so much better (and popular) than Ruby, but here we don’t have a Python tab. =/

    • Python is one of the few languages to have an interpreter out-of-the-box on almost any GNU/Linux system. The only other popular scripting language that can say that is Perl. This makes Python great for portability (and this will only improve when all distros make the switch to Python 3).

      I also agree that Python is likely more commonly used than Ruby, but that’s in part because it’s a general purpose language. You can use it for web development (and I know a few Python web developers), but it’s also commonly used by scientists, system administrators, and academia as a language of choice. I *suspect* it’s approximately equal in popularity to Ruby when strictly looking at the language from a web development perspective, but I also feel Ruby is slowly stagnating. Time will tell. Go Python! :)

    • abderblog

      Thanks @Mayucus Florizandes and @dindinet For your comments. Can you kindly clarify what you mean by the Python tab?

      • At the top of the page. SitePoint only has tabs/categories for HTML & CSS, JS, PHP, Ruby, etc. so far. But I understand we might be able to do something about that at some point if we get more regular Python content. Which in turn probably depends on having more SitePoint readers demanding and reading Python content. ;)

        • abderblog

          Oh, I see what you mean, missed that :-) Yes, I also look forward to having a Python tab at SitePoint.

        • OphelieLechat

          Oh yeah, 100% this. We don’t have a ton of Python content yet (not enough to add it to the navigation) but if there is demand, we can make it happen!

  • dindinet

    I second that. Bring on the Python tab.

  • Ds

    Picked up Python (Over Ruby) over the weekend can’t wait to give it a spin on the next project.

    PyCharm is free from the same people that do PhpStorm (for opensource) with is a really nice IDE for anybody that wants something. I also picked up WingsIDE on steam of all places on the cheap a while back which is another decent IDE.

  • Got back from PyCon-Au Sunday night. Biggest turnout yet, with 440 people IIRC. It’s great to get back and see some Python content on SitePoint. Thanks for the tip regarding cookiecutter!

    I would suggest for the Comment Rigorously section that a comment in triple-quotes at the top of each method is recommended (which was missing from the example). eg.

    #!/usr/bin/env python

    class Arithmetic(object):
    """ Calculate things """

    def __init__(self, numbers = []):
    """ Initialise a list of numbers """

    self.numbers = numbers

    def get_averages(self):
    """ Return the average of numbers in self.numbers """

    counter = 0
    # Sum all numbers in self.numbers (same as sum())
    for number in self.numbers:
    counter += number

    return counter / float(len(self.numbers))

    if __name__ == "__main__":
    print Arithmetic([1, 2, 3, 4, 5, 6]).get_averages()

    • abderblog

      Thanks boltronics
      for your nice comment. Yes, using triple quotes at the top of each
      method seems a good practice. I also hope you enjoyed your time in
      PyCon-Au? You can kindly invite your friends to join the discussion for
      more tips from you Pythonists :-)

  • Do you want to know something really interesting that is worth paying your attention right now,a fabulous online opportunity to work for those people who want to use their free time so that they can make some extra money using their computers… I have been working on this for last two and half years and I am making 60-90 dollar/ hour … In the past week I have earned 13,70 dollars for almost 20 hours sitting ….

    Any skills, Degree ,Specific qualification is not necessary for this, just keyboard typing and a good working and reliable internet connection ….

    Any time limitations to start work is not required … You may do this work at any time when you willing to do it ….

    Do you want to know how I have been doing this?…..….see this {Iink} on my !|profile|!` to know how I am working` on this`