{"id":3879,"date":"2022-08-20T21:42:22","date_gmt":"2022-08-20T16:12:22","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/"},"modified":"2022-08-20T21:42:22","modified_gmt":"2022-08-20T16:12:22","slug":"solved-does-python-have-a-ternary-conditional-operator","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/","title":{"rendered":"(Solved) Does Python have a ternary conditional operator?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-394814\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"394814\" data-parentid=\"394809\" data-score=\"8582\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Yes, it was <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/mail.python.org\/pipermail\/python-dev\/2005-September\/056846.html\" title=\"[Python-Dev] Conditional Expression Resolution\">added<\/a> in version 2.5. The expression syntax is:<\/p>\n<pre class=\"lang-py prettyprint-override\"><code>a if condition else b\n<\/code><\/pre>\n<p>First <code>condition<\/code> is evaluated, then exactly one of either <code>a<\/code> or <code>b<\/code> is evaluated and returned based on the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Boolean_data_type\" title=\"Boolean data type\">Boolean<\/a> value of <code>condition<\/code>. If <code>condition<\/code> evaluates to <code>True<\/code>, then <code>a<\/code> is evaluated and returned but <code>b<\/code> is ignored, or else when <code>b<\/code> is evaluated and returned but <code>a<\/code> is ignored.<\/p>\n<p>This allows short-circuiting because when <code>condition<\/code> is true only <code>a<\/code> is evaluated and <code>b<\/code> is not evaluated at all, but when <code>condition<\/code> is false only <code>b<\/code> is evaluated and <code>a<\/code> is not evaluated at all.<\/p>\n<p>For example:<\/p>\n<pre class=\"lang-py prettyprint-override\"><code>&gt;&gt;&gt; 'true' if True else 'false'\n'true'\n&gt;&gt;&gt; 'true' if False else 'false'\n'false'\n<\/code><\/pre>\n<p>Note that conditionals are an <em>expression<\/em>, not a <em>statement<\/em>. This means you can&#8217;t use assignment statements or <code>pass<\/code> or other <strong>statements<\/strong> within a conditional <strong>expression<\/strong>:<\/p>\n<pre class=\"lang-py prettyprint-override\"><code>&gt;&gt;&gt; pass if False else x = 3\n  File \"&lt;stdin&gt;\", line 1\n    pass if False else x = 3\n          ^\nSyntaxError: invalid syntax\n<\/code><\/pre>\n<p>You can, however, use conditional expressions to assign a variable like so:<\/p>\n<pre class=\"lang-py prettyprint-override\"><code>x = a if True else b\n<\/code><\/pre>\n<p>Think of the conditional expression as switching between two values. It is very useful when you&#8217;re in a &#8216;one value or another&#8217; situation, but it doesn&#8217;t do much else.<\/p>\n<p>If you need to use statements, you have to use a normal <code>if<\/code> <strong>statement<\/strong> instead of a conditional <strong>expression<\/strong>.<\/p>\n<hr>\n<p>Keep in mind that it&#8217;s frowned upon by some Pythonistas for several reasons:<\/p>\n<ul>\n<li>The order of the arguments is different from those of the classic <code>condition ? a : b<\/code> ternary operator from many other languages (such as <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/C_%28programming_language%29\">C<\/a>, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/C%2B%2B\">C++<\/a>, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Go_%28programming_language%29\">Go<\/a>, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Perl\">Perl<\/a>, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Ruby_%28programming_language%29\">Ruby<\/a>, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Java_%28programming_language%29\">Java<\/a>, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/JavaScript\">JavaScript<\/a>, etc.), which may lead to bugs when people unfamiliar with Python&#8217;s &#8220;surprising&#8221; behaviour use it (they may reverse the argument order).<\/li>\n<li>Some find it &#8220;unwieldy&#8221;, since it goes contrary to the normal flow of thought (thinking of the condition first and then the effects).<\/li>\n<li>Stylistic reasons. (Although the &#8216;inline <code>if<\/code>&#8216; can be <em>really<\/em> useful, and make your script more concise, it really does complicate your code)<\/li>\n<\/ul>\n<p>If you&#8217;re having trouble remembering the order, then remember that when read aloud, you (almost) say what you mean. For example, <code>x = 4 if b &gt; 8 else 9<\/code> is read aloud as <code>x will be 4 if b is greater than 8 otherwise 9<\/code>.<\/p>\n<p>Official documentation:<\/p>\n<ul>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.python.org\/3\/reference\/expressions.html#conditional-expressions\" title=\"Conditional expressions\">Conditional expressions<\/a><\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.python.org\/3\/faq\/programming.html#is-there-an-equivalent-of-c-s-ternary-operator\" title=\"Is there an equivalent of C\u2019s \u201d?:\u201d ternary operator?\">Is there an equivalent of C\u2019s \u201d?:\u201d ternary operator?<\/a><\/li>\n<\/ul>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">12<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Does Python have a ternary conditional operator? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Yes, it was added in version 2.5. The expression syntax is: a if condition else b First condition is evaluated, then exactly one of either a or b is evaluated and returned based on the Boolean value of condition. If condition evaluates to True, then a is evaluated and returned but b is ignored, &#8230; <a title=\"(Solved) Does Python have a ternary conditional operator?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/\" aria-label=\"More on (Solved) Does Python have a ternary conditional operator?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[427,369,349],"class_list":["post-3879","post","type-post","status-publish","format-standard","hentry","category-solved","tag-conditional-operator","tag-operators","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>(Solved) Does Python have a ternary conditional operator? - JassWeb<\/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:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"(Solved) Does Python have a ternary conditional operator? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Yes, it was added in version 2.5. The expression syntax is: a if condition else b First condition is evaluated, then exactly one of either a or b is evaluated and returned based on the Boolean value of condition. If condition evaluates to True, then a is evaluated and returned but b is ignored, ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-20T16:12:22+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\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:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"(Solved) Does Python have a ternary conditional operator?\",\"datePublished\":\"2022-08-20T16:12:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/\"},\"wordCount\":331,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"conditional-operator\",\"operators\",\"python\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/\",\"name\":\"(Solved) Does Python have a ternary conditional operator? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-20T16:12:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"(Solved) Does Python have a ternary conditional operator?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"(Solved) Does Python have a ternary conditional operator? - JassWeb","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:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/","og_locale":"en_US","og_type":"article","og_title":"(Solved) Does Python have a ternary conditional operator? - JassWeb","og_description":"[ad_1] Yes, it was added in version 2.5. The expression syntax is: a if condition else b First condition is evaluated, then exactly one of either a or b is evaluated and returned based on the Boolean value of condition. If condition evaluates to True, then a is evaluated and returned but b is ignored, ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/","og_site_name":"JassWeb","article_published_time":"2022-08-20T16:12:22+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"(Solved) Does Python have a ternary conditional operator?","datePublished":"2022-08-20T16:12:22+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/"},"wordCount":331,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["conditional-operator","operators","python"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/","url":"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/","name":"(Solved) Does Python have a ternary conditional operator? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-20T16:12:22+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-does-python-have-a-ternary-conditional-operator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"(Solved) Does Python have a ternary conditional operator?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/3879","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=3879"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/3879\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=3879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=3879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=3879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}