Tips and tricks#
It is very early in the lecture, but 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? How was this function signature again? are very common interrogations, and it is normal to feel a bit overwhelmed.
This chapter will give you some brief tips to help you 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 its syntax: it’s 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: for this week’s assignment we will continue to use the standard library only, but starting next week we will use external packages as well.
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 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.
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 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’ll be honest: without an internet connection, I am a very bad python programmer. I don’t expect you to learn all the python commands by heart for the exam (yes, you will have an internet access during the exam). 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’m sure you’ll 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 advantages 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, and is therefore available on linux at the university (
$ spyder &
). It is my recommended IDE for this semester’s course.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’s very minimal, and you’ll 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.
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 doesn’t 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 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’s 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 extremely more useful than
print
, because it allows you to explore many variables at once. Spyder comes with a debugger included, and we will make a short demo in class.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)