{"id":97,"date":"2019-01-11T02:42:35","date_gmt":"2019-01-11T02:42:35","guid":{"rendered":"https:\/\/kindsonthegenius.com\/python\/?p=97"},"modified":"2019-03-31T21:10:52","modified_gmt":"2019-03-31T21:10:52","slug":"06-python-conditional-statements","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/","title":{"rendered":"Python &#8211; Conditional Statements"},"content":{"rendered":"<p>Conditional statements allows you to make decisions on what part of your program would execute based on certain criteria.<br \/>The criteria is specified in form of an expression that evaluates to either True or False. So you write code that would execute depending on either the result is True or False.<\/p>\r\n<p>The general form of Python conditional statement is given in Figure 1.0.<\/p>\r\n<p>&nbsp;<\/p>\r\n\r\n<div class=\"wp-block-image\">\r\n<figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-137\" src=\"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/01\/Conditional-Statement-in-Python.jpg\" alt=\"\" width=\"270\" height=\"376\" srcset=\"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/01\/Conditional-Statement-in-Python.jpg 449w, https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/01\/Conditional-Statement-in-Python-216x300.jpg 216w\" sizes=\"auto, (max-width: 270px) 100vw, 270px\" \/>\r\n<figcaption>Figure 1.0: Conditional Statement in Python<\/figcaption>\r\n<\/figure>\r\n<\/div>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">By default, non-zero values or non-null values in Python have a value of True while zero or null values have a value of False<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The following three conditional statement construct can be implemented in Python<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>If Statements: <\/strong>This is a statement (condition) followed by a block of one or more statements which are executed if the condition evaluates to true. If the condition if false, the block is ignored and execution continues with statements after the block. Example of If statement is given in the code below.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>a = 20\r\nb = 40\r\nif a == b:\r\n    print(\"a is equal to b\")<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>if &#8230; else Statement:<\/strong> This is like the if statement, but provides an optional else block which must be executed if the condition evaluates to false. This is shown in the code below<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>a = 20\r\nb = 40\r\nif a == b:\r\n    print(\"a is equal to b\")\r\nelse:\r\n    print(\"a is not equal to b\")<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>elif Statement: <\/strong>This is a situation when more than one condition is to be evaluated. In this case, if the first condition evaluates to False, the elif provides a second condition to check. The structure of the <strong>elif <\/strong>statment is given below<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>a = 20\r\nb = 40\r\nif a == b:\r\n    print(\"a is equal to b\")\r\nelif a &gt; b:\r\n    print(\"a is greater than b\")\r\nelse:\r\n    print(\"a is less than b\")<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Nested If Statements: This is a situation where a second condition have to be evaluated if the first condition is True. In this case, the second if statement is nested inside the first if statement. This is shown below<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>a = int(input(\"Please enter a number\"))\r\nif a &lt; 100:\r\n    if &gt; 50:\r\n         print(\"a is less than 100 but greater than 50\")\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Try out the code below the is used to assign a grade A, B, C, D, E to a score. You can also watch the video below to see how it is explained and implemented in PyCharm<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>a = input(\"Enter a number\")\r\na = float(a)\r\nif a&gt;= 70 and a &lt;= 100:\r\n    print(\"A\")\r\nelif a &gt;= 60 and a &lt;=69:\r\n    print(\"B\")\r\nelif a &gt;= 50 and a&lt;=59:\r\n    print(\"C\")\r\nelif a &gt; 45 and a &lt; 59\r\n    print(\"D\")<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Watch the Video<\/strong><\/p>\r\n\r\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/zC7RUwy3-L8\" width=\"100%\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\r\n\r\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">&nbsp;<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Conditional statements allows you to make decisions on what part of your program would execute based on certain criteria.The criteria is specified in form of &hellip; <\/p>\n","protected":false},"author":395,"featured_media":289,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[10],"class_list":["post-97","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-conditional-statements-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python - Conditional Statements - Python Tutorials<\/title>\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.kindsonthegenius.com\/python\/06-python-conditional-statements\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python - Conditional Statements - Python Tutorials\" \/>\n<meta property=\"og:description\" content=\"Conditional statements allows you to make decisions on what part of your program would execute based on certain criteria.The criteria is specified in form of &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-11T02:42:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-31T21:10:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/01\/Conditional-Statement-in-Python-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"449\" \/>\n\t<meta property=\"og:image:height\" content=\"625\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\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.kindsonthegenius.com\\\/python\\\/06-python-conditional-statements\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/06-python-conditional-statements\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Python &#8211; Conditional Statements\",\"datePublished\":\"2019-01-11T02:42:35+00:00\",\"dateModified\":\"2019-03-31T21:10:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/06-python-conditional-statements\\\/\"},\"wordCount\":314,\"commentCount\":1,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/06-python-conditional-statements\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/01\\\/Conditional-Statement-in-Python-1.jpg\",\"keywords\":[\"Conditional Statements in Python\"],\"articleSection\":[\"Python Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/06-python-conditional-statements\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/06-python-conditional-statements\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/06-python-conditional-statements\\\/\",\"name\":\"Python - Conditional Statements - Python Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/06-python-conditional-statements\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/06-python-conditional-statements\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/01\\\/Conditional-Statement-in-Python-1.jpg\",\"datePublished\":\"2019-01-11T02:42:35+00:00\",\"dateModified\":\"2019-03-31T21:10:52+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/06-python-conditional-statements\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/06-python-conditional-statements\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/06-python-conditional-statements\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/01\\\/Conditional-Statement-in-Python-1.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/01\\\/Conditional-Statement-in-Python-1.jpg\",\"width\":449,\"height\":625,\"caption\":\"Conditional Statement in Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/06-python-conditional-statements\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python &#8211; Conditional Statements\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/\",\"name\":\"Python Tutorials\",\"description\":\"Python Tutorial for Programming and Data Science\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python - Conditional Statements - Python Tutorials","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.kindsonthegenius.com\/python\/06-python-conditional-statements\/","og_locale":"en_US","og_type":"article","og_title":"Python - Conditional Statements - Python Tutorials","og_description":"Conditional statements allows you to make decisions on what part of your program would execute based on certain criteria.The criteria is specified in form of &hellip;","og_url":"https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/","og_site_name":"Python Tutorials","article_published_time":"2019-01-11T02:42:35+00:00","article_modified_time":"2019-03-31T21:10:52+00:00","og_image":[{"width":449,"height":625,"url":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/01\/Conditional-Statement-in-Python-1.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Python &#8211; Conditional Statements","datePublished":"2019-01-11T02:42:35+00:00","dateModified":"2019-03-31T21:10:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/"},"wordCount":314,"commentCount":1,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/01\/Conditional-Statement-in-Python-1.jpg","keywords":["Conditional Statements in Python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/","url":"https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/","name":"Python - Conditional Statements - Python Tutorials","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/01\/Conditional-Statement-in-Python-1.jpg","datePublished":"2019-01-11T02:42:35+00:00","dateModified":"2019-03-31T21:10:52+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/01\/Conditional-Statement-in-Python-1.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/01\/Conditional-Statement-in-Python-1.jpg","width":449,"height":625,"caption":"Conditional Statement in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/python\/06-python-conditional-statements\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/python\/"},{"@type":"ListItem","position":2,"name":"Python &#8211; Conditional Statements"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/python\/#website","url":"https:\/\/www.kindsonthegenius.com\/python\/","name":"Python Tutorials","description":"Python Tutorial for Programming and Data Science","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/python\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":270,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/97\/revisions\/270"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media\/289"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}