{"id":2594,"date":"2022-02-27T11:54:37","date_gmt":"2022-02-27T06:24:37","guid":{"rendered":"https:\/\/nolowiz.com\/?p=2594"},"modified":"2022-02-28T09:52:45","modified_gmt":"2022-02-28T04:22:45","slug":"python-single-line-if","status":"publish","type":"post","link":"https:\/\/nolowiz.com\/python-single-line-if\/","title":{"rendered":"Python Single line If"},"content":{"rendered":"\n<p>If statements are important in any programming language. Python provides if statements, in this article we  will discuss single line if statements.<\/p>\n\n\n\n<p>Let&#8217;s first understand the syntax of single line if statement in python. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>some_expression if condition else other_expression<\/code><\/pre>\n\n\n\n<p>Example <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntotal_mark = 70\n\n# One line if-else statement\nresult = &quot;Passed&quot; if total_mark &gt; 60 else &quot;Failed&quot;\n\nprint(result)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Passed<\/code><\/pre>\n\n\n\n<p>Singe line statements reduces number of lines but still maintains code quality, but don&#8217;t over use it; long and complex statements can reduce code readability.<\/p>\n\n\n\n<h2>One line if statements in Python<\/h2>\n\n\n\n<p>Python provides <a href=\"https:\/\/docs.python.org\/3\/reference\/expressions.html#conditional-expressions\" target=\"_blank\" rel=\"noreferrer noopener\">conditional expressions <\/a>. Sometimes it is also known as ternary operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;on_true] if &#91;expression] else &#91;on_false] <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>x if Condition else y<\/code><\/pre>\n\n\n\n<p>In one line if statement first the &#8220;Condition&#8221; is evaluated,<\/p>\n\n\n\n<ul><li>If it is true then x is evaluated and its value is returned<\/li><li>If it is false then y is evaluated and its value is returned<\/li><\/ul>\n\n\n\n<p>Let&#8217;s understand with an example.<\/p>\n\n\n\n<p>In the following case we need to check student mark if it is greater than 60 then student passed.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntotal_mark = 70\nresult = None\n\nif total_mark &gt; 60:\n  result = &quot;Passed&quot;\nelse:\n  result = &quot;Failed&quot;\n  \nprint(result)\n<\/pre><\/div>\n\n\n<p>Instead of this long if else statements we can use single line if statement as shown below<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntotal_mark = 70\nresult = &quot;Passed&quot; if total_mark &gt; 60 else &quot;Failed&quot;\nprint(result)\n<\/pre><\/div>\n\n\n<p>See the difference, the number of lines of code is reduced and code is readable.<\/p>\n\n\n\n<p>As a word or precaution don&#8217;t over use one line if statements.<\/p>\n\n\n\n<h2>When to use one line if statement?<\/h2>\n\n\n\n<p>One line if statements best suited for when an if else returns boolean value. For example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nis_high = False\ntemperature = 50\n\nif temperature &gt; 100:\n  is_high = True\nelse:\n  is_high = False\n\n  print(is_high)\n<\/pre><\/div>\n\n\n<p>We can use one line as shown below<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntemperature = 120\n\nis_high = True if temperature &gt; 100 else False\n\nprint(is_high)\n<\/pre><\/div>\n\n\n<p>As we can see it is only three lines of code to check to do the operation.<\/p>\n\n\n\n<h2>Why one line if-elif-else is not supported?<\/h2>\n\n\n\n<p>Fitting everything in a single line is not a good practice. As per <a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0008\/\" target=\"_blank\" rel=\"noreferrer noopener\">PEP 8<\/a> standard, a python code line should not exceed 80 characters,this is why Python does not support the if-elif-else statements as one-liner expressions.<\/p>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p>In conclusion, we can turn Python if else statement to ternary operator\/conditional expression and it reduces the number of lines of code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If statements are important in any programming language. Python provides if statements, in this article we will discuss single line if statements. Let&#8217;s first understand the syntax of single line if statement in python. Example Output Singe line statements reduces number of lines but still maintains code quality, but don&#8217;t over use it; long and &#8230; <a title=\"Python Single line If\" class=\"read-more\" href=\"https:\/\/nolowiz.com\/python-single-line-if\/\" aria-label=\"More on Python Single line If\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[25],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Single line If - NoloWiz<\/title>\n<meta name=\"description\" content=\"Python provides if statements in this article we will discuss the usage of single line if statements in programming.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nolowiz.com\/python-single-line-if\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Single line If - NoloWiz\" \/>\n<meta property=\"og:description\" content=\"Python provides if statements in this article we will discuss the usage of single line if statements in programming.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nolowiz.com\/python-single-line-if\/\" \/>\n<meta property=\"og:site_name\" content=\"NoloWiz\" \/>\n<meta property=\"article:published_time\" content=\"2022-02-27T06:24:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-02-28T04:22:45+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rupesh Sreeraman\" \/>\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\":\"Organization\",\"@id\":\"https:\/\/nolowiz.com\/#organization\",\"name\":\"NoloWiz\",\"url\":\"https:\/\/nolowiz.com\/\",\"sameAs\":[],\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/nolowiz.com\/#logo\",\"inLanguage\":\"en\",\"url\":\"https:\/\/nolowiz.com\/wp-content\/uploads\/2021\/01\/cropped-android-chrome-512x512-2.png\",\"contentUrl\":\"https:\/\/nolowiz.com\/wp-content\/uploads\/2021\/01\/cropped-android-chrome-512x512-2.png\",\"width\":512,\"height\":512,\"caption\":\"NoloWiz\"},\"image\":{\"@id\":\"https:\/\/nolowiz.com\/#logo\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/nolowiz.com\/#website\",\"url\":\"https:\/\/nolowiz.com\/\",\"name\":\"NoloWiz\",\"description\":\"Technology news, tips and tutorials\",\"publisher\":{\"@id\":\"https:\/\/nolowiz.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/nolowiz.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/nolowiz.com\/python-single-line-if\/#webpage\",\"url\":\"https:\/\/nolowiz.com\/python-single-line-if\/\",\"name\":\"Python Single line If - NoloWiz\",\"isPartOf\":{\"@id\":\"https:\/\/nolowiz.com\/#website\"},\"datePublished\":\"2022-02-27T06:24:37+00:00\",\"dateModified\":\"2022-02-28T04:22:45+00:00\",\"description\":\"Python provides if statements in this article we will discuss the usage of single line if statements in programming.\",\"breadcrumb\":{\"@id\":\"https:\/\/nolowiz.com\/python-single-line-if\/#breadcrumb\"},\"inLanguage\":\"en\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/nolowiz.com\/python-single-line-if\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/nolowiz.com\/python-single-line-if\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/nolowiz.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Single line If\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/nolowiz.com\/python-single-line-if\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/nolowiz.com\/python-single-line-if\/#webpage\"},\"author\":{\"@id\":\"https:\/\/nolowiz.com\/#\/schema\/person\/6ef6f57a69193ce0993d74a8b6ac4414\"},\"headline\":\"Python Single line If\",\"datePublished\":\"2022-02-27T06:24:37+00:00\",\"dateModified\":\"2022-02-28T04:22:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/nolowiz.com\/python-single-line-if\/#webpage\"},\"wordCount\":300,\"publisher\":{\"@id\":\"https:\/\/nolowiz.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/nolowiz.com\/#\/schema\/person\/6ef6f57a69193ce0993d74a8b6ac4414\",\"name\":\"Rupesh Sreeraman\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/nolowiz.com\/#personlogo\",\"inLanguage\":\"en\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b6c0696ed1695a540102a80daa94ad0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b6c0696ed1695a540102a80daa94ad0?s=96&d=mm&r=g\",\"caption\":\"Rupesh Sreeraman\"},\"sameAs\":[\"http:\/\/nolowiz.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Single line If - NoloWiz","description":"Python provides if statements in this article we will discuss the usage of single line if statements in programming.","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:\/\/nolowiz.com\/python-single-line-if\/","og_locale":"en_US","og_type":"article","og_title":"Python Single line If - NoloWiz","og_description":"Python provides if statements in this article we will discuss the usage of single line if statements in programming.","og_url":"https:\/\/nolowiz.com\/python-single-line-if\/","og_site_name":"NoloWiz","article_published_time":"2022-02-27T06:24:37+00:00","article_modified_time":"2022-02-28T04:22:45+00:00","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rupesh Sreeraman","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/nolowiz.com\/#organization","name":"NoloWiz","url":"https:\/\/nolowiz.com\/","sameAs":[],"logo":{"@type":"ImageObject","@id":"https:\/\/nolowiz.com\/#logo","inLanguage":"en","url":"https:\/\/nolowiz.com\/wp-content\/uploads\/2021\/01\/cropped-android-chrome-512x512-2.png","contentUrl":"https:\/\/nolowiz.com\/wp-content\/uploads\/2021\/01\/cropped-android-chrome-512x512-2.png","width":512,"height":512,"caption":"NoloWiz"},"image":{"@id":"https:\/\/nolowiz.com\/#logo"}},{"@type":"WebSite","@id":"https:\/\/nolowiz.com\/#website","url":"https:\/\/nolowiz.com\/","name":"NoloWiz","description":"Technology news, tips and tutorials","publisher":{"@id":"https:\/\/nolowiz.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nolowiz.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en"},{"@type":"WebPage","@id":"https:\/\/nolowiz.com\/python-single-line-if\/#webpage","url":"https:\/\/nolowiz.com\/python-single-line-if\/","name":"Python Single line If - NoloWiz","isPartOf":{"@id":"https:\/\/nolowiz.com\/#website"},"datePublished":"2022-02-27T06:24:37+00:00","dateModified":"2022-02-28T04:22:45+00:00","description":"Python provides if statements in this article we will discuss the usage of single line if statements in programming.","breadcrumb":{"@id":"https:\/\/nolowiz.com\/python-single-line-if\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nolowiz.com\/python-single-line-if\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nolowiz.com\/python-single-line-if\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nolowiz.com\/"},{"@type":"ListItem","position":2,"name":"Python Single line If"}]},{"@type":"Article","@id":"https:\/\/nolowiz.com\/python-single-line-if\/#article","isPartOf":{"@id":"https:\/\/nolowiz.com\/python-single-line-if\/#webpage"},"author":{"@id":"https:\/\/nolowiz.com\/#\/schema\/person\/6ef6f57a69193ce0993d74a8b6ac4414"},"headline":"Python Single line If","datePublished":"2022-02-27T06:24:37+00:00","dateModified":"2022-02-28T04:22:45+00:00","mainEntityOfPage":{"@id":"https:\/\/nolowiz.com\/python-single-line-if\/#webpage"},"wordCount":300,"publisher":{"@id":"https:\/\/nolowiz.com\/#organization"},"articleSection":["Python"],"inLanguage":"en"},{"@type":"Person","@id":"https:\/\/nolowiz.com\/#\/schema\/person\/6ef6f57a69193ce0993d74a8b6ac4414","name":"Rupesh Sreeraman","image":{"@type":"ImageObject","@id":"https:\/\/nolowiz.com\/#personlogo","inLanguage":"en","url":"https:\/\/secure.gravatar.com\/avatar\/0b6c0696ed1695a540102a80daa94ad0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b6c0696ed1695a540102a80daa94ad0?s=96&d=mm&r=g","caption":"Rupesh Sreeraman"},"sameAs":["http:\/\/nolowiz.com"]}]}},"_links":{"self":[{"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/posts\/2594"}],"collection":[{"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/comments?post=2594"}],"version-history":[{"count":29,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/posts\/2594\/revisions"}],"predecessor-version":[{"id":2623,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/posts\/2594\/revisions\/2623"}],"wp:attachment":[{"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/media?parent=2594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/categories?post=2594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nolowiz.com\/wp-json\/wp\/v2\/tags?post=2594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}