How to execute python code
Welcome to the python tutorial of Welcome to the python tutorial of start science here! This tutorial will guide you through all the important topics in learning the python programming language.
Python is an interpreted, high-level, general purpose programming language.
It’s interpreted because python programs don’t need to be compiled.
It’s a high-level programming language, because of its layer of abstraction. You don’t need to write instructions which memory register to pass through which core of the CPU. This would also prohibit us from sharing code, between different processors.
It’s general purpose, because you can do almost anything with it. From math, websites, inventory management systems to computational chemistry.
Jupyter Notebooks
The easiest way to run python code is via the jupyter notebooks provided at binder:
jupyter-notebooks contain a number of so-called cells. Cells can either be code cells, or markdown cells. This text is written in a markdown cell. In these cells text is easier to read and can be formatted like these [STRIKEOUT:examples] here. You can make lists, tables and format your text nicely. These markdown cells accompany the code cells. In the code cells the magic happens. These cells contain code segments which can be executed on a cell-by-cell basis. All variables declared in the code cells are available as long as the notebook is running. This means that contrary to a code.py file a variable declared in a cell further down can be used in a cell further up, as long as the cell with the variable declaration is ran first.
How to execute code in jupyter notebooks
To execute a cell you can either use the “Run” button at menu bar on top, our you can use these key combinations:
Shift
+Enter
to run a cell and move the cursor to the next cell.Ctrl
+Enter
to run a cell without moving the cursor to the next cell.
Further PROs for jupyter notebooks
You can render graphs and images.
You can also render molecule structures.
You can add additional functionality like exercises, tables of contents for quick navigation of larger notebooks.
You can describe your code via markdown cells.
[1]:
print("Start science here!")
Start science here!
Congratulations, you executed your first python code.
The next part about the python shell can easily be skipped. Head over to the basics_00_datatypes.ipynb notebook to continue.
The python shell
Besides jupyter notebooks there’s the more basic python shell, which can also come in useful, for when jupyter notebooks are not working. To get to a python shell there are a few things you could do:
The easiest way is to head over to binder:
and open a terminal by clicking on New and then Terminal:
There are also other possibilities:
If you are currenlty using a linux operating system, you are good to go. Just open a terminal and continue.
If you are on a Windows PC you can either install the Windows subsystem for linux
or download a python installer, install it and open a python shell by searching for “python” in your start menu.
For all cases (except 2) we also want to enter python
into our current shell to start the python shell. After that we are greeted by >>>
, the python prompt. Which tells us, we can start to write python code. Try typing the following code line-by-line hitting return after every line and omitting the three chevrons >>>
at the start. The lines without chevrons are the output of the previous line, so you can check whether the expected output is correct.
>>> 1 + 3
4
>>> a = 5
>>> a
5
>>> 1 + a
6
>>> print('Hello World!')
Hello World!
To leave the python shell type quit()
:
quit()
Now it’s time to start the python tutorial with basics_00_datatypes.ipynb