Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

28
Views
Is there a way to match inequalities in Python ≥ 3.10?

The new structural pattern matching feature in Python 3.10 is a very welcome feature. Is there a way to match inequalities using this statement? Prototype example:

match a:
    case < 42:
        print('Less')
    case == 42:
        print('The answer')
    case > 42:
        print('Greater')
10 months ago · Santiago Trujillo
1 answers
Answer question

0

You can use guards:

match a:
   case _ if a < 42:
      print('Less')
   case _ if a == 42:
     print('The answer')
   case _ if a > 42:
     print('Greater')

Another option, without guards, using pure pattern matching:

match [a < 42, a == 42]:
   case [True, False]:
      print('Less')
   case [_, True]:
      print('The answer')
   case [False, False]:
      print('Greater')
10 months ago · Santiago Trujillo Report
Answer question
Find remote jobs