Olli Graf
10 months ago
4 changed files with 72 additions and 0 deletions
@ -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) |
||||
|
|
||||
|
|
||||
|
|
@ -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}') |
@ -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') |
||||
|
|
||||
|
|
||||
|
|
@ -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 |
Loading…
Reference in new issue