Tips and tricks

At this stage of the lecture, you are now equipped to write more complex scripts and algorithms. You know everything you need to know about the syntax (almost ;-)), but you might be a little lost in all the possibilities the language is now offering to you. Which tool should I use to do this and that? How was this function signature again? This chapter will briefly give you some tips to help you out during your assignments.

Batteries included

You may have read this expression ("batteries included") during your research about the Python language. It is widely used to indicate that if you choose python, it's not only for it's syntax: it is for all the tools that come with it. And there are PLENTY.

Spend a minute or two to scroll through the list of tools available in the standard library. The standard library is available per default with any python installation. It is very robust and provides the core functionality of the language.

As you will see, there is not much for us scientists in this list though. This is why third party packages were developed. And there are so many of them that one can easily be overwhelmed: for this week's assignment we will use the standard library only.

Getting help

In order to become a good programmer, nothing is more important than being able to find help, either online or with your friends and colleagues.

Online resources

Fortunately, getting help has become quite easy nowadays. Here are some resources to get you started:

  • the official python documentation is definitely the best place to look for everything related to the standard library. It is also THE reference, and as such can sometimes be too detailed or complex. If this fails, then you'll have to refer to
  • your favorite search engine. Ask your question as precisely as possible! With some experience you'll learn to detect the sources you can trust, and learn to filter out outdated information (e.g. related to python 2). Very often your web search will lead you to
  • Stackoverflow. In recent years, the Stack Exchange community of sites has emerged as a major resource for answering technical and other questions and is even the preferred forum for many open-source projects. Their system of rewards for well formulated questions and correct answers clearly makes finding the right solution easier than in many other forums.

Here is a general rule. When you encounter a problem, ask yourself the following question: is it likely that someone else had this problem before me? In a large majority of the cases, the answer will be "yes" (believe me on this one). The trick is to find the answer to your question: use as few words as possible, but put in all the keywords that makes your question unambiguous. Here is the example outcome of two google searches:

The latter is the one you want and will lead you to the right answer. With time, you are going to get used to using the search engine more efficiently, but if everything else fails, then you might have to as ask a question yourself.

Ask questions the smart way

If you want to ask a question on stackoverflow or somewhere else (e.g. by sending an email at your teacher), please spend some time reading this resource. Believe me, this will help you to get better answers!

Getting help VS learning

I'll make a confession: without an internet connection, I am a very bad python programmer. And I don't expect you to learn all the python commands by heart and solve the assignments without online help. However, there is a difference between "getting help to find how to realize a specific task" and "asking for someone to do my homework for me". The border between the two might be subtle, but I'm sure you'll know when you cross it: at the moment you stopped to use your brain and simply copy/pasted code from one site to your assignment script, you crossed it.

Python IDEs

While it is possible to write python programs using a basic text editor, the majority of you will find it useful to use an integrated development environment (short: IDE) instead. The advantages of IDEs is that they ship with tools that make your life easier when programming. Here is a non-exhaustive list of the tools a good IDE should have:

  • linter: automatic detection of syntax errors and style violations
  • debugger
  • Intelligent code completion
  • project explorer and code exploration tools
  • for interactive languages: a command line and variables
  • version control tools
  • automated build and testing tools
  • appealing colors and graphics
  • ...

There are many Python IDEs, but here are some of the ones that come up most often as recommendations on forums or blogposts:

  • Spyder: the IDE made by and for scientists. It resembles the Matlab IDE and has many nice features such as variable exploration and an integrated ipython console. It is very easy to install with conda, and is therefore available on linux at the university ($ spyder &). It is my recommended IDE for this semester's course.
  • PyCharm: this is my personal favorite. It is however quite heavyweight and not recommended for beginners.
  • Atom: a modern, very nice looking editor. Useful if you program in more than one language, but it must be tuned a bit to become a real IDE
  • vim or emacs: for people allergic to the mouse. Not recommended for most of us, but the people who learned to use them are (allegedly) the fastest programmers ever.

What's next?