My Top 10 Tips for Python

I was recently asked what are my top 10 tips for Python. Well, here they are (in no specific order).

  • Use Python 3 (3.9 is the latest, Anaconda is at Python 3.8 at the moment, which is absolutely fine) – Python 2 is outdated and will get no more updates. Also, Python is becoming faster with every new release, so if you do not want to waste precious computing time, use the latest (Anaconda) release.

  • To install Python, use an Anaconda installation – Anaconda is the most popular Python distribution platform; it is open source and free. Using Anaconda is just the easiest way to have control over your Python environment, and let’s you easily install and manage the desired packages. Furthermore, the performance is usually better than a Python installation that is either manually installed through PyPI/PIP (Python Package Index) or an installation that comes with your OS. And finally, you can even install Anaconda without root privileges in any directory – this comes handy if you are e.g. working on a server of your company/institute – I’ve described installation of Anaconda in a short Tutorial (it also covers how to switch between different Python versions in case you need that).

  • To get started quickly or just test a few lines of code, simply start an iPython shell (type “ipython” in a shell to start it) to try out stuff. Make sure you use the iPython shell (i.e. the interactive Python shell), not the plain Python shell – only the iPython shell has code completion and other features that make life so much easier: e.g. if you need help on any function or module, type the functions name followed by a question mark, like “print?“. Using the iPython shell is the easiest and often fastest way to try out a few lines of code. That’s also one of the core strengths of Python: rapid prototyping. – For anything a bit larger, I would recommend using a Jupyter Notebook though (see below) and writing scripts with functions or modules.

  • Get overview of the Python Standard Library and the Numpy, SciPy, Matplotlib stack. Then add Astropy (for astronomers) and Pandas for loading and working with datasets. Go to the according websites and make yourself familiar with the ecosystem. Python is said to come with “batteries included”, so there are many functions and modules that already solve a lot of common tasks. You don’t need to know them all by heart – just browse through the table of contents and get a feeling for what is there – that makes it much easier in the future if you need something and already know roughly where to find it. (for astronomers: Astropy is well suited to work with astronomical datasets and coordinates. Spend some time on the tutorials to see what it’s capable of, it’s the de-facto standard library for astronomical applications).

  • Use Jupyter Notebooks for exploratory data analysis – the best thing that ever happened to me, since I started with Python. Jupyter Notebooks are part of a full Anaconda installation (see above), and you can simply start them by typing “jupyter notebook” in your command line. You can not only write code in a notebook and visualize your results, but also add complimentary text and structure the notebook into sections. This makes Jupyter Notebooks perfect for documentation and to create nice looking reports – one of the great features if you regularly discuss your projects with a team. Once I am happy with some functionality and I intend to reuse the code, I encapsulate the functionality in functions and put them into a toolbox-module outside of the notebook, so I can import them also in other notebooks.

  • Once you start writing your own scripts or modules and packages, you will need an editor or IDE (Integraded Development Environment). For an IDE use either Spyder or PyCharm – I personally do not use any of them but use Eclipse plus PyDev, but that’s because I also manage Software in other languages like C++ and Eclipse allows to handle many different programming languages. So if you do not have any need for that, try out either of the two aforementioned IDEs – they are the ones usually referred to when someone talks about a Python IDE. Get to know them (esp. keyboard shortcuts) – you don’t want to use a hammer in order to drive a screw into wood.

  • Once you are writing code, try to follow the Style Guide for Python Code (aka. PEP 8; https://www.python.org/dev/peps/pep-0008/) as much as possible – especially make sure you always use 4 spaces instead of tabs for indentation – once you set up your editor and get used to it, it makes life in Python so much easier. Four spaces are the recommended standard indentation, and that’s what you will find in all examples you run into on the Web – and you do not want to change indentation when copying code from the Web. – Following PEP 8 from the start makes it much easier to maintain your personal Python library on the long run.

  • For performance: avoid loops (and esp. loops within loops), use build-in functions and Numpy vectorization (i.e. use array operations rather than looping over 10,000 vectors). Make use of as many Numpy and Python build-in functions as possible, as these are mostly written in C and optimized far beyond anything you will ever be able to achieve yourself, if you are not a god of CPU and memory architecture.

  • What else? – Well, depends on what you want to achieve – Python has an extremely diverse ecosystem. It can be used for many different tasks, and therefore I suggest before you start coding something, check if this has not been done before and is maybe available as a package on PyPI. Also searching for similar projects on Github is a good starting point. And finally there is Stack Overflow…. if you do not know about it, this is where you will find answers (and don’t be shy to ask questions).

And no, you counted correct: these are 9 and not 10 points. There are many more tips for Python users. It mostly depends on your individual starting point and your personal goals. Are you already an expert programmer, and just make the switch to Python or is programming a new thing for you? What will you use Python for? Game development? Web-Design? Data Science? – There are many free resources out there, that help you getting started. If you want to get into or dive deeper into data science, maybe my blog-post one continuous learning is interesting for you.