{"id":87,"date":"2020-10-03T01:22:02","date_gmt":"2020-10-03T01:22:02","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=87"},"modified":"2025-03-26T10:19:10","modified_gmt":"2025-03-26T10:19:10","slug":"python-if","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-if\/","title":{"rendered":"Python if Statement"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Python <code>if<\/code> statement to execute a block of code based on a condition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='the-simple-python-if-statement'>The simple Python if statement <a href=\"#the-simple-python-if-statement\" class=\"anchor\" id=\"the-simple-python-if-statement\" title=\"Anchor for The simple Python if statement\">#<\/a><\/h2>\n\n\n\n<p>You use the <code>if <\/code>statement to execute a block of code based on a specified condition. <\/p>\n\n\n\n<p>The syntax of the <code>if <\/code>statement is as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">if<\/span> condition:\n    <span class=\"hljs-keyword\">if<\/span>-block<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>if<\/code> statement checks the condition first.<\/p>\n\n\n\n<p>If the condition evaluates to <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 <code>condition<\/code> is very important. If you forget it, you&#8217;ll get a syntax error.<\/p>\n\n\n\n<p>The following flowchart illustrates the if statement:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"243\" height=\"540\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-if-Statement.png\" alt=\"\" class=\"wp-image-835\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-if-Statement.png 243w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-if-Statement-135x300.png 135w\" sizes=\"auto, (max-width: 243px) 100vw, 243px\" \/><\/figure>\n<\/div>\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">age = input(<span class=\"hljs-string\">'Enter your age:'<\/span>)\n<span class=\"hljs-keyword\">if<\/span> int(age) &gt;= <span class=\"hljs-number\">18<\/span>:\n    print(<span class=\"hljs-string\">\"You're eligible to vote.\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=YWdlID0gaW5wdXQoJ0VudGVyIHlvdXIgYWdlOicpCmlmIGludChhZ2UpID49IDE4OgogICAgcHJpbnQoIllvdSdyZSBlbGlnaWJsZSB0byB2b3RlLiIp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\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 <code>18<\/code>, it&#8217;ll show a message <code>\"You're eligible to vote\"<\/code> on the screen. Otherwise, it does nothing.<\/p>\n\n\n\n<p>The condition <code>int(age) &gt;= 18<\/code> converts the input string to an integer and compares it with 18.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Enter your age:<span class=\"hljs-number\">18<\/span>\nYo<span class=\"hljs-string\">u're eligible to vote.<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>See the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">age = input(<span class=\"hljs-string\">'Enter your age:'<\/span>)\n<span class=\"hljs-keyword\">if<\/span> int(age) &gt;= <span class=\"hljs-number\">18<\/span>:\n    print(<span class=\"hljs-string\">\"You're eligible to vote.\"<\/span>)\n    print(<span class=\"hljs-string\">\"Let's go and vote.\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=YWdlID0gaW5wdXQoJ0VudGVyIHlvdXIgYWdlOicpCmlmIGludChhZ2UpID49IDE4OgogICAgcHJpbnQoIllvdSdyZSBlbGlnaWJsZSB0byB2b3RlLiIpCiAgICBwcmludCgiTGV0J3MgZ28gYW5kIHZvdGUuIik%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this example, if you enter a number that is greater than or equal to <code>18<\/code>, you&#8217;ll see two messages.<\/p>\n\n\n\n<p>In this example, indentation is very important. Any statement that follows the <code>if<\/code> statement needs to have four spaces.<\/p>\n\n\n\n<p>If you don&#8217;t use the indentation correctly, the program will work differently. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">age = input(<span class=\"hljs-string\">'Enter your age:'<\/span>)\n<span class=\"hljs-keyword\">if<\/span> int(age) &gt;= <span class=\"hljs-number\">18<\/span>:\n    print(<span class=\"hljs-string\">\"You're eligible to vote.\"<\/span>)\nprint(<span class=\"hljs-string\">\"Let's go and vote.\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=YWdlID0gaW5wdXQoJ0VudGVyIHlvdXIgYWdlOicpCmlmIGludChhZ2UpID49IDE4OgogICAgcHJpbnQoIllvdSdyZSBlbGlnaWJsZSB0byB2b3RlLiIpCnByaW50KCJMZXQncyBnbyBhbmQgdm90ZS4iKQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this example, the final statement always executes regardless of the <code>condition<\/code> in the <code>if<\/code> statement. The reason is that it doesn&#8217;t belong to the <code>if<\/code> block:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Enter your age:<span class=\"hljs-number\">11<\/span>\nLet<span class=\"hljs-string\">'s go and vote.<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='python-if-else-statement'>Python if&#8230;else statement <a href=\"#python-if-else-statement\" class=\"anchor\" id=\"python-if-else-statement\" title=\"Anchor for Python if...else statement\">#<\/a><\/h2>\n\n\n\n<p>Typically, you want to perform an action when a condition is <code>True<\/code> and another action when the condition is <code>False<\/code>.<\/p>\n\n\n\n<p>To do so, you use the <code>if...else<\/code> statement. <\/p>\n\n\n\n<p>The following shows the syntax of the <code>if...else<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">if<\/span> condition:\n    <span class=\"hljs-keyword\">if<\/span>-block;\n<span class=\"hljs-keyword\">else<\/span>:\n    <span class=\"hljs-keyword\">else<\/span>-block;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, the <code>if...else<\/code> will execute the <code>if-block<\/code> if the condition evaluates to <code>True<\/code>. Otherwise, it&#8217;ll execute the <code>else-block<\/code>.<\/p>\n\n\n\n<p>The following flowchart illustrates the <code>if..else<\/code> statement:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"376\" height=\"555\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-if-else-statement.png\" alt=\"\" class=\"wp-image-836\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-if-else-statement.png 376w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-if-else-statement-203x300.png 203w\" sizes=\"auto, (max-width: 376px) 100vw, 376px\" \/><\/figure>\n<\/div>\n\n\n<p>The following example illustrates you how to use the&nbsp; <code>if...else<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">age = input(<span class=\"hljs-string\">'Enter your age:'<\/span>)\n<span class=\"hljs-keyword\">if<\/span> int(age) &gt;= <span class=\"hljs-number\">18<\/span>:\n    print(<span class=\"hljs-string\">\"You're eligible to vote.\"<\/span>)\n<span class=\"hljs-keyword\">else<\/span>:\n    print(<span class=\"hljs-string\">\"You're not eligible to vote.\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=YWdlID0gaW5wdXQoJ0VudGVyIHlvdXIgYWdlOicpCmlmIGludChhZ2UpID49IDE4OgogICAgcHJpbnQoIllvdSdyZSBlbGlnaWJsZSB0byB2b3RlLiIpCmVsc2U6CiAgICBwcmludCgiWW91J3JlIG5vdCBlbGlnaWJsZSB0byB2b3RlLiIp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this example, if you enter your age with a number less than 18, you&#8217;ll see the message <code>\"You're not eligible to vote.\"<\/code> like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Enter your age:<span class=\"hljs-number\">11<\/span>\nYo<span class=\"hljs-string\">u're not eligible to vote.<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='python-if-elif-else-statement'>Python if&#8230;elif&#8230;else statement <a href=\"#python-if-elif-else-statement\" class=\"anchor\" id=\"python-if-elif-else-statement\" title=\"Anchor for Python if...elif...else statement\">#<\/a><\/h2>\n\n\n\n<p>If you want to check multiple conditions and perform an action accordingly, you can use the <code>if...elif...else<\/code> statement. The <code>elif<\/code> stands for <code>else if<\/code>.<\/p>\n\n\n\n<p>Here is the syntax if the <code>if...elif...else<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-keyword\">if<\/span>-condition:\n    <span class=\"hljs-keyword\">if<\/span>-block\n<span class=\"hljs-keyword\">elif<\/span> <span class=\"hljs-keyword\">elif<\/span>-condition1:\n    <span class=\"hljs-keyword\">elif<\/span>-block1\n<span class=\"hljs-keyword\">elif<\/span> <span class=\"hljs-keyword\">elif<\/span>-condition2:\n    <span class=\"hljs-keyword\">elif<\/span>-block2\n...\n<span class=\"hljs-keyword\">else<\/span>:\n    <span class=\"hljs-keyword\">else<\/span>-block<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>if...elif...else<\/code> statement checks each condition (<code>if-condition<\/code>, <code>elif-condition1<\/code>, <code>elif-condition2<\/code>, &#8230;) in the order that they appear in the statement until it finds the one that evaluates to <code>True<\/code>.<\/p>\n\n\n\n<p>When the <code>if...elif...else<\/code> 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 <code>True<\/code>, the <code>if...elif...else<\/code> statement executes the statement in the <code>else<\/code> branch.<\/p>\n\n\n\n<p>Note that the <code>else<\/code> block is optional. If you omit it and no condition is <code>True<\/code>, the statement does nothing.<\/p>\n\n\n\n<p>The following flowchart illustrates the <code>if...elif...else<\/code> statement:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"716\" height=\"467\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-if-elif-else-statement.png\" alt=\"\" class=\"wp-image-837\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-if-elif-else-statement.png 716w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/10\/Python-if-elif-else-statement-300x196.png 300w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/figure>\n\n\n\n<p>The following example uses the <code>if...elif..else<\/code> statement to determine the ticket price based on the age:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">age = input(<span class=\"hljs-string\">'Enter your age:'<\/span>)\n\n<span class=\"hljs-comment\"># convert the string to int<\/span>\nyour_age = int(age)\n\n<span class=\"hljs-comment\"># determine the ticket price<\/span>\n<span class=\"hljs-keyword\">if<\/span> your_age &lt; <span class=\"hljs-number\">5<\/span>:\n    ticket_price = <span class=\"hljs-number\">5<\/span>\n<span class=\"hljs-keyword\">elif<\/span> your_age &lt; <span class=\"hljs-number\">16<\/span>:\n    ticket_price = <span class=\"hljs-number\">10<\/span>\n<span class=\"hljs-keyword\">else<\/span>:\n    ticket_price = <span class=\"hljs-number\">18<\/span>\n\n<span class=\"hljs-comment\"># show the ticket price<\/span>\nprint(<span class=\"hljs-string\">f\"You'll pay $<span class=\"hljs-subst\">{ticket_price}<\/span> for the ticket\"<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=YWdlID0gaW5wdXQoJ0VudGVyIHlvdXIgYWdlOicpCgojIGNvbnZlcnQgdGhlIHN0cmluZyB0byBpbnQKeW91cl9hZ2UgPSBpbnQoYWdlKQoKIyBkZXRlcm1pbmUgdGhlIHRpY2tldCBwcmljZQppZiB5b3VyX2FnZSA8IDU6CiAgICB0aWNrZXRfcHJpY2UgPSA1CmVsaWYgeW91cl9hZ2UgPCAxNjoKICAgIHRpY2tldF9wcmljZSA9IDEwCmVsc2U6CiAgICB0aWNrZXRfcHJpY2UgPSAxOAoKIyBzaG93IHRoZSB0aWNrZXQgcHJpY2UKcHJpbnQoZiJZb3UnbGwgcGF5ICR7dGlja2V0X3ByaWNlfSBmb3IgdGhlIHRpY2tldCIp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\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\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>if<\/code> statement when you want to run a code block based on a condition.<\/li>\n\n\n\n<li>Use the <code>if...else<\/code> statement when you want to run another code block if the condition is not <code>True<\/code>.<\/li>\n\n\n\n<li>Use the <code>if...elif...else<\/code> statement when you want to check multiple conditions and run the corresponding code block that follows the condition that evaluates to <code>True<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=if\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"87\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-if\/\"\n\t\t\t\tdata-post-title=\"Python if Statement\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"87\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-if\/\"\n\t\t\t\tdata-post-title=\"Python if Statement\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to use the Python if statement to execute a block of code based on a condition. The simple Python if statement # You use the if statement to execute a block of code based on a specified condition. The syntax of the if statement is as follows: The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":12,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-87","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/87","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/87\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}