After having learned how to set up a working python environment with mamba
from scratch or from an environment specification file in the previous chapters, we will now refocus this tutorial on how to write a python code. The process of writing, editing, and improving code and its structure is referred to the term code development. Basically, you can develop your Python code in three different ways:
There is a wide range of different kinds of text editors that depend on your local operating system. The most prominent ones are the classical text editor or the notepad on windows, vi or vim for linux kernel based operating systems. They all have in common that they provide a very simple interface with limited functionality to process basic text input. Although it is still possible to use this kind of editor for writing simple Python codes, we do not recommend these for code development purposes.
Nevertheless, we can use editors like these to write and execute our first, very simple Python file! Open your text editor of choice and create a new, blank file. Afterwards write the following command in the first line of the file:
print("Hello World")
Hello World
Save this file to your file system with .py as file ending. The actual filename does not matter. For instance you can save your file as:
myFirstProgram.py
In the next step start your mamba
environment with Python preinstalled and navigate in your command line interface to the location, where you python file is stored on your local file system. For instance:
Now you can execute your Python file by the following command over your command line interface:
python myfirstprogramm.py
The program will exactly follow our instructions. We implemented that our programm should print
the phrase hello world:
Generally you can use the print()
statement to display text on the command line interface. Beware that you must put your text output in ""
or ''
. Just play around with the print
command, edit your python file and try to display your name for example on the command line. Just overwrite your file after you have done your edits and rerun your python file as shown before.
In contrast to simple text editors that are limited to basic text operation functions, rich text editors are enhanced software tools especially designed for working with text. They empower you to use individual formats to characters, paragraphs, sections, or the whole document. Furthermore, they allow text and syntax highlighting appropriated to your programming languages. These additional features make your work much more convenient. In the following an overview of the most popular rich text editors for windows, linux and Mac OS is given.
Note, there are many rich text editors out there, here we just mention three of them.
Although these editors come with a lot of additional features, we would still not recommend them for professional code development purposes. Nevertheless it is beneficial to have at least one of these editors installed.
Notepad++ is a great rich text editor for nearly every purpose. It is derived as open-source software product and originally written in C++. It supports more than 50 different programming languages, but is only available for Windows. You can also choose between different color schemes. Furthermore, you have the option to install additional custom packages that help you to make time spending task much easier.
Visual Studio Code (VSCode) is probably one of the most used editors for programming purposes out there! It provides great syntax highlighting and auto-complete features which will improve your coding experience a lot. VSCode supports a wide range of different programming languages and it comes with a whole ecosystem of extensions for those. Definitely a recommendation.
Atom is a relatively new editor with a lot of potential! Atom is developed and distributed by the Github team. It has a built-in package manager for installing new packages or start creating your own within this cool tool. Atom comes pre-installed with four UI and eight syntax themes in a variety of colors. The rich and supportive community also creates cool themes for everybody to use so you might find what you’re looking for there. It is available for all common operating systems. Highly recommended!
Besides text editors an integrated development environment (IDE) combines the writing of code with the execution of code, so you do not have to execute your code manually from your CLI. Instead, you can run your written code directly in the same software! Although this one of the biggest advantages of using an IDE for code development purposes, IDEs comes with a lot of additional features that will make your coding experience way more convenient. As programmers have several different preferences there are a lot of great IDEs, sometimes specialized for certain programming languages. For the purposes of this tutorial the IDE spyder is a good choice, especially if you have already worked with RStudio in the past. You will directly see the similarity to RStudio:
This IDE suits perfectly for scientists, engineers, and data analysts. It offers a unique combination of the advanced editing, analysis, debugging, and profiling functionality of a comprehensive development tool with the data exploration, interactive execution, deep inspection, and beautiful visualization capabilities of a scientific package.
To work with spyder you can easily install it to your mamba
environment of choice over:
mamba install spyder
Afterwards you can start this IDE easily over the command line with:
spyder
Besides Spyder it is definitely worth to have a look on PyCharm as it is one of the most popular IDE for Python. Like shown above you can easily PyCharm with mamba
.
Besides these desktop IDEs there are also web browser based IDE. You work with them in the same browser you are reading this tutorial with. The most popular and successful browser based IDE for Python is JupyterLab.
JupyterLab is distributed by the non-profit and open-source Project Jupyter, firstly published in 2014. Although it is a great IDE for developing Python code, you can use JupyterLab for every other language too. The most essential advantage of using JupyterLab as IDE of choice is the possibility to share documents that contain live code, equations, visualizations, and narrative text togehter in one file! These features make Jupyter Notebooks really benefitial for a lot of different purposes.
For using JupterLab just install it to your local mamba
environment by:
mamba install jupterlab
We would also like to recommend to start your experience with JupyterLab through this tutorial. Therefore, you will find the possibilty to download every chapter of this Python tutorial as jupyter notebook file, since this guide is originally written in JupyterLab.
A dedicated guide, how to use and work with JupyterLab is provided in the next chapter.
Citation
The E-Learning project SOGA-Py was developed at the Department of Earth Sciences by Annette Rudolph, Joachim Krois and Kai Hartmann. You can reach us via mail by soga[at]zedat.fu-berlin.de.
Please cite as follow: Rudolph, A., Krois, J., Hartmann, K. (2023): Statistics and Geodata Analysis using Python (SOGA-Py). Department of Earth Sciences, Freie Universitaet Berlin.