Skip to content

Arithmetic operations using switch case in Python

  • by

Python doesn’t have a switch statement like some other programming languages such as C or C++. Instead, you can achieve the same functionality using if, elif, and else statements.

Certainly! Python introduced the match statement in PEP 634, which is similar to a switch statement in other languages. It’s available in Python 3.10 and later versions.

match value:
    case pattern_1:
        # Code block for pattern_1
    case pattern_2:
        # Code block for pattern_2
    ...
    case pattern_n:
        # Code block for pattern_n
    case _:
        # Code block for catch-all case
  • match is the keyword that starts the match statement.
  • value is the expression you want to match against the patterns.
  • case pattern: is used to define different patterns you want to match against the value.
  • The case lines are followed by indented code blocks where you specify what should be executed when a match is found.
  • You can have as many case blocks as needed, each with its corresponding pattern and code.
  • The _ pattern is a catch-all case that will match anything that hasn’t been matched by the previous cases.

Arithmetic operations using switch case in Python example

Here’s how you can use the match statement for arithmetic operations:

def arithmetic_operation(operator, num1, num2):
    match operator:
        case '+':
            return num1 + num2
        case '-':
            return num1 - num2
        case '*':
            return num1 * num2
        case '/':
            if num2 != 0:
                return num1 / num2
            else:
                return "Cannot divide by zero"
        case _:
            return "Invalid operator"

operator = input("Enter an operator (+, -, *, /): ")
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

result = arithmetic_operation(operator, num1, num2)
print("Result:", result)

Output:

Arithmetic operations using switch case in Python

In this code, the match statement allows you to match the operator against different cases and execute the corresponding block of code. The _ case is used as a catch-all for any other values that aren’t explicitly handled.

Note: this feature is available in Python 3.10 and later versions. If you’re using an older version of Python, you won’t be able to use the match statement directly.

Another example

def arithmetic_operation(operator, num1, num2):
    result = None

    if operator == '+':
        result = num1 + num2
    elif operator == '-':
        result = num1 - num2
    elif operator == '*':
        result = num1 * num2
    elif operator == '/':
        if num2 != 0:
            result = num1 / num2
        else:
            result = "Cannot divide by zero"
    else:
        result = "Invalid operator"

    return result

operator = input("Enter an operator (+, -, *, /): ")
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

result = arithmetic_operation(operator, num1, num2)
print("Result:", result)

In this example, the arithmetic_operation function takes an operator (+, -, *, /) along with two numbers and performs the corresponding arithmetic operation. The function uses if, elif, and else statements to handle different cases.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading