{"id":36,"date":"2025-07-29T10:00:56","date_gmt":"2025-07-29T10:00:56","guid":{"rendered":"https:\/\/medium.com\/p\/82fdc416b322"},"modified":"2025-07-31T08:31:24","modified_gmt":"2025-07-31T08:31:24","slug":"simple-arithmetic-operations-using-python-match-case","status":"publish","type":"post","link":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/","title":{"rendered":"Simple Arithmetic Operations Using Python&#8217;s Match-Case Statements"},"content":{"rendered":"<h2><strong>Simple Arithmetic Operations Using Python&#8217;s Match-Case Statements (Python 3.10+)<\/strong><\/h2>\n<p>In Python 3.10 and above, the <code>match-case<\/code> construct was introduced as a more powerful and flexible way to handle control flow. If you&#8217;re using Python 3.10 or later, this is an excellent opportunity to explore the new <code>match-case<\/code> statement to implement arithmetic operations. In this blog, I will show you how to use the <code>match-case<\/code> statement to perform basic arithmetic operations such as addition, subtraction, multiplication, and division.<\/p>\n<h3><strong>Understanding Python&#8217;s <code>match-case<\/code> Statement<\/strong><\/h3>\n<p>The <code>match-case<\/code> statement in Python is part of a more general approach to pattern matching. It enables cleaner, more readable control flow based on conditions, which is especially useful when handling multiple cases, like performing arithmetic operations.<\/p>\n<p>If you&#8217;re following along with this tutorial, make sure you have Python version 3.10 or higher installed on your machine.<\/p>\n<p>Learn more about <a href=\"https:\/\/www.geeksforgeeks.org\/python\/python-arithmetic-operators\/\" target=\"_blank\" rel=\"noopener\">Python Arithmetic Operators<\/a> here.<\/p>\n<h3><strong>Example: Performing Arithmetic Operations Using <code>match-case<\/code><\/strong><\/h3>\n<p>In this demonstration, I will show you how to implement arithmetic operations using Python&#8217;s <code>match-case<\/code> statement. This allows users to input two numbers and select an operation, which will be carried out based on their choice.<\/p>\n<p>Here is the code for performing the four basic arithmetic operations:<\/p>\n<pre><code class=\"language-python\">\"\"\"\r\nThis 'match'-'case' statement feature is available from Python version 3.10 onwards.\r\n\"\"\"\r\n\r\ndef arithmetic_operations(operation, first, second):\r\n    \"\"\"\r\n    This function performs arithmetic operations like addition, subtraction, multiplication, and division.\r\n    \r\n    :param operation: Operation type - (1) addition, (2) subtraction, (3) multiplication, (4) division.\r\n    :param first: The first number\r\n    :param second: The second number\r\n    :return: None\r\n    \"\"\"\r\n    match operation:\r\n        case 1:\r\n            addition = first + second\r\n            print(f\"Addition of {first} + {second} = {addition}\")\r\n        case 2:\r\n            subtraction = first - second\r\n            print(f\"Subtraction of {first} - {second} = {subtraction}\")\r\n        case 3:\r\n            multiplication = first * second\r\n            print(f\"Multiplication of {first} * {second} = {multiplication}\")\r\n        case 4:\r\n            if second == 0:\r\n                print(\"The divisor must not be zero. Please restart the process.\")\r\n                return False\r\n            division = first \/ second\r\n            print(f\"Division of {first} \/ {second} = {division}\")\r\n        case _:\r\n            print(\"Re-select a valid operation\")\r\n\r\nif __name__ == \"__main__\":\r\n    while True:\r\n        x = float(input(\"Enter your first number: \"))\r\n        y = float(input(\"Enter your second number: \"))\r\n        print(\"1. Addition | 2. Subtraction | 3. Multiplication | 4. Division\")\r\n        op = int(input(\"Enter a number for the operation: \"))\r\n\r\n        arithmetic_operations(op, x, y)\r\n\r\n        con = input(\"Do you want to continue (y\/n): \")\r\n        if con.lower() == 'n':\r\n            break\r\n<\/code><\/pre>\n<h3><strong>Explanation of the Code<\/strong><\/h3>\n<ul>\n<li><strong>Function Definition<\/strong>:<br \/>\nThe function <code>arithmetic_operations<\/code> is defined to take three arguments: <code>operation<\/code> (which specifies the type of arithmetic operation), <code>first<\/code> (the first number), and <code>second<\/code> (the second number).<\/li>\n<li><strong>The <code>match-case<\/code> Statement<\/strong>:<br \/>\nThe <code>match-case<\/code> statement is used here to check the value of <code>operation<\/code> and execute the corresponding block of code based on the user&#8217;s input. The four cases represent addition, subtraction, multiplication, and division.<\/li>\n<li><strong>Case Default<\/strong>:<br \/>\nThe <code>case _<\/code> syntax is used to handle invalid inputs, printing a message that prompts the user to select a valid operation.<\/li>\n<li><strong>Error Handling for Division<\/strong>:<br \/>\nInside the <code>case 4<\/code> (for division), there&#8217;s an additional check to prevent division by zero. If <code>second == 0<\/code>, a message is displayed, and the function returns <code>False<\/code>.<\/li>\n<li><strong>Loop for Continuous Input<\/strong>:<br \/>\nThe program runs in a <code>while True<\/code> loop, continuously prompting the user for input until they choose to exit by entering &#8216;n&#8217; when asked if they want to continue.<\/li>\n<\/ul>\n<h3><strong>How This Works<\/strong><\/h3>\n<ol>\n<li><strong>User Input<\/strong>:\n<ul>\n<li>The user is prompted to enter two numbers.<\/li>\n<li>Then, they are asked to select an arithmetic operation (1 for addition, 2 for subtraction, 3 for multiplication, and 4 for division).<\/li>\n<\/ul>\n<\/li>\n<li><strong>Performing Operations<\/strong>:\n<ul>\n<li>Based on the user\u2019s choice, the appropriate arithmetic operation is performed using the <code>match-case<\/code> statement.<\/li>\n<li>The result of the operation is then displayed.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Re-running or Exiting<\/strong>:\n<ul>\n<li>After displaying the result, the program asks if the user wants to continue. If they enter &#8216;n&#8217;, the loop exits, and the program terminates.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3><strong>Why Use <code>match-case<\/code> for Arithmetic Operations?<\/strong><\/h3>\n<p>While using <code>if-elif-else<\/code> statements for simple conditions is common in earlier Python versions, the <code>match-case<\/code> statement introduced in Python 3.10 makes the code cleaner and more readable, especially when dealing with multiple cases. It&#8217;s easier to scale and maintain, particularly when adding new types of operations or conditions.<\/p>\n<h3><strong>Conclusion<\/strong><\/h3>\n<p>By leveraging Python 3.10&#8217;s <code>match-case<\/code> statements, we can implement cleaner and more efficient arithmetic operations. This feature helps reduce the need for multiple <code>if-elif<\/code> branches, making the code more readable and maintainable.<\/p>\n<p>Remember, the <code>match-case<\/code> statement is available starting from Python 3.10, so ensure you&#8217;re using a version that&#8217;s 3.10 or newer.<\/p>\n<h3>Read More<\/h3>\n<p>Read more about Python&#8217;s match case here &#8211; <a href=\"https:\/\/www.geeksforgeeks.org\/python\/python-match-case-statement\/\" target=\"_blank\" rel=\"noopener\">Click Here<\/a><\/p>\n<hr \/>\n<table style=\"width: 100%; border-collapse: collapse;\" border=\"1\">\n<thead>\n<tr>\n<th style=\"padding: 8px; text-align: center;\">Related Post<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 8px; text-align: center;\"><a href=\"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/\" rel=\"noopener\">Simple Arithmetic Operations using PHP<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to perform basic arithmetic operations in Python using the new match-case statement introduced in Python 3.10.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[17,53],"tags":[56,58,55,54,57],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-programming","category-python","tag-arithmetic-operations-in-python","tag-basic-python-operations","tag-match-case-statement","tag-python-3-10","tag-python-match-case"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Simple Arithmetic Operations Using Python&#039;s Match-Case Statements<\/title>\n<meta name=\"description\" content=\"Learn how to perform basic arithmetic operations in Python using the new match-case statement introduced in Python 3.10.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Simple Arithmetic Operations Using Python&#039;s Match-Case Statements\" \/>\n<meta property=\"og:description\" content=\"Learn how to perform basic arithmetic operations in Python using the new match-case statement introduced in Python 3.10.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/\" \/>\n<meta property=\"og:site_name\" content=\"Practical Kishor\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-29T10:00:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-31T08:31:24+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a\"},\"headline\":\"Simple Arithmetic Operations Using Python&#8217;s Match-Case Statements\",\"datePublished\":\"2025-07-29T10:00:56+00:00\",\"dateModified\":\"2025-07-31T08:31:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/\"},\"wordCount\":546,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a\"},\"keywords\":[\"arithmetic operations in Python\",\"basic Python operations\",\"match-case statement\",\"Python 3.10\",\"Python match-case\"],\"articleSection\":[\"Programming\",\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/\",\"url\":\"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/\",\"name\":\"Simple Arithmetic Operations Using Python's Match-Case Statements\",\"isPartOf\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/#website\"},\"datePublished\":\"2025-07-29T10:00:56+00:00\",\"dateModified\":\"2025-07-31T08:31:24+00:00\",\"description\":\"Learn how to perform basic arithmetic operations in Python using the new match-case statement introduced in Python 3.10.\",\"breadcrumb\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/kishor10d.com\/blogs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Simple Arithmetic Operations Using Python&#8217;s Match-Case Statements\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/kishor10d.com\/blogs\/#website\",\"url\":\"https:\/\/kishor10d.com\/blogs\/\",\"name\":\"Practical Kishor\",\"description\":\"The quick brown fox jumps over the lazy dog\",\"publisher\":{\"@id\":\"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/kishor10d.com\/blogs\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"logo\":{\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g\"},\"sameAs\":[\"https:\/\/kishor10d.com\/blogs\"],\"url\":\"https:\/\/kishor10d.com\/blogs\/author\/kishor10d\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Simple Arithmetic Operations Using Python's Match-Case Statements","description":"Learn how to perform basic arithmetic operations in Python using the new match-case statement introduced in Python 3.10.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/","og_locale":"en_US","og_type":"article","og_title":"Simple Arithmetic Operations Using Python's Match-Case Statements","og_description":"Learn how to perform basic arithmetic operations in Python using the new match-case statement introduced in Python 3.10.","og_url":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/","og_site_name":"Practical Kishor","article_published_time":"2025-07-29T10:00:56+00:00","article_modified_time":"2025-07-31T08:31:24+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/#article","isPartOf":{"@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/"},"author":{"name":"admin","@id":"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a"},"headline":"Simple Arithmetic Operations Using Python&#8217;s Match-Case Statements","datePublished":"2025-07-29T10:00:56+00:00","dateModified":"2025-07-31T08:31:24+00:00","mainEntityOfPage":{"@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/"},"wordCount":546,"commentCount":0,"publisher":{"@id":"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a"},"keywords":["arithmetic operations in Python","basic Python operations","match-case statement","Python 3.10","Python match-case"],"articleSection":["Programming","Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/","url":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/","name":"Simple Arithmetic Operations Using Python's Match-Case Statements","isPartOf":{"@id":"https:\/\/kishor10d.com\/blogs\/#website"},"datePublished":"2025-07-29T10:00:56+00:00","dateModified":"2025-07-31T08:31:24+00:00","description":"Learn how to perform basic arithmetic operations in Python using the new match-case statement introduced in Python 3.10.","breadcrumb":{"@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-python-match-case\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kishor10d.com\/blogs\/"},{"@type":"ListItem","position":2,"name":"Simple Arithmetic Operations Using Python&#8217;s Match-Case Statements"}]},{"@type":"WebSite","@id":"https:\/\/kishor10d.com\/blogs\/#website","url":"https:\/\/kishor10d.com\/blogs\/","name":"Practical Kishor","description":"The quick brown fox jumps over the lazy dog","publisher":{"@id":"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kishor10d.com\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g","caption":"admin"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g"},"sameAs":["https:\/\/kishor10d.com\/blogs"],"url":"https:\/\/kishor10d.com\/blogs\/author\/kishor10d\/"}]}},"_links":{"self":[{"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":5,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":77,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/posts\/36\/revisions\/77"}],"wp:attachment":[{"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}