Tips and tricks#

You are now already well equipped to write more complex scripts and algorithms: you know (almost ;)) everything you need to know about the syntax, but you might be a little lost in all the possibilities that the python language is now offering to you. Which tool should I use to do this and that? What was this function signature again? are very common questions, and it is normal to feel a bit overwhelmed.

This chapter will give you some brief tips to help you find answers to these type of questions.

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 is not only for its 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 (no plotting library for example). This is why third party packages were developed. And there are so many of them that one can easily be overwhelmed.

Getting help#

In order to become a good programmer, nothing is more important than being able to find help, either online or from 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 will have to refer to

  • your favorite search engine. Ask your question as precisely as possible! With some experience you will learn to detect the sources you can trust, and learn to filter out outdated information (e.g. related to the old python 2 language). 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.

  • ChatGPT or similar AI tools are more recent additions to the list of resources. More about it in the next section (Coding and ChatGPT).

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 make your question unambiguous. Here is the example outcome of two google searches:

The latter is the one you want and that 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 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 to your teacher), please spend some time reading this resource. Believe me, this will help you to get better answers!

Getting help VS learning#

I will be honest: without an internet connection, I am a very bad python programmer. I do not expect you to learn all the python commands by heart. Similarly, I encourage you to use the internet to find help to solve the weekly exercises.

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 line between the two might be subtle, but I am sure you will know when you cross it: at the moment you stopped using 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 advantage of IDEs is that they ship with tools that make your life easier when programming.

Here is a non-exhaustive (and subjective) list of the tools a good IDE should have:

  • linter: automatic detection of syntax errors and style violations

  • debugger: helping you find where the errors in your program come from

  • intelligent code completion: automatic TAB completion, module recognition, etc.

  • project explorer and code exploration tools

  • for interactive languages: a command line and variable explorer

  • appealing colors and graphics

For more advanced uses, you will also look for version control tools and automated build and testing tools.

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

  • 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.

  • PyCharm: this is my current personal favorite. It is however quite heavyweight and can be confusing for beginners. The community version of PyCharm is free, and has enough functionality for most use cases.

  • Visual Studio Code (from Microsoft) is one of the newest “cool kids” around. I have not tested it myself, but people seem to be very happy with it. It is very minimal, and you will have to install plugins for it to become a true python 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. (Addition by M. Lehner who is committed to vim: vim has indeed a rather steep learning curve, but once you get used to it, it allows for very efficient coding. And yes, I prefer having to use the mouse as little as possible :))

Debugging code#

It is impossible to write bug free code in one go (just accept it and get used to it). There will always be mistakes. Syntax errors and other obvious problems (e.g. NameError) will most often be picked up by your IDE’s linter. The other “easier ones” (e.g. TypeError) are also likely to be corrected after a first couple of test runs of your code. But then, sometimes, the error is not obvious: the code runs, but does not produce the expected output. What to do in these cases?

  • use print statements (bad): with this method, you can inspect variables at run time by printing their values. It is a quick and dirty way to debug, and should not be used too often because it is error prone (indeed, by adding print statements you modify your code, might forget about them afterwards, and they are often not very useful). If you need more than 3 or 4 print statements to track the source of an issue, it is time for opening a debugger.

  • use a debugger (good): a debugger will allow to interrupt a program’s execution and give you access to the program’s state at a predefined location (called a break point). It is far more useful than print, because it allows you to explore many variables at once. Spyder comes with a debugger included.

  • go for a walk (very good): think about something else for a while and come back with a fresh, open mind

  • talk to your colleagues about your problem (very good): they might see the issue (or typo) from a different angle and detect the problem easily

  • if you have no one around to talk to, talk to a duck (very good)