Starting from −273.15 °C
Where to start
You have never written a piece of code and want to get started? You have not programmed anything except Karel the robot in middle school? Your only programming experience with coding is an undergrad course on some outdated or niche language that your local quantum physics group was using?
In any case, if you want to pick up coding, you need to figure out where to start. I will make the case that as a scientist (or a generalist) your most sensible choice will usually be to start with Python. The reasons are simple: Python is easy to learn, has a broad user base (which means there is a plethora of tutorials and answers to common problems), and it has many optional packages for a wide range of use cases, especially for solving scientific problems (pandas for data science / statistics and scikit-learn for machine learning are just two major ones. There are thousands of packages for specialized applications).
Common critiques of Python mention its comparatively low speed and dynamic typing. The latter is a quite technical argument and you will likely benefit from the user-friendly dynamic typing and not notice its downsides. That Python is slower than say C, will not bug you much for most cases, especially since many Python packages where speed really matters (such as NumPy which handles large matrices) are implemented in C, they just pleasantly hide it from you.
They major reason not to use Python would be some very specific requirements on your side. If you definitely, definitely, need to use C to solve your problem, you will have to walk that path. In any other case, there’s a high chance that Python should be your first choice. It’s more versatile than most programming languages, especially including domain-specific tools you might be familiar with (e.g. MATLAB, Igor,…) and it beats the other highly popular, very versatile programming language Java, as Python has grown an extensive collection of out-of-the-box solutions for scientific problems.
So, let’s finish the talking and start out!
Get your Python running
To get started with Python, download the version suitable for your system from python.org and install.
Depending on your operating system, the installer might have the option to “Add Python to PATH”.
This allows you to call python from a terminal (in the case of Windows, this would be the command line), so check it. You should not activate this option if you plan to use different versions of python on your system. But as this is a beginner’s guide, you probably don’t right now. Click “Install Now” and the installer should finish without further obstacles. For MacOS, a similar installer is available and for other UNIX flavors, I recommend installing through the respective package manager.
If you read similar instructions, you might have been distracted by the recommendations to (a) use an environment manager for the installation and (b) use virtual environments. While both are great ideas, you don’t want to worry about these complications now. You don’t need them to write and run code, so we’ll cover them in a later tutorial.
Say “Hello World!”
You now have a working Python installation, so let’s give it a test drive!
Open your terminal (or “cmd” on Windows) and type python
.
You should see something like this:
Notice how the prompt has changed from displaying your current directory to showing >>>
.
This indicates you are now in a Python shell, i.e. you can speak with Python interactively.
Try giving it a few simple calculations by just typing e.g. 3 + 2
and hitting return.
Python will display the result immediately.
Note: If you could not open Python from the terminal, getting a response that roughly says “command ‘python’ not found”, you still need to add Python to your path. The process differs by operating system but is generally simple (make use of your favorite search engine).
MacOS users: MacOS has an outdated Python2.7 installed, which might open instead of the version you just installed (check the version number in the top line after typing python).
To conclude our first Python experience, let’s give Python an explicit command instead of the calculator tasks above. Type the command below and hit return.
print('Hello World!')
While this is a dead simple example, it should teach you a few things about the Python syntax:
- To print output that the user needs to read, use
print()
- Python functions, like the
print()
command above, usually consist of a command word followed by brackets. Within the brackets, you give the arguments that the function needs to operate. - ‘Hello World!’ is an example of a string, i.e. a sequence of characters such as text. It’s always enclosed in single
''
or double""
quotes. Strings are one (ubiquitous) example of data types. There are other data types e.g. for numbers and we’ll get to know them soon.
Alright, close your terminal, take a sip of your coffee, and pat your shoulder for actually getting started with Python. In the next post, we are gonna transition away from the interactive mode you just got to know and start writing scripts that we can save and reuse whenever we need them. But before that, let’s consider what to do when you ever hit a roadblock (you will).
When you are stuck
Get used to the thought that this will happen a lot. Fortunately for you, a lot of other people got stuck on the exact same thing in the past and so there is tons of helpful material out there. Let’s split these into your two major use cases:
How do I do X?
If you have a plan but no idea how to make python get it, these are helpful:
- Stack Overflow
- Cheat sheets: There are many cheat sheets around for Python and some of the bigger packages (read: add-ons) which are incredibly helpful when you just can’t remember what all these functions are called again
How does this specific function/attribute/class/… work?
If you already know the name of the thing you need and you wonder about what inputs it takes, what outputs it returns, what data types it accepts, all these technical details that often go wrong, then you want to consult the documentation. The complete documentations are often called “library reference” or “API reference” (the one for Python is here). They are challenging at the start but will get easier to read once you are a bit familiar with the computer scientist lingo.
The other quick way to find information on a specific bit of code is built into Python itself.
In interactive mode, just call help()
on whatever gives you trouble, e.g.
help(print())
Python will print a (rather technical) explanation of whatever function, class, module, etc.) you ask it about. These might be hard to understand in the beginning owing to the extensive use of computer scientist lingo, but you will get used to it. You might even find them very helpful to lookup implementation details (how many arguments does this function take? Which datatype does it accept as argument X? …)
Recap
In this post you learned:
- Why Python is generally a good choice for most scientists’ use cases
- How to install Python
- Using Python in interactive mode
- The
print()
function - Where to look for help