diff --git a/teil24/match_list.py b/teil24/match_list.py new file mode 100644 index 0000000..bff59a9 --- /dev/null +++ b/teil24/match_list.py @@ -0,0 +1,19 @@ + +def print_match(wert): + + print(f'wert={wert}') + match wert: + case 1|2: + print('1 oder 2') + case 3|4: + print('3 oder 4') + case _: + print('unbekannt') + + +print_match(1) +print_match(3) +print_match(5) + + + diff --git a/teil24/match_structure.py b/teil24/match_structure.py new file mode 100644 index 0000000..707742f --- /dev/null +++ b/teil24/match_structure.py @@ -0,0 +1,11 @@ +values= ['John'] +# Matching structure in Python switch-case +match values: + case [a]: + print(f'Only one item: {a}') + case [a, b]: + print(f'Two items: {a}, {b}') + case [a, b, c]: + print(f'Three items: {a}, {b}, and {c}') + case [a, b, c, *rest]: + print(f'More than three items: {a}, {b}, {c}, as well as: {rest}') diff --git a/teil24/print_class.py b/teil24/print_class.py new file mode 100644 index 0000000..3da48f7 --- /dev/null +++ b/teil24/print_class.py @@ -0,0 +1,19 @@ + +def print_match(name): + + print(f'name={name}') + match name: + case 'Lisa'|'Ralph'|'Janey': + print('zweite Klasse bei Ms. Hoover') + case 'Bart'|'Milhouse'|'Nelson': + print('vierte Klasse bei Ms. Krabappel') + case _: + print('unbekannt') + + +print_match('Lisa') +print_match('Milhouse') +print_match('Homer') + + + diff --git a/teil24/print_type.py b/teil24/print_type.py new file mode 100644 index 0000000..7ba0634 --- /dev/null +++ b/teil24/print_type.py @@ -0,0 +1,23 @@ +#encoding:utf-8 +def print_type(value): + match value: + case int(): + print('Integer') + case str(): + print('String') + case list(): + print('Liste') + case dict() as d: + print(f'Dictionary mit {len(d)} Einträgen') + case _: + print('unbekannter Datentyp') + +print_type(1) #Integer + +print_type('The Simpsons') # String + +print_type([1, 2, 3]) # Liste + +print_type({'a': 1, 'b': 2, 'c': 3}) # Dictionary + +print_type(1.0) #float ist nicht implentiert