{"id":16429,"date":"2020-10-22T04:21:41","date_gmt":"2020-10-22T11:21:41","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=16429"},"modified":"2023-12-01T04:03:21","modified_gmt":"2023-12-01T12:03:21","slug":"python-if-else","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-if-else\/","title":{"rendered":"if else Python Statements: A Step-By-Step Guide"},"content":{"rendered":"\n<p><em>An if else Python statement evaluates whether an expressio<\/em><em>n is tr<\/em><em>ue or false. If a conditio<\/em><em>n is tr<\/em><em>ue, the &#8220;if&#8221; statement executes. Otherwise, the &#8220;else&#8221; statement executes. Python if else statements help coders control the flow of their programs.<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>When you\u2019re writing a program, you may want a block of code to run only when a certain condition is met. That\u2019s where conditional statements come in. Conditional statements allow you to control the flow of your program more effectively. <\/p>\n\n\n\n<p>In Python, the <em>if<\/em> and <em>if&#8230;else<\/em> statements are used to perform conditional operations. This tutorial will discuss, with reference to examples, the basics of the <em>if<\/em>, <em>if&#8230;else<\/em>, and <em>elif<\/em> statements in Python. We\u2019ll also discuss how to use nested <em>if<\/em> statements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python if Statement<\/h2>\n\n\n\n<p>A Python if statement evaluates whether a condition is equal to true or false. The statement will execute a block of code if a specified condition is equal to true. Otherwise, the block of code within the if statement is not executed.<\/p>\n\n\n\n<p>Let&#8217;s write a program that prints the price of a sandwich order. The price of a sandwich order should only be displayed if the customer has ordered a ham roll. Our sandwich order is a <a href=\"https:\/\/careerkarma.com\/blog\/python-substring\/\">Python string<\/a>. Here&#8217;s the code for our program:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>sandwich_order = &quot;Ham Roll&quot;\n\nif sandwich_order == &quot;Ham Roll&quot;:\n\tprint(&quot;Price: $1.75&quot;)<\/pre><\/div>\n\n\n\n<p>Our code returns: <code>Price: $1.75<\/code><\/p>\n\n\n\n<p>We have declared a variable called sandwich_order. This variable has been assigned the value <em>Ham Roll<\/em>.\n\n<\/p>\n\n\n\n<p>We use an <em>if<\/em> statement to check whether sandwich_order is equal to <em>Ham Roll<\/em>. If our condition is true, our <a href=\"https:\/\/careerkarma.com\/blog\/python-print-without-new-line\/\"><em>print()<\/em><\/a><a href=\"https:\/\/careerkarma.com\/blog\/python-print-without-new-line\/\"> statement<\/a> is be executed. If our condition is false, nothing will happen.\n\n<\/p>\n\n\n\n<p>Our sandwich_order variable is equal to <em>Ham Roll. <\/em>This means that our <em>if<\/em> statement is executed. Now, let\u2019s see what happens when we change our sandwich order to <em>Cheese Roll<\/em>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>sandwich_order = &quot;Cheese Roll&quot;\n\nif sandwich_order == &quot;Ham Roll&quot;:\n\tprint(&quot;Price: $1.75&quot;)<\/pre><\/div>\n\n\n\n<p>Our code returns nothing. The <em>print()<\/em> statement in our code is not given the chance to execute. This is because our sandwich order is not equal to <em>Ham Roll.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">if else Python Statement<\/h2>\n\n\n\n<p>An if&#8230;else Python statement checks whether a condition is true. If a condition is true, the if statement executes. Otherwise, the else statement executes.<\/p>\n\n\n\n<p>So far, we have used an if statement to test for whether a particular condition is met. But, what if we want to do something if a condition is not met?<\/p>\n\n\n\n<p>Suppose we are building an app that checks whether a customer at a local restaurant has run up a tab. If the customer has run up a tab over $20, they need to pay it off before they can order more food. Nothing should happen if the customer does not have a tab accrued over $20.\n\n<\/p>\n\n\n\n<p>To accomplish this task, we could use the following code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>tab = 29.95\n\nif tab &gt; 20:\n\tprint(&quot;This user has a tab over $20 that needs to be paid.&quot;)\nelse:\n\tprint(&quot;This user's tab is below $20 that does not require immediate payment.&quot;)<\/pre><\/div>\n\n\n\n<p>Our code returns: <em>This user has a tab over $20 that needs to be paid.<\/em>\n\n<\/p>\n\n\n\n<p>Let\u2019s walk through how our code works. First, we declare a <a href=\"https:\/\/careerkarma.com\/blog\/python-variables\/\">Python variable<\/a> called <em>tab. <\/em>This variable tracks a customer\u2019s tab. We use an <em>if<\/em> statement to check whether the customer\u2019s tab is greater than 20.<\/p>\n\n\n\n<p>If a customer&#8217;s tab is worth more than $20, the <em>print()<\/em> statement after our <em>if<\/em> statement is executed. Otherwise, the <em>print()<\/em> statement after our Python if\u2026else clause is executed.\n\n<\/p>\n\n\n\n<p>Because our customer\u2019s tab is over $20, the <a href=\"https:\/\/careerkarma.com\/blog\/python-interpreter\/\">Python interpreter<\/a> executes our if statement. This instructs our program to print a message to the console. The message tells us that the customer must pay their tab.\n\n<\/p>\n\n\n\n<p>Let&#8217;s set the customer&#8217;s tab to $0 and see what happens:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>This user's tab is below $20 which does not require immediate payment.<\/pre><\/div>\n\n\n\n<p>Our code returns a different output. The customer\u2019s tab is not over $20. This means the contents of our <em>else<\/em> statement are executed instead of our <em>if<\/em> statement.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python elif Statement<\/h2>\n\n\n\n<p>A Python elif statement checks for another condition if all preceding conditions are not met. They appear after a Python if statement and before an else statement. You can use as many elif statements as you want.<\/p>\n\n\n\n<p>In our above example, we created a conditional statement with two possible outcomes. If the user\u2019s tab was over $20, a message was printed to the console. If a user\u2019s tab was under $20, a different message was printed to the console.<\/p>\n\n\n\n<p>In some cases, we may want to evaluate multiple conditions and create outcomes for each of those conditions. That\u2019s where the <em>elif<\/em> condition comes in.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">elif Python Statement Example<\/h3>\n\n\n\n<p>Let\u2019s return to our sandwich example from earlier. Suppose we want to have four potential outputs from our program, depending on the sandwich filling a customer chooses. These are:\n\n<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Ham Roll: $1.75<\/li><li>Cheese Roll: $1.80<\/li><li>Bacon Roll: $2.10<\/li><li>Other Filled Roll: $2.00<\/li><\/ul>\n\n\n\n<p>We could use the following code to calculate the cost of the customer\u2019s order:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>sandwich_order = &quot;Bacon Roll&quot;\n\nif sandwich_order == &quot;Ham Roll&quot;:\n\tprint(&quot;Price: $1.75&quot;)\nelif sandwich_order == &quot;Cheese Roll&quot;:\n\tprint(&quot;Price: $1.80&quot;)\nelif sandwich_order == &quot;Bacon Roll&quot;:\n\tprint(&quot;Price: $2.10&quot;)\nelse:\n\tprint(&quot;Price: $2.00&quot;)<\/pre><\/div>\n\n\n\n<p>Our code returns: <code>Price: $2.10<\/code>.<\/p>\n\n\n\n<p>Our code has four possible outcomes:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If a customer orders a ham roll, the contents of the \u201cif\u201d statement are executed. This prints \u201cPrice: $1.75\u201d to the console.<\/li><li>If a customer orders a cheese roll, the contents of the first \u201celif\u201d statement are executed. This prints \u201cPrice: $1.80\u201d to the console.<\/li><li>If a customer orders a bacon roll, the contents of the second \u201celif\u201d statement are run. This prints \u201cPrice: $2.10\u201d to the console.<\/li><li>We display \u201cPrice: $2.10\u201d on the console if a customer orders a roll with a different filling.<\/li><\/ul>\n\n\n\n<p>We used an <em>if<\/em> statement to test for a specific condition. Our two <em>elif<\/em> blocks to test for alternative conditions. The <em>else<\/em> statement returns a value in case no conditions are met.\n\n<\/p>\n\n\n\n<p>We could add in more <em>elif<\/em> statements to our above code if we wanted. If we introduced a new <em>Tuna Roll<\/em> to our sandwich menu, we could add in a new <em>elif<\/em> statement. This new statement could print the price of the new menu item to the console.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Nested if Statements<\/h2>\n\n\n\n<p>A nested if statement is an if statement inside another if statement. Nested if statements let you check if a condition is met after another condition has already been met.<\/p>\n\n\n\n<p>Let\u2019s return to our sandwich example from earlier. Suppose we want to check whether a customer has ordered a roll that is on our menu. We want to do this before we check the prices of the customer\u2019s order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Python Nested if Statement Example<\/h3>\n\n\n\n<p>A message should be printed to the screen with our default price for non-menu items if a customer has ordered another <em>custom<\/em> sandwich. Custom sandwiches are sandwiches that are not on our menu (such as a buttered roll, or a jam roll).<\/p>\n\n\n\n<p>However, if a customer has ordered a sandwich that is on our menu, we should then check to see the price of that sandwich. We could do so using this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>sandwich_order = &quot;Other Filled Roll&quot;\n\nif sandwich_order != &quot;Other Filled Roll&quot;:\n\n\tif sandwich_order == &quot;Ham Roll&quot;:\n\tprint(&quot;Price: $1.75&quot;)\nif sandwich_order == &quot;Cheese Roll&quot;:\n\tprint(&quot;Price: $1.80&quot;)\nelif sandwich_order == &quot;Bacon Roll&quot;:\n\tprint(&quot;Price: $2.10&quot;)\n\nelse:\n\tprint(&quot;Price: $2.00&quot;)<\/pre><\/div>\n\n\n\n<p>Our code returns: <code>Price: $2.00<\/code>. <\/p>\n\n\n\n<p>First, our program evaluates whether our sandwich order is not equal to <em>Other Filled Roll<\/em>. Our program will compare the sandwich we have ordered with the list of sandwiches on our menu. This happens if we have ordered a sandwich on the menu.\n\n<\/p>\n\n\n\n<p>If we have ordered a filled roll that is not on our menu, the contents of the <em>else<\/em> statement in our code are executed.\n\n<\/p>\n\n\n\n<p>In this example, we have ordered a filled roll that is not on our menu. This means that the statement <em>if sandwich_order != Other Filled Roll<\/em> evaluates to False, so the code in our if statement is executed.\n\n<\/p>\n\n\n\n<p>Now, suppose we ordered a ham roll instead. This would cause our first <em>if<\/em> statement to evaluate to true. This is because <em>Ham Roll<\/em> is not equal to <em>Other Filled Roll. <\/em>Our order will be compared to the list of sandwich prices we have specified.<\/p>\n\n\n\n<p><strong>View the Repl.it from this tutorial:<\/strong> <\/p>\n\n\n\n<iframe loading=\"lazy\" src=\"https:\/\/repl.it\/@careerkarma\/Python-if-Statement?lite=true\" width=\"100%\" height=\"400px\" frameborder=\"0\"><\/iframe>\n<br>\n<br>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The if else statement lets you control the flow of your programs. First, Python evaluates if a condition is true. If a condition is not true and an elif statement exists, another condition is evaluated.<\/p>\n\n\n\n<p>If no conditions are met and an else statement is specified, the contents of an else statement are run. By using a conditional statement, you can instruct a program to only execute a block of code when a condition is met.<\/p>\n\n\n\n<p>Now you\u2019re ready to start using these statements in your own code, like a Python expert! To learn more about coding in Python, read our complete guide on <a href=\"https:\/\/careerkarma.com\/blog\/how-to-learn-python\/\">How to Code in Python<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"An if else Python statement evaluates whether an expression is true or false. If a condition is true, the \"if\" statement executes. Otherwise, the \"else\" statement executes. Python if else statements help coders control the flow of their programs. When you\u2019re writing a program, you may want a block of code to run only when&hellip;","protected":false},"author":240,"featured_media":16430,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-16429","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>if else Python Statements: A Step-By-Step Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"The Python if and if else statements execute code depending on an expression. On Career Karma, learn how to use if and if else.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-if-else\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"if else Python Statements: A Step-By-Step Guide\" \/>\n<meta property=\"og:description\" content=\"The Python if and if else statements execute code depending on an expression. On Career Karma, learn how to use if and if else.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-if-else\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-22T11:21:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:03:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/business-chair-coffee-computer-265072.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-if-else\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-if-else\\\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"if else Python Statements: A Step-By-Step Guide\",\"datePublished\":\"2020-10-22T11:21:41+00:00\",\"dateModified\":\"2023-12-01T12:03:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-if-else\\\/\"},\"wordCount\":1384,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-if-else\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/business-chair-coffee-computer-265072.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-if-else\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-if-else\\\/\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-if-else\\\/\",\"name\":\"if else Python Statements: A Step-By-Step Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-if-else\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-if-else\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/business-chair-coffee-computer-265072.jpg\",\"datePublished\":\"2020-10-22T11:21:41+00:00\",\"dateModified\":\"2023-12-01T12:03:21+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"The Python if and if else statements execute code depending on an expression. On Career Karma, learn how to use if and if else.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-if-else\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-if-else\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-if-else\\\/#primaryimage\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/business-chair-coffee-computer-265072.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/business-chair-coffee-computer-265072.jpg\",\"width\":1020,\"height\":680},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python-if-else\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"if else Python Statements: A Step-By-Step Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/author\\\/jamesgallagher\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"if else Python Statements: A Step-By-Step Guide | Career Karma","description":"The Python if and if else statements execute code depending on an expression. On Career Karma, learn how to use if and if else.","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:\/\/careerkarma.com\/blog\/python-if-else\/","og_locale":"en_US","og_type":"article","og_title":"if else Python Statements: A Step-By-Step Guide","og_description":"The Python if and if else statements execute code depending on an expression. On Career Karma, learn how to use if and if else.","og_url":"https:\/\/careerkarma.com\/blog\/python-if-else\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-10-22T11:21:41+00:00","article_modified_time":"2023-12-01T12:03:21+00:00","og_image":[{"width":1020,"height":680,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/business-chair-coffee-computer-265072.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-if-else\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-if-else\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"if else Python Statements: A Step-By-Step Guide","datePublished":"2020-10-22T11:21:41+00:00","dateModified":"2023-12-01T12:03:21+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-if-else\/"},"wordCount":1384,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-if-else\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/business-chair-coffee-computer-265072.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-if-else\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-if-else\/","url":"https:\/\/careerkarma.com\/blog\/python-if-else\/","name":"if else Python Statements: A Step-By-Step Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-if-else\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-if-else\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/business-chair-coffee-computer-265072.jpg","datePublished":"2020-10-22T11:21:41+00:00","dateModified":"2023-12-01T12:03:21+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"The Python if and if else statements execute code depending on an expression. On Career Karma, learn how to use if and if else.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-if-else\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-if-else\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-if-else\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/business-chair-coffee-computer-265072.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/business-chair-coffee-computer-265072.jpg","width":1020,"height":680},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-if-else\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"if else Python Statements: A Step-By-Step Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/16429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=16429"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/16429\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/16430"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=16429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=16429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=16429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}