Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
talit:python_basics [2020-02-09 21:38] – sca | talit:python_basics [2020-02-09 21:42] (aktuell) – sca | ||
---|---|---|---|
Zeile 57: | Zeile 57: | ||
</ | </ | ||
- | Note that you can also write a number as a string, e.g., `{s = ' | + | Note that you can also write a number as a string, e.g., `s = ' |
However, `s` will be treated as text and not as a number, therefore, you cannot do any calculations with it. | However, `s` will be treated as text and not as a number, therefore, you cannot do any calculations with it. | ||
Also, you can use variables with more than one character, e.g., `mystring = "Hello World" | Also, you can use variables with more than one character, e.g., `mystring = "Hello World" | ||
Zeile 93: | Zeile 93: | ||
1. Can you use numbers or special characters like `?, | 1. Can you use numbers or special characters like `?, | ||
- | <!-- You can even use numbers in variable names, as long as they are not in the beginning, e.g., `mystring13 = "Hello World" | ||
- | But you can NOT use special characters in variable names | ||
- | --> | ||
2. How can you add a line break to a string? | 2. How can you add a line break to a string? | ||
- | <!-- % mystring = " | ||
3. Is Python case-sensitive? | 3. Is Python case-sensitive? | ||
4. Given a (long enough) string `s`, what do the commands | 4. Given a (long enough) string `s`, what do the commands | ||
Zeile 106: | Zeile 102: | ||
## Integers & floats | ## Integers & floats | ||
- | \label{sec integers and floats} | ||
Next, let's have a look at numbers, in particular integers and floats. | Next, let's have a look at numbers, in particular integers and floats. | ||
Zeile 295: | Zeile 290: | ||
- | # Conditions | + | # Conditions & Booleans |
We can check if one or multiple conditions are satisfied. | We can check if one or multiple conditions are satisfied. | ||
Zeile 346: | Zeile 341: | ||
# If statements | # If statements | ||
- | In an `if' | + | In an `if` statement, the action depends on conditions. |
Let's say we are given some number $a$ and want to determine its absolute value. It is given by $a$ if $a \geq 0$ and by $-a$ if $a < 0$. | Let's say we are given some number $a$ and want to determine its absolute value. It is given by $a$ if $a \geq 0$ and by $-a$ if $a < 0$. | ||
We can realize this by | We can realize this by | ||
Zeile 359: | Zeile 354: | ||
In the first line, we just choose some number. | In the first line, we just choose some number. | ||
Feel free to play around with its value. | Feel free to play around with its value. | ||
- | In line 3, the `if' | + | In line 3, the `if` statement begins. |
It starts with `if` and is followed by a condition, here `a < 0`, and a mandatory colon. | It starts with `if` and is followed by a condition, here `a < 0`, and a mandatory colon. | ||
If the condition is `True` (satisfied), | If the condition is `True` (satisfied), | ||
Zeile 414: | Zeile 409: | ||
Second, we discuss the **while-loop**. It repeats a piece of code as long as a condition is satisfied. | Second, we discuss the **while-loop**. It repeats a piece of code as long as a condition is satisfied. | ||
- | Careful: If you have a bug in your code and the condition is always true, you have an `infinite loop': the code will never stop running and you will have to kill the process manually. | + | Careful: If you have a bug in your code and the condition is always true, you have an **infinite loop**: the code will never stop running and you will have to kill the process manually. |
A simple example of a while-loop is: | A simple example of a while-loop is: | ||
Zeile 441: | Zeile 436: | ||
There are many additional packages that can be imported. | There are many additional packages that can be imported. | ||
- | For doing math, `numpy' | + | For doing math, `numpy` is particular useful. |
For example, it contains a lot of predefined functions like trigonometric functions. | For example, it contains a lot of predefined functions like trigonometric functions. | ||
Let us do an example | Let us do an example | ||
Zeile 450: | Zeile 445: | ||
print(np.pi) | print(np.pi) | ||
</ | </ | ||
- | In the first line, we import the `numpy' | + | In the first line, we import the `numpy` package and give it the short name `np`. Of course, you could use a different name than `np` but this is the common choice. |
- | Each time we call a function (or something else) from numpy, we have to start with `np.', in order for Python to know that it has to look inside the numpy package. This is exactly what we do on line 3: We call the sine function defined in numpy and evaluate it for the argument $2$. In the last line, we print the value of the constant $\pi$. | + | Each time we call a function (or something else) from numpy, we have to start with `np.`, in order for Python to know that it has to look inside the numpy package. This is exactly what we do on line 3: We call the sine function defined in numpy and evaluate it for the argument $2$. In the last line, we print the value of the constant $\pi$. |
**Questions: | **Questions: | ||
Zeile 463: | Zeile 458: | ||
# Elapsed time | # Elapsed time | ||
- | \label{sec elapsed time} | ||
If you want to test the efficiency of your code, it is useful to measure the time needed for the calculation: | If you want to test the efficiency of your code, it is useful to measure the time needed for the calculation: |