Python Einführung
Zahlen (int & float)
7
0.001
1 + 1
2 * 3
7 / 2
4 / 2
7 // 2
7 % 2
2**3
11**137
Text (str)
"Hello Python"
len("Hello")
"hello".replace('e', 'a').capitalize()
"1,2;3,4;5,6".replace(',', '.')
Text & Zahlen
2 * 3
'2' * 3
'2*3'
int('2') * 3
float('3.5')
'2' + '3'
n = 9
f'file_{n}.txt'
ext = 'dat'
f'file_{n}.{ext}'
'a;b;c'.split(';')
Wertlisten (list)
['a', 'b', 'c']
len(['a', 'b', 'c'])
['a', 'b', 'c'][0]
['a', 'b', 'c'][:2]
['a', 'b', 'c'][2:]
['a', 'b', 'c'][::-1]
'abc'[1]
[1, 2, 3] + ['a', 'b', 'c']
';'.join(['a', 'b', 'c'])
range(5)
Boolesche Variable (True & False)
3 == 2
x = 5
y = 4
x > y
x >= x
x != y
12 > 7
'12' > '7'
'art' in 'Stuttgart'
6 in [5, 7, 2, 8, 4, 10, 1, 3, 9]
Mehr Wertlisten
values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for value in values:
print(value)
[2**i for i in values]
[i for i in values if i > 5]
[i for i in values if i % 2 == 0]
Dateien
with open('SchPark01/SchPark_GT_2011_01_01.csv') as csv_file:
for line in csv_file:
print(line)
with open('new_file.txt', 'w') as new_file:
new_file.write("Hello\n")
Verzeichnisse
from pathlib import Path
for csv_path in Path('SchPark01').glob('*.csv'):
print(csv_path)
for csv_path in Path('SchPark03').glob('*/*/*.csv'):
print(csv_path)
Function
def f(a, b):
return a + b
f(2, 3)
Sortieren
sorted([3,1,2])
numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
sorted(numbers)
sorted(numbers, key=int)
INSEL
Some examples are included in inselpy_examples
# pip install insel
import insel
insel.block('pi')
insel.block('sum', 2, 3)
insel.block('do', parameters=[1, 10, 1])
insel.template('a_times_b', a=7, b=3)