{"id":33,"date":"2024-01-24T13:25:54","date_gmt":"2024-01-24T13:25:54","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=33"},"modified":"2024-01-24T13:25:56","modified_gmt":"2024-01-24T13:25:56","slug":"python-if-statement","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-if-statement\/","title":{"rendered":"Python if Statement"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use the Python&nbsp;<code>if<\/code>&nbsp;statement to execute a block of code based on a condition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The simple Python if statement<\/h2>\n\n\n\n<p>You use the&nbsp;<code>if&nbsp;<\/code>statement to execute a block of code based on a specified condition.<\/p>\n\n\n\n<p>The syntax of the\u00a0<code>if\u00a0<\/code>statement is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>if condition: if-block<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>if<\/code>&nbsp;statement checks the condition first.<\/p>\n\n\n\n<p>If the condition evaluates to&nbsp;<code>True<\/code>, it executes the statements in the if-block. Otherwise, it ignores the statements.<\/p>\n\n\n\n<p>Note that the colon (<code>:<\/code>) that follows the&nbsp;<code>condition<\/code>&nbsp;is very important. If you forget it, you\u2019ll get a syntax error.<\/p>\n\n\n\n<p>The following flowchart illustrates the if 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-if-Statement.png\" alt=\"\" class=\"wp-image-835\"\/><\/figure>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>age = input('Enter your age:') if int(age) >= 18: print(\"You're eligible to vote.\")<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>This example prompts you to input your age. If you enter a number that is greater than or equal to&nbsp;<code>18<\/code>, it\u2019ll show a message&nbsp;<code>\"You're eligible to vote\"<\/code>&nbsp;on the screen. Otherwise, it does nothing.<\/p>\n\n\n\n<p>The condition\u00a0<code>int(age) >= 18<\/code>\u00a0converts the input string to an integer and compares it with 18.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Enter your age:18 You're eligible to vote.<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>See the following example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>age = input('Enter your age:') if int(age) >= 18: print(\"You're eligible to vote.\") print(\"Let's go and vote.\")<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, if you enter a number that is greater than or equal to&nbsp;<code>18<\/code>, you\u2019ll see two messages.<\/p>\n\n\n\n<p>In this example, indentation is very important. Any statement that follows the&nbsp;<code>if<\/code>&nbsp;statement needs to have four spaces.<\/p>\n\n\n\n<p>If you don\u2019t use the indentation correctly, the program will work differently. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>age = input('Enter your age:') if int(age) >= 18: print(\"You're eligible to vote.\") print(\"Let's go and vote.\") <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the final statement always executes regardless of the\u00a0<code>condition<\/code>\u00a0in the\u00a0<code>if<\/code>\u00a0statement. The reason is that it doesn\u2019t belong to the\u00a0<code>if<\/code>\u00a0block:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Enter your age:11 Let's go and vote.<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python if\u2026else statement<\/h2>\n\n\n\n<p>Typically, you want to perform an action when a condition is&nbsp;<code>True<\/code>&nbsp;and another action when the condition is&nbsp;<code>False<\/code>.<\/p>\n\n\n\n<p>To do so, you use the&nbsp;<code>if...else<\/code>&nbsp;statement.<\/p>\n\n\n\n<p>The following shows the syntax of the\u00a0<code>if...else<\/code>\u00a0statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>if condition: if-block; else: else-block;<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this syntax, the&nbsp;<code>if...else<\/code>&nbsp;will execute the&nbsp;<code>if-block<\/code>&nbsp;if the condition evaluates to&nbsp;<code>True<\/code>. Otherwise, it\u2019ll execute the&nbsp;<code>else-block<\/code>.<\/p>\n\n\n\n<p>The following flowchart illustrates the&nbsp;<code>if..else<\/code>&nbsp;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-if-else-statement.png\" alt=\"\" class=\"wp-image-836\"\/><\/figure>\n\n\n\n<p>The following example illustrates you how to use the\u00a0\u00a0<code>if...else<\/code>\u00a0statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>age = input('Enter your age:') if int(age) >= 18: print(\"You're eligible to vote.\") else: print(\"You're not eligible to vote.\")<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, if you enter your age with a number less than 18, you\u2019ll see the message\u00a0<code>\"You're not eligible to vote.\"<\/code>\u00a0like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Enter your age:11 You're not eligible to vote.<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Python if\u2026elif\u2026else statement<\/h2>\n\n\n\n<p>If you want to check multiple conditions and perform an action accordingly, you can use the&nbsp;<code>if...elif...else<\/code>&nbsp;statement. The&nbsp;<code>elif<\/code>&nbsp;stands for&nbsp;<code>else if<\/code>.<\/p>\n\n\n\n<p>Here is the syntax if the\u00a0<code>if...elif...else<\/code>\u00a0statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>if if-condition: if-block elif elif-condition1: elif-block1 elif elif-condition2: elif-block2 ... else: else-block<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>if...elif...else<\/code>&nbsp;statement checks each condition (<code>if-condition<\/code>,&nbsp;<code>elif-condition1<\/code>,&nbsp;<code>elif-condition2<\/code>, \u2026) in the order that they appear in the statement until it finds the one that evaluates to&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<p>When the&nbsp;<code>if...elif...else<\/code>&nbsp;statement finds one, it executes the statement that follows the condition and skips testing the remaining conditions.<\/p>\n\n\n\n<p>If no condition evaluates to&nbsp;<code>True<\/code>, the&nbsp;<code>if...elif...else<\/code>&nbsp;statement executes the statement in the&nbsp;<code>else<\/code>&nbsp;branch.<\/p>\n\n\n\n<p>Note that the&nbsp;<code>else<\/code>&nbsp;block is optional. If you omit it and no condition is&nbsp;<code>True<\/code>, the statement does nothing.<\/p>\n\n\n\n<p>The following flowchart illustrates the&nbsp;<code>if...elif...else<\/code>&nbsp;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-if-elif-else-statement.png\" alt=\"\" class=\"wp-image-837\"\/><\/figure>\n\n\n\n<p>The following example uses the\u00a0<code>if...elif..else<\/code>\u00a0statement to determine the ticket price based on the age:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>age = input('Enter your age:') <em># convert the string to int<\/em> your_age = int(age) <em># determine the ticket price<\/em> if your_age &lt; 5: ticket_price = 5 elif your_age &lt; 16: ticket_price = 10 else: ticket_price = 18 <em># show the ticket price<\/em> print(f\"You'll pay ${ticket_price} for the ticket\") <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the input age is less than 5, the ticket price will be $5.<\/li>\n\n\n\n<li>If the input age is greater than or equal to 5 and less than 16, the ticket price is $10.<\/li>\n\n\n\n<li>Otherwise, the ticket price is $18.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use the Python&nbsp;if&nbsp;statement to execute a block of code based on a condition. The simple Python if statement You use the&nbsp;if&nbsp;statement to execute a block of code based on a specified condition. The syntax of the\u00a0if\u00a0statement is as follows: The&nbsp;if&nbsp;statement checks the condition first. If the condition [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-3-control-flow"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/33","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=33"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":34,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/33\/revisions\/34"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}