At this stage of the lecture, you are now 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 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.
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. 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 but starting next week we will use external packages as well.
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.
Fortunately, getting help has become quite easy nowadays. Here are some resources to get you started:
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:
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!
I'll make a confession: 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 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 to use your brain and simply copy/pasted code from one site to your assignment script, you crossed it.
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:
TAB
completion, module recognition, etc.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 &
). It is my recommended IDE for this semester's course.It is impossible to write bug free code in one go. There will always be mistakes. Syntax errors will most often be picked up by your IDE's linter. The other "easy ones" (TypeError
, NameError
) 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?
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 start to need more than 3 or 4 print statements to track the source of an issue, it's time for using a debugger.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.Back to the table of contents, or jump to the next chapter.