Skip to content
Snippets Groups Projects
Commit e0ab91a8 authored by Ugo Paavo Finnendahl's avatar Ugo Paavo Finnendahl
Browse files

Merge branch 'gabo-main-patch-22713' into 'main'

Replace tutorial00_python_ex2.ipynb

See merge request mkohlbrenner/wr-notebooks!1
parents b2b7bc9c 70b2ca89
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Einführung in Python und NumPy
%% Cell type:markdown id: tags:
## Variablen und Datentypen in Python
%% Cell type:markdown id: tags:
### Primitive Datentypen
%% Cell type:code id: tags:
``` python
a = 5
b = "Hello World"
c = 3.14
d = True
e = 4 + 3j
print(a, b, c, d, e)
print(type(a), type(b), type(c), type(d), type(e))
```
%% Cell type:markdown id: tags:
### Variablen überschreiben
%% Cell type:code id: tags:
``` python
var = 50
print(var)
var = "Hello World" # Neuer Datentyp
print(var)
```
%% Cell type:markdown id: tags:
### Mehrere Variablen initialisieren
%% Cell type:code id: tags:
``` python
x, y, z = 5, 6, 7
i = j = k = "ha"
print(x, y, z)
print(i, j, k)
```
%% Cell type:markdown id: tags:
### Variablen tauschen
%% Cell type:code id: tags:
``` python
a, b = 1, 2
a, b = b, a
print(b)
```
%% Cell type:markdown id: tags:
### Operatoren
%% Cell type:code id: tags:
``` python
a, b = 2, 4
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a**b)
print(b**(1/a))
```
%% Cell type:markdown id: tags:
### Variablen mit Operatoren neu zuweisen
%% Cell type:code id: tags:
``` python
i = j = k = 7
i += 5;
j *= 5;
k **= 5;
i += 5
j *= 5
k **= 5
print(i, j, k)
# Kein i++
```
%% Cell type:markdown id: tags:
### Boolesche Operatoren
%% Cell type:code id: tags:
``` python
print(1 + 1 == 2)
print(1 + 2 != 2)
print(1 + 2 > 3)
print(1 + 2 <= 3)
print(1 + 2 is 3)
print(True or False)
print(True and False)
print(not True)
print(True or False) # ||
print(True and False) # &&
print(not True) # !
```
%% Cell type:markdown id: tags:
### Listen
%% Cell type:code id: tags:
``` python
l = ["python", "numpy", "scipy", 10, True]
print(l[0], l[1], l[2], l[3], l[4])
print(l[-1], l[-2], l[-3], l[-4], l[-5])
print(len(l))
```
%% Cell type:markdown id: tags:
### Listen bearbeiten
%% Cell type:code id: tags:
``` python
l.append(False)
print(l)
l[3] = "matplotlib"
print(l)
```
%% Cell type:markdown id: tags:
### Operatoren auf Listen
%% Cell type:code id: tags:
``` python
l1, l2 = [1,2,3,4], [5,6,7,8]
l = l1 + l2
print(l)
print(3 * l1)
print(5 in l2)
print(5 in l1)
```
%% Cell type:markdown id: tags:
### Mehrdimensionale Listen
%% Cell type:code id: tags:
``` python
A = [[1,2,3], [4,5,6], [7,8,9]]
print(A)
print(A[0][0], A[1][0], A[1][2])
print(A[0], A[1], A[2])
```
%% Cell type:markdown id: tags:
### List Slices
Notation: liste[start: stop: step]
%% Cell type:code id: tags:
``` python
print(l)
print(l[2:5])
print(l[2:])
print(l[:5])
print(l[2:5]) # der Index von stop ist nicht mit dabei
print(l[2:]) # stop weglassen => bis zum Ende
print(l[:5]) # start weglassen => vom Anfang an
print(l[:])
print(l[2:5:2])
print(l[2:5:2]) # Das erste Element (start) ist immer mit dabei
print(l[5:2:-1])
print(l[::-1])
```
%% Cell type:markdown id: tags:
### Tupel
%% Cell type:code id: tags:
``` python
t = ("Audi", 250, 4.7, True)
print(t[0], t[1], t[2], t[3])
print(t[-1], t[-2], t[-3], t[-4])
print(len(t))
print("Audi" in t)
```
%% Cell type:markdown id: tags:
Tupel können nicht verändert werden, nur zusammengefügt!
%% Cell type:code id: tags:
``` python
t += ("white",)
t += ("white",) # es wird ein neues Tupel erstellt
print(t)
a, b, c, d, e = t
print(c, e)
```
%% Cell type:markdown id: tags:
### Dictionaries
%% Cell type:code id: tags:
``` python
c = {
"brand": "Audi",
"ps": 250,
"acc": 4.7
}
c["color"] = "white"
print(c["brand"])
print(c.keys())
print(c.values())
print(c.items())
```
%% Cell type:markdown id: tags:
## Funktionen und Verzweigungen
%% Cell type:code id: tags:
``` python
def greet():
print("Hello world")
greet()
```
%% Cell type:code id: tags:
``` python
def square(x):
def square(x: int) -> int:
return x**2
print(square(4))
```
%% Cell type:markdown id: tags:
### Optionale Parameter und Parameter mit Namen
%% Cell type:code id: tags:
``` python
def pow(x, y = 0):
return x**y
print(pow(10))
print(pow(10, 2))
print(pow(y=2, x=5))
```
%% Cell type:markdown id: tags:
### Mehrere Rückgabewerte (Tupel)
%% Cell type:code id: tags:
``` python
def f(x, y):
return x**2, y**2
a, b = f(3, 4)
print(a, b)
ab = f(3, 4)
print(ab)
```
%% Cell type:markdown id: tags:
### Bedingte Anweisungen
%% Cell type:code id: tags:
``` python
def sign(x):
if x < 0:
return -1
elif x > 0:
return 1
else:
return 0
print(sign(17), sign(-3), sign(0))
```
%% Cell type:code id: tags:
``` python
def positive(x):
return True if x > 0 else False
print(positive(14), positive(0))
# return x > 0 ? True : False
# C/Java return x > 0 ? True : False
```
%% Cell type:markdown id: tags:
### Schleifen
%% Cell type:code id: tags:
``` python
for i in range(5):
for i in range(5): # Python For-Schleifen iterieren immer über ein iterable z.B. Liste
print(i)
print(list(range(1, 6)))
print(list(range(1, 10, 2)))
```
%% Cell type:markdown id: tags:
Funktionsweise: range(start, stop, step)
%% Cell type:markdown id: tags:
### List Comprehensions
%% Cell type:code id: tags:
``` python
a = [i**2 for i in range(5)]
print(a)
b = [i**2 for i in range(10) if i % 2 == 0]
print(b)
```
%% Cell type:markdown id: tags:
### Über Datenstrukturen iterieren
%% Cell type:code id: tags:
``` python
print(l2)
```
%% Cell type:code id: tags:
``` python
for el in l2:
print(el)
```
%% Cell type:code id: tags:
``` python
for i in range(len(l2)):
print(i, l2[i])
```
%% Cell type:code id: tags:
``` python
for i, el in enumerate(l2):
print(i, el)
```
%% Cell type:code id: tags:
``` python
print(c)
```
%% Cell type:code id: tags:
``` python
for key, val in c.items():
print(key, val)
```
%% Cell type:markdown id: tags:
## Objektorientierte Programmierung
%% Cell type:code id: tags:
``` python
class Person:
def __init__(self, name):
self.name = name
def greet(self):
print("Hallo, ich bin {}".format(self.name))
def __repr__(self):
return "Person: {}".format(self.name)
```
%% Cell type:code id: tags:
``` python
p = Person("Anna")
p.greet()
print(p)
```
%% Cell type:code id: tags:
``` python
class Student(Person):
def __init__(self, name, age, height):
Person.__init__(self, name)
self.__age = age # private
self._height = height # protected
def __repr__(self):
return "Person[name={},age={},height={}]"\
.format(self.name, self.__age, self._height)
```
%% Cell type:code id: tags:
``` python
s = Student("Anna", 18, 175)
s.greet()
print(s)
# print(s.__age)
```
%% Cell type:markdown id: tags:
## Funktionale Programmierung
%% Cell type:code id: tags:
``` python
g = lambda x: x**4
h = lambda x, y: x**(2*y)
print(g(3))
print(h(2,2))
```
%% Cell type:code id: tags:
``` python
print(l)
```
%% Cell type:code id: tags:
``` python
lsq = map(lambda x: x**2, l)
print(lsq)
print(list(lsq))
```
%% Cell type:code id: tags:
``` python
print(list(filter(lambda x: x % 2 == 1, l)))
```
%% Cell type:code id: tags:
``` python
from functools import reduce
print(reduce(lambda a, b: a + b, l))
```
%% Cell type:code id: tags:
``` python
print(reduce(lambda a, b: a if a > b else b, l))
```
%% Cell type:markdown id: tags:
## == vs. is
%% Cell type:code id: tags:
``` python
print(5 == 5)
print(5 is 5)
```
%% Cell type:code id: tags:
``` python
a = b = [1,2,3]
print([1,2,3] == [1,2,3])
print([1,2,3] is [1,2,3])
print(a is b)
```
%% Cell type:markdown id: tags:
## NumPy Grundlagen
%% Cell type:code id: tags:
``` python
import numpy as np
```
%% Cell type:markdown id: tags:
### NumPy Arrays
%% Cell type:code id: tags:
``` python
a = np.array([1, 2, 3, 4, 5, 6])
print(a)
print(a[0])
print(a[-1])
print(a[1:4])
print(a[:2])
print(a[::-1])
print(a[1::2])
print(a[[0, 2, 3, 4]])
print(a[[0, 2, 3, 4]]) # Liste von Indizes
print(a[[True, False, True, False, False, True]])
```
%% Cell type:markdown id: tags:
### Operatoren auf NumPy Arrays
%% Cell type:code id: tags:
``` python
b = np.array([6, 5, 4, 3, 2, 1]) # b = a[::-1]
print(a + 2)
print(3 * a)
print(a**2)
print(2**a)
```
%% Cell type:code id: tags:
``` python
print(a + 2)
print(3 * a)
print(a**2)
print(2**a)
b = np.array([6, 5, 4, 3, 2, 1]) # b = a[::-1]
```
%% Cell type:code id: tags:
``` python
print(a + b)
print(a * b)
print(a**b)
```
%% Cell type:markdown id: tags:
### Zweidimensionale Arrays (Matrizen)
%% Cell type:code id: tags:
``` python
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(A)
print(A[0,0], A[1, 2])
```
%% Cell type:code id: tags:
``` python
print(A[0])
print(A[:, 0])
```
%% Cell type:code id: tags:
``` python
print(A[:2, :2])
```
%% Cell type:code id: tags:
``` python
print(A[:2, 1::-1])
```
%% Cell type:code id: tags:
``` python
print(A[1::-1, :2])
```
%% Cell type:markdown id: tags:
### Werte überschreiben
%% Cell type:code id: tags:
``` python
B = A.copy()
print(B)
```
%% Cell type:code id: tags:
``` python
B[0] = 12
print(B)
```
%% Cell type:code id: tags:
``` python
B[:, 1] = -5
print(B)
```
%% Cell type:code id: tags:
``` python
B[2] = [5, 6, 7]
print(B)
```
%% Cell type:code id: tags:
``` python
B[:, 1] = B[0]
print(B)
```
%% Cell type:markdown id: tags:
### Dimensionen hinzufügen
%% Cell type:code id: tags:
``` python
print(A[None])
```
%% Cell type:code id: tags:
``` python
print(A[:, None])
```
%% Cell type:code id: tags:
``` python
print(A[:, :, None])
```
%% Cell type:code id: tags:
``` python
X = np.array([[1,0],[0,1]])
Y = np.array([[1,1],[2,1],[3,4]])
print(X[:, None] - Y[None, :])
print(X[None, :] - Y[:, None])
```
%% Cell type:markdown id: tags:
### Boolesche Funktionen auf NumPy Arrays
%% Cell type:code id: tags:
``` python
b = np.array([1, 0, 1])
```
%% Cell type:code id: tags:
``` python
print(b == 1)
```
%% Cell type:code id: tags:
``` python
print(b < 1)
```
%% Cell type:code id: tags:
``` python
print(A[b == 1])
```
%% Cell type:code id: tags:
``` python
print(A[[True, False, True]])
```
%% Cell type:code id: tags:
``` python
print(A > 5)
```
%% Cell type:code id: tags:
``` python
print(A[A > 5])
```
%% Cell type:markdown id: tags:
### Nützliche NumPy Funktionen
%% Cell type:code id: tags:
``` python
X = np.ndarray((4,4))
A = np.zeros((3, 4))
B = np.ones((3,4))
c = np.arange(0, 1, .1)
d = np.linspace(0, 1, 10)
I = np.identity(4)
```
%% Cell type:code id: tags:
``` python
print(X) # zufällige Werte
```
%% Cell type:code id: tags:
``` python
print(A)
```
%% Cell type:code id: tags:
``` python
print(B)
```
%% Cell type:code id: tags:
``` python
print(c)
```
%% Cell type:code id: tags:
``` python
print(d)
```
%% Cell type:code id: tags:
``` python
print(I)
```
%% Cell type:code id: tags:
``` python
M = np.linspace(1, 16, 16).reshape((4,4))
print(M)
```
%% Cell type:code id: tags:
``` python
print(M.T)
```
%% Cell type:code id: tags:
``` python
print(M.sum(), M.mean(), M.prod())
```
%% Cell type:code id: tags:
``` python
print(M.sum(0), M.mean(0), M.prod(0))
```
%% Cell type:code id: tags:
``` python
print(M.sum(1), M.mean(1), M.prod(1))
```
%% Cell type:code id: tags:
``` python
x = np.linspace(1,4, 4)
print(x)
```
%% Cell type:code id: tags:
``` python
print(M.dot(x))
```
%% Cell type:code id: tags:
``` python
print(M @ x)
```
%% Cell type:code id: tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment