{"id":5960,"date":"2019-10-01T09:36:56","date_gmt":"2019-10-01T04:06:56","guid":{"rendered":"https:\/\/www.csestack.org\/?p=5960"},"modified":"2019-10-01T09:39:48","modified_gmt":"2019-10-01T04:09:48","slug":"python-for-while-loop-else","status":"publish","type":"post","link":"https:\/\/www.csestack.org\/python-for-while-loop-else\/","title":{"rendered":"Python else after for\/while loop explained with Example"},"content":{"rendered":"<p>For most of the programming languages like C\/C++, Java&#8230;, else-statement is coupled with if-statement. The else-statement can be used only with the if-statement.<\/p>\n<p>This is not the case with Python. As usual, you are free to use else-statement with if-statement. You can also use else-statement after for or while loop.<\/p>\n<p><strong>But, how does it work?<\/strong><\/p>\n<p>In Python, if you are using else statement after the loop&#8230;<\/p>\n<p><strong>The else-block will not be executed if the break statement is executed inside the loop<\/strong>.<\/p>\n<p>This is really a tricky and exceptional concept. There are many questions asked in job interviews based on this concept.<\/p>\n<p>As a part of this tutorial, you will learn using else-statement after for and while loop in Python.<\/p>\n<p>Let&#8217;s take some examples.<\/p>\n<h4>Example: Python for else<\/h4>\n<p>The syntax is simple. Write else-block just after for loop.<\/p>\n<p>[python]<br \/>\nfor i in range(0,5):<br \/>\n    print(i)<\/p>\n<p>else:<br \/>\n    print(&quot;inside else&quot;)<br \/>\n[\/python]<\/p>\n<p>What is the output of this program?<\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre>0\r\n1\r\n2\r\n3\r\n4\r\ninside else<\/pre>\n<p>Here else-statement is getting executed as there is no break statement inside the for-loop.<\/p>\n<p>We are using <a href=\"https:\/\/www.csestack.org\/difference-between-range-and-xrange-python\/\">range() method to iterate over for-loop<\/a>.<\/p>\n<p>Now, let&#8217;s add break statement inside for loop.<\/p>\n<p>[python]<br \/>\nfor i in range(0,5):<br \/>\n    if i==3:<br \/>\n        break;<br \/>\n    print(i)<\/p>\n<p>else:<br \/>\n    print(&quot;inside else&quot;)<br \/>\n[\/python]<\/p>\n<p>Execute the program and print the output.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>0<br \/>\n1<br \/>\n2<\/p>\n<p>When i==3 inside for loop, the break statement will be executed. Control will come out of the for-loop and will not execute the else-block.<\/p>\n<p>Now consider while loop.<\/p>\n<h4>Example: Python while else<\/h4>\n<p>You can also use else statement with while loop.<\/p>\n<p>[python]<br \/>\ni=0<br \/>\nwhile i&lt;5:<br \/>\n    print(i)<br \/>\n    i=i+1<\/p>\n<p>else:<br \/>\n    print(&quot;inside else&quot;)<br \/>\n[\/python]<\/p>\n<p>What is the output of this program?<\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre>0\r\n1\r\n2\r\n3\r\n4\r\ninside else<\/pre>\n<p>The else-block is executed as there is no break statement inside the while loop.<\/p>\n<p>Now we are adding break statement inside the while loop.<\/p>\n<p>[python]i=0<br \/>\nwhile i&lt;5:<br \/>\n    if i==3:<br \/>\n        break<br \/>\n    print(i)<br \/>\n    i=i+1<\/p>\n<p>else:<br \/>\n    print(&quot;inside else&quot;)<br \/>\n[\/python]<\/p>\n<p>Run this program again and see the output.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre>0\r\n1\r\n2<\/pre>\n<p>As break statement has occurred inside the while-loop, else-block is not executed.<\/p>\n<p>This is a little confusing for many of us. Just to remember- when there is a break, there is no else. When there is no break, there is else.<\/p>\n<p><strong>Note:<\/strong> Python for else and Python while else statements work same in Python 2 and Python 3. You can try running this program in different Python versions.<\/p>\n<p><strong>Related tutorials:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/www.csestack.org\/elif-nested-if-else-in-python-3-code-example-syntax\/\">Python if-else Explained with Examples<\/a><\/li>\n<li><a href=\"https:\/\/www.csestack.org\/basic-python3-syntax-code-example\/\">[cheat sheet] Basic Python3 Syntax<\/a><\/li>\n<\/ul>\n<p>Any doubt? Write in the comment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Explained with Examples] Python for else and while else statement. Else statement after the loop in Python.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,73],"tags":[72],"class_list":["post-5960","post","type-post","status-publish","format-standard","hentry","category-code","category-python","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python else after for\/while loop explained with Example<\/title>\n<meta name=\"description\" content=\"Python for else and while else statement. Else statement after the loop in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.csestack.org\/python-for-while-loop-else\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python else after for\/while loop explained with Example\" \/>\n<meta property=\"og:description\" content=\"Python for else and while else statement. Else statement after the loop in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.csestack.org\/python-for-while-loop-else\/\" \/>\n<meta property=\"og:site_name\" content=\"CSEstack\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-01T04:06:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-10-01T04:09:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2024\/01\/csestack-blog.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Aniruddha Chaudhari\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:site\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Aniruddha Chaudhari\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-for-while-loop-else\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-for-while-loop-else\\\/\"},\"author\":{\"name\":\"Aniruddha Chaudhari\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"headline\":\"Python else after for\\\/while loop explained with Example\",\"datePublished\":\"2019-10-01T04:06:56+00:00\",\"dateModified\":\"2019-10-01T04:09:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-for-while-loop-else\\\/\"},\"wordCount\":428,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"keywords\":[\"Python\"],\"articleSection\":[\"Code\",\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/python-for-while-loop-else\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-for-while-loop-else\\\/\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/python-for-while-loop-else\\\/\",\"name\":\"Python else after for\\\/while loop explained with Example\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\"},\"datePublished\":\"2019-10-01T04:06:56+00:00\",\"dateModified\":\"2019-10-01T04:09:48+00:00\",\"description\":\"Python for else and while else statement. Else statement after the loop in Python.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-for-while-loop-else\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/python-for-while-loop-else\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-for-while-loop-else\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.csestack.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python else after for\\\/while loop explained with Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/\",\"name\":\"CSEstack\",\"description\":\"Computer Science &amp; Programming Portal\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.csestack.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\",\"name\":\"Aniruddha Chaudhari\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"contentUrl\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"width\":634,\"height\":634,\"caption\":\"Aniruddha Chaudhari\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\"},\"description\":\"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.\",\"sameAs\":[\"https:\\\/\\\/www.csestack.org\",\"https:\\\/\\\/www.facebook.com\\\/aniruddha.ca\",\"pythonwithani\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/aniruddha28\\\/\",\"https:\\\/\\\/x.com\\\/ani_chaudhari\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCw0a__B0eJsvCujkSIfLTAA\"],\"url\":\"https:\\\/\\\/www.csestack.org\\\/author\\\/anicse\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python else after for\/while loop explained with Example","description":"Python for else and while else statement. Else statement after the loop in Python.","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:\/\/www.csestack.org\/python-for-while-loop-else\/","og_locale":"en_US","og_type":"article","og_title":"Python else after for\/while loop explained with Example","og_description":"Python for else and while else statement. Else statement after the loop in Python.","og_url":"https:\/\/www.csestack.org\/python-for-while-loop-else\/","og_site_name":"CSEstack","article_publisher":"https:\/\/www.facebook.com\/aniruddha.ca","article_author":"https:\/\/www.facebook.com\/aniruddha.ca","article_published_time":"2019-10-01T04:06:56+00:00","article_modified_time":"2019-10-01T04:09:48+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2024\/01\/csestack-blog.jpg","type":"image\/jpeg"}],"author":"Aniruddha Chaudhari","twitter_card":"summary_large_image","twitter_creator":"@ani_chaudhari","twitter_site":"@ani_chaudhari","twitter_misc":{"Written by":"Aniruddha Chaudhari","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.csestack.org\/python-for-while-loop-else\/#article","isPartOf":{"@id":"https:\/\/www.csestack.org\/python-for-while-loop-else\/"},"author":{"name":"Aniruddha Chaudhari","@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"headline":"Python else after for\/while loop explained with Example","datePublished":"2019-10-01T04:06:56+00:00","dateModified":"2019-10-01T04:09:48+00:00","mainEntityOfPage":{"@id":"https:\/\/www.csestack.org\/python-for-while-loop-else\/"},"wordCount":428,"commentCount":0,"publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"keywords":["Python"],"articleSection":["Code","Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.csestack.org\/python-for-while-loop-else\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.csestack.org\/python-for-while-loop-else\/","url":"https:\/\/www.csestack.org\/python-for-while-loop-else\/","name":"Python else after for\/while loop explained with Example","isPartOf":{"@id":"https:\/\/www.csestack.org\/#website"},"datePublished":"2019-10-01T04:06:56+00:00","dateModified":"2019-10-01T04:09:48+00:00","description":"Python for else and while else statement. Else statement after the loop in Python.","breadcrumb":{"@id":"https:\/\/www.csestack.org\/python-for-while-loop-else\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.csestack.org\/python-for-while-loop-else\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.csestack.org\/python-for-while-loop-else\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.csestack.org\/"},{"@type":"ListItem","position":2,"name":"Python else after for\/while loop explained with Example"}]},{"@type":"WebSite","@id":"https:\/\/www.csestack.org\/#website","url":"https:\/\/www.csestack.org\/","name":"CSEstack","description":"Computer Science &amp; Programming Portal","publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.csestack.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218","name":"Aniruddha Chaudhari","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","contentUrl":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","width":634,"height":634,"caption":"Aniruddha Chaudhari"},"logo":{"@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg"},"description":"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.","sameAs":["https:\/\/www.csestack.org","https:\/\/www.facebook.com\/aniruddha.ca","pythonwithani","https:\/\/www.linkedin.com\/in\/aniruddha28\/","https:\/\/x.com\/ani_chaudhari","https:\/\/www.youtube.com\/channel\/UCw0a__B0eJsvCujkSIfLTAA"],"url":"https:\/\/www.csestack.org\/author\/anicse\/"}]}},"_links":{"self":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/5960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/comments?post=5960"}],"version-history":[{"count":10,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/5960\/revisions"}],"predecessor-version":[{"id":5970,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/5960\/revisions\/5970"}],"wp:attachment":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/media?parent=5960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/categories?post=5960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/tags?post=5960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}