Structural Pattern Matching

Structural Pattern Matching

From

name = input() 
match name: 
	case "Misha": 
		return "Hello Misha" 
	case "John": 
		return "Hello John" 
	case _: 
		return "Go away"

To

match name: 
	case "Misha" | "John": 
		return f"Hello {name}" 
	case "Michelle": 
		return "Long time no see, Michelle" 
	case _: 
		return "Go away"
  • Add Additional Conditions on matches with if

def get_car_price(make, is_turbocharged): 
	match make: 
	case "Subaru" if is_turbocharged: 
		return 10000 
	case "Toyota" if not is_turbocharged: 
		return 7500 
	case _: 
		return 2300