{"id":117,"date":"2024-01-25T11:17:35","date_gmt":"2024-01-25T11:17:35","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=117"},"modified":"2024-01-25T11:17:36","modified_gmt":"2024-01-25T11:17:36","slug":"python-for-else","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-for-else\/","title":{"rendered":"Python for else"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the Python&nbsp;<code>for else<\/code>&nbsp;statement and how to use it effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python for else statement<\/h2>\n\n\n\n<p>In Python, the\u00a0<code>for<\/code>\u00a0statement can have an optional\u00a0<code>else<\/code>\u00a0clause, which you may not be familiar with especially if you\u2019re coming from other languages such as Java or C#.<\/p>\n\n\n\n<p>The following shows the syntax of the\u00a0<code>for<\/code>\u00a0statement with the\u00a0<code>else<\/code>\u00a0clause:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>for item in iterables: <em># process item<\/em> else: <em># statement<\/em><\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>In this syntax, the\u00a0<code>else<\/code>\u00a0clause will execute only if the loop runs\u00a0<strong>normally<\/strong>. In other words, the\u00a0<code>else<\/code>\u00a0clause won\u2019t execute if the loop encounters a\u00a0<code>break<\/code>\u00a0statement.<\/p>\n\n\n\n<p>In addition, the&nbsp;<code>else<\/code>&nbsp;clause also executes when the iterables object has no item.<\/p>\n\n\n\n<p>The following flowchart illustrates the for\u2026else statement:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/python-for-else.png\" alt=\"\" class=\"wp-image-851\"\/><\/figure>\n\n\n\n<p>The&nbsp;<code>else<\/code>&nbsp;clause is quite useful in some cases if you know how to apply it effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python for else statement example<\/h2>\n\n\n\n<p>Suppose that you have a\u00a0list\u00a0of people, where each person is a\u00a0dictionary\u00a0that consists of\u00a0<code>name<\/code>\u00a0and\u00a0<code>age<\/code>\u00a0like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>people = &#91;{'name': 'John', 'age': 25}, {'name': 'Jane', 'age': 22}, {'name': 'Peter', 'age': 30}, {'name': 'Jenifer', 'age': 28}]<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>And you want to search for a person by name.<\/p>\n\n\n\n<p>If the list contains the person, you want to display the information of that person. Otherwise, you want to show a message saying that the name is not found.<\/p>\n\n\n\n<p>To do it, you may come up with a program like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>people = &#91;{'name': 'John', 'age': 25}, {'name': 'Jane', 'age': 22}, {'name': 'Peter', 'age': 30}, {'name': 'Jenifer', 'age': 28}] name = input('Enter a name:') found = False for person in people: if person&#91;'name'] == name: found = True print(person) break if not found: print(f'{name} not found!') <\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>How it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, prompt for a name by using the&nbsp;<code>input()<\/code>&nbsp;function.<\/li>\n\n\n\n<li>Then, set a flag (<code>found<\/code>) to&nbsp;<code>False<\/code>. If the input name matches with a person on the list, set its value to&nbsp;<code>True<\/code>, show the person\u2019s information and exit the loop by using the&nbsp;<code>break<\/code>&nbsp;statement.<\/li>\n\n\n\n<li>Finally, check the&nbsp;<code>found<\/code>&nbsp;flag and show a message.<\/li>\n<\/ul>\n\n\n\n<p>The following runs a program with the name&nbsp;<code>Peter<\/code>&nbsp;and&nbsp;<code>Maria<\/code>:<\/p>\n\n\n\n<p>1st run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Enter a name:Peter {'name': 'Peter', 'age': 30}<\/code><small>Code language: Shell Session (shell)<\/small><\/code><\/pre>\n\n\n\n<p>2nd run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Enter a name:Maria Maria not found!<\/code><small>Code language: Shell Session (shell)<\/small><\/code><\/pre>\n\n\n\n<p>It works perfectly fine.<\/p>\n\n\n\n<p>However, if you use the&nbsp;<code>for else<\/code>&nbsp;statement, the program will be much shorter.<\/p>\n\n\n\n<p>The following shows the new version of the program that uses the\u00a0<code>for else<\/code>\u00a0statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>people = &#91;{'name': 'John', 'age': 25}, {'name': 'Jane', 'age': 22}, {'name': 'Peter', 'age': 30}, {'name': 'Jenifer', 'age': 28}] name = input('Enter a name:') for person in people: if person&#91;'name'] == name: print(person) break else: print(f'{name} not found!')<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>By using the\u00a0<code>for else<\/code>\u00a0statement, the program doesn\u2019t need to use a\u00a0<code>flag<\/code>\u00a0and an\u00a0<code>if<\/code>\u00a0statement after the loop.<\/p>\n\n\n\n<p>In this new program, if the input name matches a person on the list, it\u2019ll show the person\u2019s information and exit the loop by using the&nbsp;<code>break<\/code>&nbsp;statement.<\/p>\n\n\n\n<p>When the loop encounters the&nbsp;<code>break<\/code>&nbsp;statement, the&nbsp;<code>else<\/code>&nbsp;clause won\u2019t execute.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about the Python&nbsp;for else&nbsp;statement and how to use it effectively. Introduction to the Python for else statement In Python, the\u00a0for\u00a0statement can have an optional\u00a0else\u00a0clause, which you may not be familiar with especially if you\u2019re coming from other languages such as Java or C#. The following shows the syntax of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-117","post","type-post","status-publish","format-standard","hentry","category-8-exception-handling"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=117"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/117\/revisions"}],"predecessor-version":[{"id":118,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/117\/revisions\/118"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}