As a starting point an introduction to the most important programming concepts, data types and control flow structures in Python is provided. This sections are for all users that are either completly new to programming languages at all or beeing new to Python. The intention is to give you as newbie a good start through this tutorial. If you are comfortable with writing code in general or if you are already experienced in coding with Python, please feel free to skip these introductory chapters.
Note: Although this general introduction to Python will explain the most essential key concepts, it is not possible to discuss all concepts on a deeper level. Please feel free to also have a look on Python books for those purposes.
Although Python is a powerful programming language, we want to keep things as simple as possible at the beginning. Therefore we will learn the fundamentals by using Python as a calculator. You could either use the command line directly, an editor or one of the IDEs, that were introduced in the previous chapters to follow up. We strongly recommend to hack in the following commands and code snippets at your own. It also strengthens your skills to solve the provided exercises. If you decide to use Jupyterlab as your IDE of choice, please feel free to download this notebook or create an new notebook at your own.
Note: Lines that starts with
#
are interpreted as comments by the programmer for himself or other programmers. These lines will not be executed by Python
# this line is a comment. Use the # to mark lines that should not be executed and ignored
The calculation of any type of arithmetic computation in Python is done by the help of the common operators. Python does the calculation and prints the result after you typed your command and execute it over strg
+ enter
(if you use Jupyter Lab):
100 + 3
103
50 - 25
25
13 / 2
6.5
Note: Mathematical operations are interpreted and executed in a specific order. If you want to combine multiple operations at once you should keep the operators precedence in mind to avoid unexpected behaviour or results.
For instance
10 + 3 * 2
16
is not the same as:
(10 + 3) * 2
26
Brackets give you control about which operator is intended to be executed at first.
Exercise: Make use of the basic mathematical operations to calculate the point of intersect with the x-axis of the following linear function in Python: $f(x) = \frac{4}{7}x + 5$
### your solution
(-5) * (7 / 4)
-8.75
If you want to store a result of a calculation (e.g. for later usage), you can assign this result with a unique name over the =
operator. To retrieve this value again, just use the assigned name. For instance:
result = 8 + 5
result
13
Note: This process is referred to the term of variable assigment. It is also possible to store single values as variables (names). It is a good practice to store important and frequently used values in an own variable and use theses variables over your script, also from a coding maintenance perspective. Let us assume you wrote a script with 100 Lines of Code. In this script you make use of one value over and over again (e.g. 20 times). After some time you realise this value was wrong or you need to change it. By use of variable assigment this task takes you just a second because you only have to change the assigment itself. Otherwise you would have to change and identify these 20 position in your manually, also with the risk to forget one place.
You also have the possibility to multiply and exponentiate values easily by the *
and the **
operator:
2 * 5
10
2 ** 5
32
As you can see above the **
operator has the meaning of $2^5$.
A complete Listing of all Python operators is provided here. Most of these operators will also be introduced over this tutorial. So please do not hesitate if you do not understand every operator directly.
Exercise: Use your already learned knowledge to calculate the 4th Root of 4096! (Hint: Have a look at the calculation rules for powering numbers)
### your solution
4096 ** (1 / 4)
8.0
Besides these operators that are reserved for calculations with numbers there are also some operators that work on characters or in general on chains of characters (words). To differentiate between numbers and word you have to use either ""
or ''
. If you want to store a word in a variable you will have to put this word in quotation marks to indicate Python that this is not a number but a word:
a_word = "Hello"
You can also store a whole sentence or a paragraph in a variable:
a_sentence = "Hello, you are reading an introductory chapter to the use of operators in Python. I hope you enjoy your journey!"
There are only a few operators that support characters or words. You can use the +
operator to join two words with one another:
a_word + " world"
'Hello world'
The *
operator is also defined for words. It repeats the word x times:
a_word * 4
'HelloHelloHelloHello'
Besides these basic operators with limited functions on words and texts Python has a wide range of functionalities for manipulating and gather information out of texts. To make use of this power, we will have to learn more about functions and datatypes at first.
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.