How to Jupyter¶
Jupyter Notebook is an open-source web application that allows you to create and share documents containing live code, equations, visualizations, and narrative text.
-It's widely used for interactive computing, data analysis, scientific research, education, and data visualization.
-Some key features and components of Jupyter Notebook includes:
--
At the bottom left is a control pad with which you can navigate through the slides.
-(How to create slides will be explained later.)
+3. Vorlesung¶
-
-
- Interactive Environment -
- Support for Multiple Programming Languages -
- Rich Text Support -
- Data Visualization -
- Equation Rendering -
- Easy Sharing -
- Notebook Extensions -
- Data Analysis and Exploration -
- Education and Learning -
Einfache Zählschleife¶
Install Python¶
In this module we will learn the programming language Python. To do this, we need to install it on our system in order to be able to use Jupyter Notebook in advance.
-The Python.org website contains a download link for each operating system. Under www.python.org/downloads/ you can download the latest Python version.
-After following the installation wizard (this depends heavily on which operating system you are using, so we do not show this here), you have successfully installed Python.
+# Als While Loop
+count = 1 # Zählvariable
+while count < 4: # Bedingung
+ print(count)
+ count += 1 # Hochzählen
+
Opening a Terminal¶
Windows:
--
-
Press Start
(⊞ - Windows Symbol) -> type and search forcmd
-- Or Press the Windows Symbol ⊞ + the R key
(Windows + R)
-> typecmd
in the window that appeard -> pressEnter
-
Mac:
-Open launchpad and Search for Terminal
Linux:
-It depends on your Environment. But Ctrl + Alt + T
should do the Trick on every System.
Upgrading Pip and Installing Jupyter¶
-
Pip - Python Package Index¶
pip is the de-facto and recommended package management programme for Python packages from the Python Package Index (PyPI). At the beginning, the project was called "pyinstall".
-It's website is found under pypi.org. Every Package you need, can and should be derived from PyPI.
--
After opening the terminal, pip should first be updated to the latest version. To do this, enter the command:
-python3 -m pip install -U pip
Requirement already satisfied: pip in /home/phil/Desktop/einfuhrung-in-die-programmierung/env/lib/python3.11/site-packages (23.2.1) +1 +2 +3
Installing Jupyter¶
Now we can install some software we only need two packages depending on your need.
-virtualenv
is a tool to create isolated Python environments. You can read more about it in the Virtualenv documentation.
# Als For Loop
+for count in [1, 2, 3]:
+ print(count)
+
1 +2 +3 ++
-
-
- We need to install the package
virtualenv
(venv)
-
python3 -m pip install virtualenv
Beispiel einer Zählschleife in C:
+for (int i = 0; i < 4, i++) {}
+
-
-
- Now we can create an virtual environment in any folder we want. (A good practice is a venv for every project) -
python3 -m venv env
the name env
is the folder which has all of our environment information, it can be named everything, but for convinence env
should be used.
# Zählschleife mittels range Funktion
+for count in range(1,4):
+ print(count)
+
1 +2 +3 ++
-
-
- After installing
virtualenv
we need to activate it
-
Windows: .\env\Scripts\activate
Linux / Mac: source env/bin/activate
your command prompt will be modified to reflect the change.
+range
kann bis zu 3 Parameter nehmen.
-
+
- 1 Parameter
range(4)
-> Zählt in 1er Schritten bis exklusive der eingegebenen Zahl 0,1,2,3
+
Der folgend genutzte Stern *
sagt Python er soll den iterator
entpacken.
-
-
- Now you can install jupyter and other dependencies without tinkering with your system -
pip install jupyterlab
-> We can use pip direct because we specified the python version with venv implicitly.
+print(*range(4))
+
0 1 2 3 ++
Starting Jupyter for the first time¶
the last thing we need to tab out of the command line is to start Jupyter
-therefor type jupyter lab
and Enter. Do not close the command line it will stop jupyter!
After a little waiting time jupyter prompts you with messages one of them should look like:
-http(s)://<server:port>/<lab-location>/lab
copy the url and open it in your Webbrowser of Choice. Now we can Proceed.
-Note: This step needs to be done everytime you want to start Jupyter!
-+
-
+
- 2 Parameter
range(1,4)
-> Zählt in 1er Schritten von dem ersten Parameter bis exklusiv zum zweiten Parameter 1,2,3
+
Alternatives¶
+print(*range(1,4))
+
1 2 3 ++
(Mini)conda¶
conda from Ananconda Inc. is an open source package and environment manager for many languages that encludes everything we needs.
-For most purposes miniconda should do the trick. You can download it here conda docs
-After installing you can change every python3 -m pip
and pip
command with conda
.
-
+
- 3 Parameter
range(1,11,2)
-> Zählt in2
er Schritten von dem ersten Parameter bis exklusiv zum zweiten Parameter
+
Jupyter Lab Desktop¶
As there description on GitHub stated
-JupyterLab Desktop is the cross-platform desktop application for JupyterLab. It is the quickest and easiest way to get started with Jupyter notebooks on your personal computer, with the flexibility for advanced use cases.
-its nothing else than a selfcontained webbrowser bundeld with Jupyter Lab. You can download a binary for your operating System under there GitHub Releases page.
+print(*range(1,11,2))
+
1 3 5 7 9 ++
Note¶
Every Process has its Advantages and Disadvantages and depends on your needs and workflow.
-The easiest way isn't always the best.
-If you have no command line experience try to get used to it and don't use Jupyter Lab Desktop.
+For-Loops
itertieren über Iteratoren. Listen sind z.b. Iteratoren.
l = [0, 1, 2, 3, 4]
+l
+
[0, 1, 2, 3, 4]+
for el in l:
+ print(el)
+
0 +1 +2 +3 +4 ++
len(l) # Anzahl 'Länge' der Liste l
+
5+
# range zählt bis 'exklusive' seines Eingabeparameters um folgendes verhalten zu emulieren
+for i in range(len(l)):
+ print(i)
+
0 +1 +2 +3 +4 ++
# Iteration über die Indexe der Liste
+for i in range(len(l)):
+ print(l[i]) # Zugriff über Index auf die Elemente der Liste
+
0 +1 +2 +3 +4 ++
# _ wird verwendet für Loops die einfach etwas immer und immer wiederholen sollen
+for _ in range(6):
+ print("Hello")
+
Hello +Hello +Hello +Hello +Hello +Hello ++
Folgende Dict beispiele Eklären sich dementsprechend selber
+d = {"a": 5, "b": 8, "c": 10}
+d
+
{'a': 5, 'b': 8, 'c': 10}+
d.values()
+
dict_values([5, 8, 10])+
for el in d.values():
+ print(f"Wert: {el}")
+
Wert: 5 +Wert: 8 +Wert: 10 ++
for key in d.keys():
+ print(f"Key: {key}")
+
Key: a +Key: b +Key: c ++
# Items gibt eine Liste mit tupeln zurück, jedes tuple wird in seine Elemente zerlegt und den Variablen k & v zugewiesen
+for k, v in d.items():
+ print(f"Key: {k} mit Wert: {v}")
+
Key: a mit Wert: 5 +Key: b mit Wert: 8 +Key: c mit Wert: 10 ++
# Liste füllen
+squared = []
+for i in range(6):
+ squared.append(i*i)
+squared
+
[0, 1, 4, 9, 16, 25]+
# List Comprehension
+sq = [n**2 for n in range(6)]
+sq
+
[0, 1, 4, 9, 16, 25]+
# Dict füllen
+di = {}
+for n in range(6):
+ di[n] = n**2
+di
+
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}+
# Dictionary Comprehension
+dic = {n: n**2 for n in range(6)}
+dic
+
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}+
System Interaction¶
+input()
+
'4'+
text = input()
+
text
+
'6'+
input("Gebe bitte eine Zahl ein:")
+
'7'+
File Handling¶
+f = open('test.txt') # Öffne File und gebe den Handler an f, Standard im Lesemodus
+f
+
<_io.TextIOWrapper name='test.txt' mode='r' encoding='UTF-8'>+
f.readlines() # Lese den Inhalt aus f
+
['Super Secret Message\n', 'Hallo Welt\n', 'Geiler Kurs\n', 'Freitag 15h yeah']+
data = open('data.txt', 'w') # Öffne eine beschreibare File
+data
+
<_io.TextIOWrapper name='data.txt' mode='w' encoding='UTF-8'>+
data.write("Ich will nachhause") # Schreibe in die File
+
18+
# Schliese die Files
+f.close()
+data.close()
+
# Standard File handling
+f = open('test.txt')
+print(f.readlines())
+f.close()
+
['Super Secret Message\n', 'Hallo Welt\n', 'Geiler Kurs\n', 'Freitag 15h yeah'] ++
f.readlines() # File ist geschlossen also ist lesen nicht möglich
+
+--------------------------------------------------------------------------- +ValueError Traceback (most recent call last) +Cell In[33], line 1 +----> 1 f.readlines() # File ist geschlossen also ist lesen nicht möglich + +ValueError: I/O operation on closed file.+
# Contexte nehmen einem die Arbeit ab
+with open('test.txt', 'r') as f:
+ print(f.readlines())
+
+# File ist bereits geschlossen
+f.readlines() # Wirft Fehler
+
['Super Secret Message\n', 'Hallo Welt\n', 'Geiler Kurs\n', 'Freitag 15h yeah'] ++
+--------------------------------------------------------------------------- +ValueError Traceback (most recent call last) +Cell In[35], line 6 + 3 print(f.readlines()) + 5 # File ist bereits geschlossen +----> 6 f.readlines() # Wirft Fehler + +ValueError: I/O operation on closed file.+
Importing¶
+import math
+
math
+
<module 'math' from '/usr/local/Cellar/python@3.12/3.12.5/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload/math.cpython-312-darwin.so'>+
math.pi
+
3.141592653589793+
math.sqrt(10)
+
3.1622776601683795+
math.sqrt(4)
+
2.0+
from math import sqrt
+
sqrt
+
<function math.sqrt(x, /)>+
sqrt(90)
+
9.486832980505138+
from math import * # Böse nicht mache führt nur zu unerklärbaren Fehlern
+
import numpy as np
+
np.sqrt(9000)
+
np.float64(94.86832980505137)+
np
+
<module 'numpy' from '/home/phil/Desktop/programmieren_wise_24_25/Material/env/lib64/python3.12/site-packages/numpy/__init__.py'>