Five ways to get help in Python
Want to share your content on python-bloggers? click here.
In an earlier blog post I offered five ways to get help in R. This post serves as the Python equivalent, assuming you are working in Jupyter notebooks.
0. Web search it
This one seems obvious, but is worth pointing out. If you get an error message, plug it into a search engine. If you would like to know “How to do X…” well, chances are others are looking for how to do X as well, and the search engines want you to find that content (“How to” content is some of YouTube’s most popular.).
This can of course become a rabbit hole; it’s easy to scroll listlessly through pages of results, give up, and watch cat videos on YouTube. The more you can bring the resources to you, the better your productivity will be. That’s why working in an integrated development environment is so helpful — it’s meant to put all the resources you need under one application.
Unlike RStudio, Jupyter is not quite a full IDE, so you may need to go outside the application for help more often. (If you are looking for an IDE for working with Python, check out PyCharm. RStudio now also features Python capabilities.)
All that said, you can get some basic assistance without leaving Jupyter… such as checking the help documentation.
1. Get the help documentation with ?
To learn more about a Python function, simply place a question mark in front of its name (no parentheses) and run:
Here you can see all necessary and optional arguments of the function with additional notes and examples.
2. Check the package’s documentation
Step 0 suggested to start with a web search, but when in doubt about Python code it’s often better to go straight to the source: the package’s documentation. The Help menu links out to some of the most popular Python packages; you can find the docs for other packages with a web search.
3. Visualize your code
When it comes to data, we analysts have a simple rule: When in doubt, visualize it. The same principle can fortunately be applied to Python coding with PythonTutor.com. This free service will visually step through the code so you see exactly how inputs transform to outputs.
If your code is throwing errors and you just can’t pinpoint where in the process things are breaking down, check out this tool.
Try your own code or use my example from the previous:
4. Compose a minimally reproducible example
If you’ve gone through the steps so far are still stumped… first, take some time away from the problem and come back. It’s amazing how often getting this distance works.
Second… did you restart your computer? (That works often, too!)
If all that fails, you may be approaching the point at which you need to “phone a friend…” or at least some person on the internet.
Now, if you are going to do that, you want to make your problem as easy as possible to follow along with: preferably by including a copy-and-pasteable code program containing a small dataset, what you are trying to achieve, the steps you are taking and where you are failing. This is known as a minimally reproducible example (MRE).
If you’ve worked in R you may be familiar with the datasets that ship out of the box like iris
or mtcars
. These are great datasets to use for an MRE because everybody’s got them. Python doesn’t ship with any datasets, so you can either make your own or find a package that includes some.
I’d suggest using the seaborn
data visualization package, which comes with some standard datasets. Some of these are pretty large, so if you’d like to make your MRE more “minimal” you could even just take a few lines of the dataset like so:
There’s more to a good MRE than a dataset, so check out this post for more tips.
5. … then hit the forums
OK, you’ve searched the web, the help documentation, tried stepping through your code and wrote an MRE. It’s time to hit the forums.
There are so many great user forums out there; r/Python and StackOverflow come to mind. But you want to do your homework beforehand; the latter in particular is notorious for chewing out unprepared posters. It’s understandable — people aren’t there to provide pro bono consulting — but it can feel a little jarring.
More recently, groups have sprung up on Slack and Discord. These are also great places to network, get and give help.
6. What else?
As mentioned previously, it’s smart to have go-to resources or procedures so that when you get blocked you don’t drift aimlessly. At the same time, there’s almost always a better way of doing anything so you want to keep your steps open for improvement.
In that spirit, what go-to steps do you take when you need help in Python? Please share them in the comments.
Video notebook
The notebook used in the video demonstration follows.
Want to share your content on python-bloggers? click here.