

{"id":17058,"date":"2018-06-01T04:30:36","date_gmt":"2018-05-31T23:00:36","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=17058"},"modified":"2025-07-23T17:48:27","modified_gmt":"2025-07-23T12:18:27","slug":"python-switch-case","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-switch-case\/","title":{"rendered":"2 Simple Ways to Implement Python Switch Case Statement"},"content":{"rendered":"<p>In our last Python tutorial, we studied\u00a0XML Processing in Python 3. Today, we will study How to implement<strong>\u00a0<\/strong>Python Switch Case Statement.<strong>\u00a0<\/strong><\/p>\n<p>Unlike other languages like Java Programming Language and C++, Python does not have a switch-case construct. Along with this, we will see how to work a loophole for Python switch case statement.<\/p>\n<p>So, let&#8217;s discuss different ways of Implementation for Python Switch Case Statement.<\/p>\n<h3>What is Python Switch Case Statement?<\/h3>\n<p>Python does not have a simple switch-case construct. Coming from a Java or C++ background, you may find this to be a bit odd.<\/p>\n<p>In C++ or Java, we have something like this:<\/p>\n<pre class=\"EnlighterJSRAW\">string week(int i){\r\n       switch(i){\r\n               case 0:\r\n                       return \u201cSunday\u201d\r\n                       break;\r\n               case 1:\r\n                       return \u201cMonday\u201d\r\n                       break;\r\n               case 2:\r\n                       return \u201cTuesday\u201d\r\n                       break;\r\n               case 3:\r\n                       return \u201cWednesday\u201d\r\n                       break;\r\n               case 4:\r\n                       return \u201cThursday\u201d\r\n                       break;\r\n               case 5:\r\n                       return \u201cFriday\u201d\r\n                       break;\r\n               case 6:\r\n                       return \u201cSaturday\u201d\r\n                       break;\r\n               default:\r\n                       return \u201cInvalid day of week\u201d\r\n       }\r\n  }<\/pre>\n<p>But Python does not have this.<\/p>\n<p>So, to get around this, we use Python\u2019s built-in dictionary construct to implement cases and decided what to do when a case is met.<\/p>\n<p>We can also specify what to do when none is met.<\/p>\n<h3 class=\"western\">Solutions for Python Switch Case Statement<\/h3>\n<p>One way out would be to implement an if-elif-else ladder. Rather, we can use a dictionary to map cases to their functionality.<\/p>\n<p>Here, we define a function week() to tell us which day a certain day of the week is. A switcher is a dictionary that performs this mapping.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; def week(i):\r\n        switcher={\r\n                0:'Sunday',\r\n                1:'Monday',\r\n                2:'Tuesday',\r\n                3:'Wednesday',\r\n                4:'Thursday',\r\n                5:'Friday',\r\n                6:'Saturday'\r\n             }\r\n         return switcher.get(i,\"Invalid day of week\")<\/pre>\n<p>Now, we make calls to week() with different values.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; week(2)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Tuesday&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; week(0)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Sunday&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; week(7)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Invalid day of week&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; week(4.5)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Invalid day of week&#8217;<\/div>\n<p>As you can see, for values other than the ones we mention in the switcher, it prints out \u201cInvalid day of week\u201d. This is because we tell it to do so using the get() method of a dictionary.<\/p>\n<h4 class=\"western\">a. Using Python Functions &amp; Lambdas<\/h4>\n<p>We can also use functions and lambdas in the dictionary.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; def zero():\r\n        return 'zero'\r\n&gt;&gt;&gt; def one():\r\n        return 'one'\r\n&gt;&gt;&gt; def indirect(i):\r\n        switcher={\r\n                0:zero,\r\n                1:one,\r\n                2:lambda:'two'\r\n                }\r\n        func=switcher.get(i,lambda :'Invalid')\r\n        return func()\r\n&gt;&gt;&gt; indirect(4)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Invalid&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; indirect(2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;two&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; indirect(1)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;one&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; indirect(0.5)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Invalid&#8217;<\/div>\n<h4>b. With Python Classes<\/h4>\n<p>Using this concept with classes lets us choose a method at runtime.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; class Switcher(object):\r\n          def indirect(self,i):\r\n                   method_name='number_'+str(i)\r\n                   method=getattr(self,method_name,lambda :'Invalid')\r\n                   return method()\r\n          def number_0(self):\r\n                   return 'zero'\r\n          def number_1(self):\r\n                   return 'one'\r\n          def number_2(self):\r\n                   return 'two'\r\n&gt;&gt;&gt; s=Switcher()\r\n&gt;&gt;&gt; s.indirect(2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;two&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; s.indirect(4)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Invalid&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; s.number_1()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;one&#8217;<\/div>\n<p>So, this was all about Python Switch Case Statement. Hope you like our tutorial.<\/p>\n<h3>Python Interview Questions on Switch Case Statement<\/h3>\n<p>1. Does Python have a switch case statement?<\/p>\n<p>2. How do you implement a switch case in Python?<\/p>\n<p>3. What is a case in switch statement in Python?<\/p>\n<p>4. Explain Python switch case statement with example.<\/p>\n<p>5. How many cases can you have in switch statement in Python?<\/p>\n<h3 class=\"western\">Conclusion<\/h3>\n<p>Hence, we conclude that Python does not have an in-built switch-case construct, we can use a dictionary instead.<\/p>\n<p>Using pattern matching where it is available makes code clearer, safer, and often faster than long chains of elif tests. Keep patterns simple for grade-school readability and reserve complex guards for deeply nested data where they pay off. This habit ensures maintainable control flow even years later.<\/p>\n<p>Furthermore, if you have any queries regarding Python Switch case Statements, feel free to ask in the comment section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last Python tutorial, we studied\u00a0XML Processing in Python 3. Today, we will study How to implement\u00a0Python Switch Case Statement.\u00a0 Unlike other languages like Java Programming Language and C++, Python does not have&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":17083,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[16495,10415,10552,10634,10875],"class_list":["post-17058","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-implement-python-switch-case","tag-python-classes","tag-python-functions-lambdas","tag-python-lambdas","tag-python-switch-case-statement"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>2 Simple Ways to Implement Python Switch Case Statement - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn different ways to Implement Python Switch Case Statement with examples- Python Function &amp; Lambda function, Python Class\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-switch-case\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"2 Simple Ways to Implement Python Switch Case Statement - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn different ways to Implement Python Switch Case Statement with examples- Python Function &amp; Lambda function, Python Class\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-switch-case\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-05-31T23:00:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-23T12:18:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/How-to-Implement-a-Switch-Case-in-Python-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"2 Simple Ways to Implement Python Switch Case Statement - DataFlair","description":"Learn different ways to Implement Python Switch Case Statement with examples- Python Function & Lambda function, Python Class","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:\/\/data-flair.training\/blogs\/python-switch-case\/","og_locale":"en_US","og_type":"article","og_title":"2 Simple Ways to Implement Python Switch Case Statement - DataFlair","og_description":"Learn different ways to Implement Python Switch Case Statement with examples- Python Function & Lambda function, Python Class","og_url":"https:\/\/data-flair.training\/blogs\/python-switch-case\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-05-31T23:00:36+00:00","article_modified_time":"2025-07-23T12:18:27+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/How-to-Implement-a-Switch-Case-in-Python-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-switch-case\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-switch-case\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"2 Simple Ways to Implement Python Switch Case Statement","datePublished":"2018-05-31T23:00:36+00:00","dateModified":"2025-07-23T12:18:27+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-switch-case\/"},"wordCount":467,"commentCount":13,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-switch-case\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/How-to-Implement-a-Switch-Case-in-Python-01.jpg","keywords":["Implement Python Switch Case","python classes","Python Functions &amp; Lambdas","Python Lambdas","Python Switch Case Statement"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-switch-case\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-switch-case\/","url":"https:\/\/data-flair.training\/blogs\/python-switch-case\/","name":"2 Simple Ways to Implement Python Switch Case Statement - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-switch-case\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-switch-case\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/How-to-Implement-a-Switch-Case-in-Python-01.jpg","datePublished":"2018-05-31T23:00:36+00:00","dateModified":"2025-07-23T12:18:27+00:00","description":"Learn different ways to Implement Python Switch Case Statement with examples- Python Function & Lambda function, Python Class","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-switch-case\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-switch-case\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-switch-case\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/How-to-Implement-a-Switch-Case-in-Python-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/How-to-Implement-a-Switch-Case-in-Python-01.jpg","width":1200,"height":628,"caption":"2 Simple Ways to Implement Python Switch Case Statement"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-switch-case\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"2 Simple Ways to Implement Python Switch Case Statement"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/17058","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=17058"}],"version-history":[{"count":13,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/17058\/revisions"}],"predecessor-version":[{"id":147751,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/17058\/revisions\/147751"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/17083"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=17058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=17058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=17058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}