commit message from python script
This commit is contained in:
BIN
Lehrer/pythonkurs/Donnerstag/.decorators.py.swp
Executable file
BIN
Lehrer/pythonkurs/Donnerstag/.decorators.py.swp
Executable file
Binary file not shown.
31
Lehrer/pythonkurs/Donnerstag/argv.py
Executable file
31
Lehrer/pythonkurs/Donnerstag/argv.py
Executable file
@@ -0,0 +1,31 @@
|
||||
"""
|
||||
Name des Moduls (Kopfzeile)
|
||||
|
||||
Ausfuehrliche Beschreibung des Moduls
|
||||
"""
|
||||
import sys
|
||||
|
||||
def argv_value(param: str, default: str = None) -> str:
|
||||
"""
|
||||
Ermittelt den Wert eines Parameters auf der Kommandozeile
|
||||
|
||||
'param' entspricht dem Parameter, nach dem gesucht wird,
|
||||
mit fuehrendem dash. etc. foo bar bla fasel :p
|
||||
|
||||
'default' plus Beschreibung dazu, bin zu faul
|
||||
|
||||
Beispiel:
|
||||
value = argv_value('-o', 'pprint')
|
||||
"""
|
||||
for idx, val in enumerate(sys.argv):
|
||||
if val == param:
|
||||
if idx+1 < len(sys.argv) and not sys.argv[idx+1].startswith('-'):
|
||||
return sys.argv[idx+1]
|
||||
|
||||
return default
|
||||
|
||||
|
||||
# python3 -m argv # Aufruf des Moduls als Hauptprogramm
|
||||
if __name__ == '__main__':
|
||||
print("Wert von -o :", argv_value('-o'))
|
||||
print("Kommandozeile :", sys.argv[1:])
|
||||
10
Lehrer/pythonkurs/Donnerstag/calc1
Executable file
10
Lehrer/pythonkurs/Donnerstag/calc1
Executable file
@@ -0,0 +1,10 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
from vectors import Vector
|
||||
|
||||
v1 = Vector()
|
||||
v2 = Vector()
|
||||
print(v1)
|
||||
print(v2)
|
||||
print("0x{:12x}".format(id(v1)))
|
||||
print("0x{:12x}".format(id(v2)))
|
||||
10
Lehrer/pythonkurs/Donnerstag/calc2
Executable file
10
Lehrer/pythonkurs/Donnerstag/calc2
Executable file
@@ -0,0 +1,10 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
from vectors import Vector
|
||||
|
||||
v1 = Vector()
|
||||
print(v1)
|
||||
|
||||
v1.test()
|
||||
v1.test(1, 2, 3)
|
||||
print(v1.test)
|
||||
16
Lehrer/pythonkurs/Donnerstag/calc3
Executable file
16
Lehrer/pythonkurs/Donnerstag/calc3
Executable file
@@ -0,0 +1,16 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
from vectors import Vector
|
||||
|
||||
v1 = Vector(1, 2, 3)
|
||||
v2 = Vector(4, 0, 1)
|
||||
print(v1)
|
||||
# Achtung: Zugriff von aussen auf Objekt-Interna sollten vermieden werden
|
||||
# print(v1.local_values)
|
||||
# Besser: Nur ueber passende Getter zugreifen
|
||||
print(v1.get_values())
|
||||
|
||||
#v2 = "Kill"
|
||||
#v2 = None
|
||||
del v2 # Loescht gleichzeitig Name v2 aus aktuellem Scope
|
||||
print("Jetzt wurde gerade v2 freigegeben")
|
||||
15
Lehrer/pythonkurs/Donnerstag/calc4
Executable file
15
Lehrer/pythonkurs/Donnerstag/calc4
Executable file
@@ -0,0 +1,15 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
from vectors import Vector
|
||||
|
||||
v1 = Vector(1, 2, 3)
|
||||
v2 = Vector(4, 0, 1)
|
||||
print(v1)
|
||||
print(len(v1))
|
||||
|
||||
# v3 = v1.skalarmultiplikation(3)
|
||||
v3 = v1 * 3 # wird daraus v1.__mul__(3)
|
||||
print("v3 =", v3, type(v3))
|
||||
|
||||
v4 = v1 + v2 # wird daraus v1.__add__(v2)
|
||||
print("v4 =", v4, type(v4))
|
||||
14
Lehrer/pythonkurs/Donnerstag/calc5
Executable file
14
Lehrer/pythonkurs/Donnerstag/calc5
Executable file
@@ -0,0 +1,14 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
from vectors import Vector2
|
||||
|
||||
v1 = Vector2(1, 3)
|
||||
v2 = Vector2(2, 0)
|
||||
|
||||
v3 = v1 + v2
|
||||
print("v3:", v3, type(v3))
|
||||
|
||||
v4 = v2 * 4
|
||||
print("v4:", v4, type(v4))
|
||||
|
||||
print("Klasse von v4:", v4.info())
|
||||
21
Lehrer/pythonkurs/Donnerstag/calc6
Executable file
21
Lehrer/pythonkurs/Donnerstag/calc6
Executable file
@@ -0,0 +1,21 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
from vectors import Vector2, Vector9
|
||||
|
||||
v1 = Vector2(1, 3)
|
||||
v2 = Vector2(2, 0)
|
||||
|
||||
print(v1.__str__())
|
||||
print(Vector2.__str__(v1))
|
||||
|
||||
print(v1.static())
|
||||
print(Vector2.static())
|
||||
|
||||
print(v1.static)
|
||||
print(Vector2.static)
|
||||
|
||||
print(v1.__str__)
|
||||
print(Vector2.__str__)
|
||||
|
||||
# v9 = Vector9(1,2,3)
|
||||
# v1 + "Quatsch"
|
||||
32
Lehrer/pythonkurs/Donnerstag/curry
Executable file
32
Lehrer/pythonkurs/Donnerstag/curry
Executable file
@@ -0,0 +1,32 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
def inc_generator(n):
|
||||
"""
|
||||
Increment-Generator mit Currying (Closure)
|
||||
"""
|
||||
return lambda i: i + n
|
||||
|
||||
|
||||
def inc_generator_full(n):
|
||||
"""
|
||||
Increment-Generator mit Currying (Closure)
|
||||
"""
|
||||
def inner_function(i):
|
||||
return i + n
|
||||
|
||||
return inner_function
|
||||
|
||||
|
||||
inc3 = inc_generator(3)
|
||||
inc9 = inc_generator(9)
|
||||
#print(inc3)
|
||||
print(inc3(10))
|
||||
print(inc9(10))
|
||||
print('-' * 30)
|
||||
inc3 = inc_generator_full(3)
|
||||
inc9 = inc_generator_full(9)
|
||||
#print(inc3)
|
||||
print(inc3(10))
|
||||
print(inc9(10))
|
||||
print(inc_generator(10)(10))
|
||||
print(inc_generator_full(10)(10))
|
||||
30
Lehrer/pythonkurs/Donnerstag/decorators.py
Executable file
30
Lehrer/pythonkurs/Donnerstag/decorators.py
Executable file
@@ -0,0 +1,30 @@
|
||||
internal_counter_dict = {}
|
||||
|
||||
def counter(func):
|
||||
|
||||
def counting(*args, **kwargs):
|
||||
counting.callcount += 1
|
||||
return func(*args, **kwargs)
|
||||
|
||||
# counting ist "nur" ein Objekt, kann daher auch Attribute halten
|
||||
counting.callcount = 0
|
||||
|
||||
# Alternativ: Funktionsreferenz als Dictionary-Key verwenden
|
||||
internal_counter_dict[counting] = 0
|
||||
return counting
|
||||
|
||||
|
||||
def get_counter(func):
|
||||
try:
|
||||
return func.callcount
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
def reset_counter(func):
|
||||
try:
|
||||
# Folgende Zeile verhindert das Setzen bei nicht dekorierten Funcs
|
||||
func.callcount
|
||||
func.callcount = 0
|
||||
except:
|
||||
pass
|
||||
24
Lehrer/pythonkurs/Donnerstag/fakultaet
Executable file
24
Lehrer/pythonkurs/Donnerstag/fakultaet
Executable file
@@ -0,0 +1,24 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
from decorators import counter, get_counter
|
||||
|
||||
# Dekorator sorgt dafuer, dass Python nach Compiler in der Ausfuehrung
|
||||
# die Funktion fak veraendert:
|
||||
#
|
||||
# fak = counter(fak)
|
||||
# fak = counter(<urspruengliche Funktionsreferenz>)
|
||||
@counter
|
||||
def fak(n):
|
||||
if type(n) is not int or n < 0:
|
||||
raise TypeError("Illegal type")
|
||||
|
||||
if n == 0:
|
||||
return 1
|
||||
|
||||
return n * fak(n-1)
|
||||
|
||||
|
||||
# reset_counter(fak)
|
||||
print("6! =", fak(6))
|
||||
print("Anzahl Aufrufe:", get_counter(fak))
|
||||
# reset_counter(print)
|
||||
15
Lehrer/pythonkurs/Donnerstag/fibonacci1
Executable file
15
Lehrer/pythonkurs/Donnerstag/fibonacci1
Executable file
@@ -0,0 +1,15 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
def fibonacci(n):
|
||||
if n == 1:
|
||||
return 1
|
||||
if n == 2:
|
||||
return 1
|
||||
|
||||
# Rekursiver Funktionsaufruf
|
||||
return fibonacci(n - 1) + fibonacci(n - 2)
|
||||
|
||||
|
||||
print("fib(5) =", fibonacci(5))
|
||||
print("fib(10) =", fibonacci(10))
|
||||
print("fib(40) =", fibonacci(40))
|
||||
24
Lehrer/pythonkurs/Donnerstag/fibonacci1.count
Executable file
24
Lehrer/pythonkurs/Donnerstag/fibonacci1.count
Executable file
@@ -0,0 +1,24 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
from decorators import counter, get_counter, reset_counter
|
||||
|
||||
@counter
|
||||
def fibonacci(n):
|
||||
if n == 1:
|
||||
return 1
|
||||
if n == 2:
|
||||
return 1
|
||||
|
||||
# Rekursiver Funktionsaufruf
|
||||
return fibonacci(n - 1) + fibonacci(n - 2)
|
||||
|
||||
|
||||
reset_counter(fibonacci)
|
||||
print("fib(5) =", fibonacci(5))
|
||||
print("Anzahl Funktionsaufrufe:", get_counter(fibonacci))
|
||||
reset_counter(fibonacci)
|
||||
print("fib(10) =", fibonacci(10))
|
||||
print("Anzahl Funktionsaufrufe:", get_counter(fibonacci))
|
||||
reset_counter(fibonacci)
|
||||
print("fib(40) =", fibonacci(40))
|
||||
print("Anzahl Funktionsaufrufe:", get_counter(fibonacci))
|
||||
21
Lehrer/pythonkurs/Donnerstag/fibonacci2
Executable file
21
Lehrer/pythonkurs/Donnerstag/fibonacci2
Executable file
@@ -0,0 +1,21 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
def fibonacci(n):
|
||||
if n == 1:
|
||||
return 1
|
||||
if n == 2:
|
||||
return 1
|
||||
|
||||
# Rekursiver Funktionsaufruf
|
||||
# return fibonacci(n - 1) + fibonacci(n - 2)
|
||||
|
||||
fn_1, fn_2 = 1, 1
|
||||
for i in range(n-2):
|
||||
fn_1, fn_2 = fn_1 + fn_2, fn_1
|
||||
|
||||
return fn_1
|
||||
|
||||
|
||||
print("fib(5) =", fibonacci(5))
|
||||
print("fib(10) =", fibonacci(10))
|
||||
print("fib(40) =", fibonacci(40))
|
||||
31
Lehrer/pythonkurs/Donnerstag/fibonacci3
Executable file
31
Lehrer/pythonkurs/Donnerstag/fibonacci3
Executable file
@@ -0,0 +1,31 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
# yield in Funktion erzeugt automatisch Iterator(-Generator)
|
||||
# yield gibt innerhalb einer Iteration einen Wert zurueck
|
||||
# und haelt die Funktion an, damit dort bei der naechsten
|
||||
# Iteration weiter gearbeitet werden kann
|
||||
def fibonacci_folge(n):
|
||||
if n >= 1:
|
||||
#print("yield 1 (fib(1))")
|
||||
yield 1
|
||||
if n >= 2:
|
||||
#print("yield 1 (fib(2))")
|
||||
yield 1
|
||||
|
||||
fn_1, fn_2 = 1, 1
|
||||
for i in range(n-2):
|
||||
fn_1, fn_2 = fn_1 + fn_2, fn_1
|
||||
#print("yield {} (fib({}))".format(fn_1, i+3))
|
||||
yield fn_1
|
||||
|
||||
|
||||
#for n in range(1,31):
|
||||
# print(fibonacci(n))
|
||||
|
||||
for fib in fibonacci_folge(30):
|
||||
print(fib)
|
||||
|
||||
# Keine konkreten Werte mehr, sondern Generator-Objekt
|
||||
# print(fibonacci_folge(30))
|
||||
# print(list(fibonacci_folge(30)))
|
||||
# print([i for i in fibonacci_folge(30)])
|
||||
32
Lehrer/pythonkurs/Donnerstag/fibonacci4
Executable file
32
Lehrer/pythonkurs/Donnerstag/fibonacci4
Executable file
@@ -0,0 +1,32 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
# yield in Funktion erzeugt automatisch Iterator(-Generator)
|
||||
# yield gibt innerhalb einer Iteration einen Wert zurueck
|
||||
# und haelt die Funktion an, damit dort bei der naechsten
|
||||
# Iteration weiter gearbeitet werden kann
|
||||
def fibonacci_folge(n):
|
||||
if n >= 1:
|
||||
#print("yield 1 (fib(1))")
|
||||
yield 1
|
||||
if n >= 2:
|
||||
#print("yield 1 (fib(2))")
|
||||
yield 1
|
||||
|
||||
fn_1, fn_2 = 1, 1
|
||||
for i in range(n-2):
|
||||
fn_1, fn_2 = fn_1 + fn_2, fn_1
|
||||
#print("yield {} (fib({}))".format(fn_1, i+3))
|
||||
yield fn_1
|
||||
|
||||
|
||||
#for n in range(1,31):
|
||||
# print(fibonacci(n))
|
||||
|
||||
# enumerate liefert Tupel aus Schleifendurchlaufsnummer und Wert
|
||||
for nr, fib in enumerate(fibonacci_folge(30)):
|
||||
print(nr+1, fib)
|
||||
|
||||
# Keine konkreten Werte mehr, sondern Generator-Objekt
|
||||
# print(fibonacci_folge(30))
|
||||
# print(list(fibonacci_folge(30)))
|
||||
# print([i for i in fibonacci_folge(30)])
|
||||
28
Lehrer/pythonkurs/Donnerstag/fibonacci5
Executable file
28
Lehrer/pythonkurs/Donnerstag/fibonacci5
Executable file
@@ -0,0 +1,28 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
# yield in Funktion erzeugt automatisch Iterator(-Generator)
|
||||
# yield gibt innerhalb einer Iteration einen Wert zurueck
|
||||
# und haelt die Funktion an, damit dort bei der naechsten
|
||||
# Iteration weiter gearbeitet werden kann
|
||||
def fibonacci_folge(n):
|
||||
if n >= 1:
|
||||
#print("yield 1 (fib(1))")
|
||||
yield 1, 1
|
||||
if n >= 2:
|
||||
#print("yield 1 (fib(2))")
|
||||
yield 2, 1
|
||||
|
||||
fn_1, fn_2 = 1, 1
|
||||
for i in range(n-2):
|
||||
fn_1, fn_2 = fn_1 + fn_2, fn_1
|
||||
#print("yield {} (fib({}))".format(fn_1, i+3))
|
||||
yield i+3, fn_1
|
||||
|
||||
|
||||
for nr, fib in fibonacci_folge(30):
|
||||
print(nr, fib)
|
||||
|
||||
# Keine konkreten Werte mehr, sondern Generator-Objekt
|
||||
# print(fibonacci_folge(30))
|
||||
# print(list(fibonacci_folge(30)))
|
||||
# print([i for i in fibonacci_folge(30)])
|
||||
28
Lehrer/pythonkurs/Donnerstag/goodies
Executable file
28
Lehrer/pythonkurs/Donnerstag/goodies
Executable file
@@ -0,0 +1,28 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
#[(lambda i: i + 1)(n) for n in range(10)]
|
||||
#[ i + 1 for i in range(10)]
|
||||
|
||||
# Achtung: Joey hat sich in den Fuss geschossen!
|
||||
|
||||
# Es scheint, als waere n in der Comprehension eine Referenz
|
||||
# und nicht in jedem Schleifendurchlauf eine neue Variable
|
||||
# Daher funktioniert Currying hier nicht
|
||||
|
||||
mylist = [(lambda i: i + n) for n in range(10)]
|
||||
#print(mylist)
|
||||
|
||||
print(mylist[5](10))
|
||||
print(mylist[8](10))
|
||||
|
||||
mylist = [lambda i: print(n) for n in range(10)]
|
||||
print(mylist[5](10))
|
||||
n = 100
|
||||
print(mylist[5](10))
|
||||
|
||||
print("-" * 30)
|
||||
mylist = []
|
||||
for n in range(10):
|
||||
mylist.append(lambda i: i + n)
|
||||
print(mylist[5](10))
|
||||
print(mylist[8](10))
|
||||
14
Lehrer/pythonkurs/Donnerstag/taxi
Executable file
14
Lehrer/pythonkurs/Donnerstag/taxi
Executable file
@@ -0,0 +1,14 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
for i in range(5):
|
||||
if i == 1:
|
||||
print(i, "Taxi")
|
||||
else:
|
||||
print(i, "Taxen")
|
||||
|
||||
print("-" * 30)
|
||||
|
||||
for i in range(5):
|
||||
print(i, "Taxi" if i == 1 else "Taxen")
|
||||
|
||||
text = "Taxi" if i == 1 else "Taxen"
|
||||
100
Lehrer/pythonkurs/Donnerstag/vectors.py
Executable file
100
Lehrer/pythonkurs/Donnerstag/vectors.py
Executable file
@@ -0,0 +1,100 @@
|
||||
"""
|
||||
Mathematische Vektoren als Klassen
|
||||
|
||||
__funktionen__ sind Magic Methods
|
||||
"""
|
||||
|
||||
class VectorError(Exception):
|
||||
pass
|
||||
|
||||
class VectorTypeError(VectorError):
|
||||
pass
|
||||
|
||||
# implizit wird automatisch von Super-Oberklasse object geerbt
|
||||
class Vector():
|
||||
values = () # Achtung: Klassenvariable (Fallback fuer self.values)
|
||||
def __init__(self, *values):
|
||||
"Initialisierung: 1. Funktion nach Erzeugen des Objekts"
|
||||
self.my_values = values
|
||||
self.values = 0 # legt lokale Variable in self an
|
||||
|
||||
def __del__(self):
|
||||
"Destruktor: Letzte Funktion vor Freigabe des Speichers"
|
||||
try:
|
||||
print("Ein Objekt wird freigegeben ({})".format(self.my_values))
|
||||
except AttributeError:
|
||||
print("Ein Objekt wird freigegeben (unknown)")
|
||||
|
||||
def __len__(self):
|
||||
return len(self.my_values) # ruft intern self.my_values.__len__() auf
|
||||
|
||||
def __str__(self):
|
||||
return type(self).__name__ + str(self.my_values)
|
||||
|
||||
def get_values(self):
|
||||
return self.my_values
|
||||
|
||||
def __mul__(self, factor):
|
||||
#self.get_values() # self.my_values
|
||||
mylist = [v * factor for v in self.get_values()]
|
||||
# *mylist expandiert Liste in Positionsparameter
|
||||
return type(self)(*mylist)
|
||||
|
||||
def __add__(self, other):
|
||||
#if type(self) != type(other):
|
||||
# raise VectorTypeError("Addition nur mit gleichen Klassen")
|
||||
if not isinstance(self, Vector) or not isinstance(other, Vector):
|
||||
raise VectorTypeError("Addition nur von Vektoren")
|
||||
if len(self) != len(other):
|
||||
raise VectorTypeError("Vektoren ungleicher Laenge")
|
||||
summenwerte = []
|
||||
for a, b in zip(self.get_values(), other.get_values()):
|
||||
summenwerte.append(a + b)
|
||||
return type(self)(*summenwerte)
|
||||
|
||||
def __sub__(self, other):
|
||||
return type(self)(*[a-b for a, b in \
|
||||
zip(self.get_values(), other.get_values())])
|
||||
|
||||
def test(*args):
|
||||
print("Aufruf von objekt-orientierter Funktion test")
|
||||
# 1. Element im Tupel ist Objekt selbst
|
||||
print(args)
|
||||
|
||||
def poly(self, *args):
|
||||
"Beispiel fuer Polymorphismus in Python"
|
||||
if len(args) == 1:
|
||||
# 1-stellige Verarbeitung
|
||||
if type(args[0]) is str:
|
||||
print("Stringverarbeitung")
|
||||
elif type(args[0]) is int or type(args[0]) is float:
|
||||
print("Arbeit mit Zahlen")
|
||||
elif len(args) == 2:
|
||||
# 2-stellige Verarbeitung
|
||||
self.poly_2param(*args)
|
||||
elif len(args) == 0:
|
||||
# 0-stellige Verarbeitung
|
||||
Vector.poly_0param(self, *args)
|
||||
else:
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def info(cls):
|
||||
return cls.__name__
|
||||
|
||||
@staticmethod
|
||||
def static():
|
||||
return "statische Methode"
|
||||
|
||||
class Vector2(Vector):
|
||||
def __init__(self, val1, val2):
|
||||
# Vector.__init__(self, val1, val2)
|
||||
# sucht in der Hierarchie der Oberklassen nach der passenden Funktion
|
||||
super().__init__(val1, val2)
|
||||
|
||||
class Vector9(Vector):
|
||||
def __init__(self, *values):
|
||||
if len(values) != 9:
|
||||
raise VectorTypeError("9 elements required")
|
||||
super().__init__(*values)
|
||||
|
||||
15
Lehrer/pythonkurs/Donnerstag/zip
Executable file
15
Lehrer/pythonkurs/Donnerstag/zip
Executable file
@@ -0,0 +1,15 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
def endless():
|
||||
"Endless Range Replacement"
|
||||
start = 0
|
||||
while True:
|
||||
yield start
|
||||
start += 1
|
||||
|
||||
it1 = range(10, 15)
|
||||
it2 = range(100, 105)
|
||||
|
||||
# Zip iteriert ueber alle angegebenen Iterables parallel
|
||||
for a, b, idx in zip(it1, it2, endless()):
|
||||
print(idx, a, b)
|
||||
66
Lehrer/pythonkurs/Donnerstag/zuweisung
Executable file
66
Lehrer/pythonkurs/Donnerstag/zuweisung
Executable file
@@ -0,0 +1,66 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
a = 10
|
||||
b = 13
|
||||
|
||||
print(a, b)
|
||||
|
||||
# Inhalt der Variablen a und b vertauschen
|
||||
tmp = a
|
||||
a = b
|
||||
b = tmp
|
||||
|
||||
print(a, b)
|
||||
|
||||
def foo(i):
|
||||
"Gibt 3 Werte als Tupel zurueck"
|
||||
inc = i + 1
|
||||
add = i + i
|
||||
mul = i * i
|
||||
|
||||
ergebnis = [inc, add, mul]
|
||||
ergebnis = (inc, add, mul)
|
||||
|
||||
# return ergebnis
|
||||
# Automatisches pack -> erzeugt Tupel
|
||||
return inc, add, mul
|
||||
|
||||
# Sieht aus wie Mehrfachzuweisung, ist es aber nicht ;-)
|
||||
a, b = b, a
|
||||
# Anwendung z.B. bei regulaeren Ausdruecken:
|
||||
# ip_address, oktett = match.groups()
|
||||
|
||||
print(a, b)
|
||||
|
||||
a, b, c, d, e = 10, 20, 30, 40, 50
|
||||
print(a, b, c, d, e)
|
||||
|
||||
# Automatisches pack -> erzeugt Tupel
|
||||
whut = 10, 20, 30, 40, 50
|
||||
print(whut)
|
||||
|
||||
mytuple = (10, 13, 14, 15, 16)
|
||||
# Automatisches unpack, Werte werden paarweise zugewiesen
|
||||
a, b, c, d, e = mytuple
|
||||
print(a, b, c, d, e)
|
||||
|
||||
mylist = [23, 24, 25, 26, 27]
|
||||
a, b, c, d, e = mylist
|
||||
print(a, b, c, d, e)
|
||||
|
||||
*a, b, c, d, e = 10, 20, 30, 40, 50, 60
|
||||
print(a, b, c, d, e)
|
||||
a, b, c, d, *e = 10, 20, 30, 40, 50, 60
|
||||
print(a, b, c, d, e)
|
||||
a, b, c, *d, e = 10, 20, 30, 40, 50, 60
|
||||
print(a, b, c, d, e)
|
||||
|
||||
a, b, c = foo(10)
|
||||
result = foo(10)
|
||||
print(a, b, c)
|
||||
print(result)
|
||||
print(*result) # *result erzeugt Menge von Positionsparametern
|
||||
|
||||
e = 6, 7, 8, 9, 10, 11, mytuple
|
||||
print(e)
|
||||
print(*e)
|
||||
Reference in New Issue
Block a user