{"id":5833,"date":"2024-12-12T10:03:24","date_gmt":"2024-12-12T10:03:24","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=5833"},"modified":"2026-02-02T07:06:02","modified_gmt":"2026-02-02T07:06:02","slug":"does-python-have-ternary-operator","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/","title":{"rendered":"Does Python Have Ternary Operator?"},"content":{"rendered":"\n<p>Python developers commonly need to evaluate conditional expressions and take action based on the result, in their code. They generally use if-else statements for this purpose. But sometimes, you may need to concisely evaluate a condition and return value, all using a single line of code. In such cases, you can use ternary operator in Python. In this article, we will learn in detail about ternary operator in Python.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#Ternary_Operator_in_Python\" >Ternary Operator in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#Nested_Ternary_Operator_in_Python\" >Nested Ternary Operator in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#Ternary_Operator_with_Python_Tuple\" >Ternary Operator with Python Tuple<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#Ternary_Operator_with_Python_Dictionary\" >Ternary Operator with Python Dictionary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#Ternary_Operator_with_Python_Lambda\" >Ternary Operator with Python Lambda<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#Disadvantages_of_Python_Ternary_Operator\" >Disadvantages of Python Ternary Operator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#FAQ\" >FAQ<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Ternary_Operator_in_Python\"><\/span>Ternary Operator in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A ternary operator in Python is a one line statement that allows you to evaluate a condition and assign value depending on whether the condition is true or false. It offers a shorter way to write an if-else statement. Here is an example of an if-else statement.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = 10<br>if(a&gt;5){<br>  print('a&gt;5')<br>}else{<br>  print('a&lt;=5')<br>}<\/pre>\n\n\n\n<p>In the above code, we basically check if the value of variable a is greater than 5 or not, and accordingly display an output message.<\/p>\n\n\n\n<p>Even though our if-else condition is simple, the above code is verbose. You can easily shorten it using ternary operator. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">true_value if condition else false_value<\/pre>\n\n\n\n<p>In the above statement, the condition will be first evaluated. If it is true, then true_value will be evaluated and returned, and false_value is ignored. Else false_value will be evaluated and returned, and true_value is ignored.<\/p>\n\n\n\n<p>It is faster than conventional if else because when condition is true, only true_value is evaluated and false_value is not evaluated at all. On the other hand, when condition is false, false_value is evaluated and true_value is not evaluated at all. Here are a couple of examples to demonstrate its use.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; 'true' if True else 'false'<br>'true'<br>&gt;&gt;&gt; 'true' if False else 'false'<br>'false'<\/pre>\n\n\n\n<p>There are some important points to keep in mind while using ternary operators.<\/p>\n\n\n\n<p>1. The true_value and false_value need to be expressions and not statements. So you cannot use commands like pass, or assignments or even augmented assignments such as += in them. In all these cases, it will throw an error. Here are some examples to illustrate it. Even using parentheses around statements do not work.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; pass if true else pass<br>  File \"&lt;stdin&gt;\", line 1<br>    pass if true else pass<br>          ^<br>SyntaxError: invalid syntax<br><br>&gt;&gt;&gt; a=1 if true else a=2<br>  File \"&lt;stdin&gt;\", line 1<br>SyntaxError: can't assign to conditional expression<br><br>&gt;&gt;&gt; (a=1) if true else (a=2)<br>  File \"&lt;stdin&gt;\", line 1<br>    (a=1) if true else (a=2)<br>      ^<br>SyntaxError: invalid syntax<\/pre>\n\n\n\n<p>Since Python 3.8+, you can also use walrus operator &#8216;:=&#8217; to make simple assignments as expressions. But it can make the code very complicated. So use it only if you have to.<\/p>\n\n\n\n<p>2. In a typical if-else statement, the else part is optional. But in case of ternary operator, the else part is mandatory. If you do not include it, you will get an error message as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; a=1<br>&gt;&gt;&gt; a if True<br>  File \"&lt;stdin&gt;\", line 1<br>    a if True<\/pre>\n\n\n\n<p>3. Another point to keep in mind is that you can use ternary operator to assign variable values. Here is an example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; a=1<br>&gt;&gt;&gt; b=2<br>&gt;&gt;&gt; x=a if True else b<br>&gt;&gt;&gt; print(x)<br>1<\/pre>\n\n\n\n<p>In the above code, the result of ternary operator is assigned to variable x.<\/p>\n\n\n\n<p>4. Since you can use it for variable assignment, you can also use ternary operator to return values from functions, as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; x=1<br>&gt;&gt;&gt; y=2<br>&gt;&gt;&gt; def check(x,y):<br>...     return x if x&gt;y else x<br>...<br>&gt;&gt;&gt; check(a,b)<br>2<\/pre>\n\n\n\n<p>Now let us look at some other ways to use ternary operators in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Nested_Ternary_Operator_in_Python\"><\/span>Nested Ternary Operator in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>By default, the ternary operator can be difficult to use for complex use cases. To overcome this limitation, you can also use ternary operator in a nested manner, one inside the other. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">true_value1 if condition1 else (true_value2 if condition2 else false_value)<\/pre>\n\n\n\n<p>Here is an example to illustrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; x=1; y=2<br>&gt;&gt;&gt; print(\"Both are equal\" if x == y else \"x &gt; y\" if x&gt;y else \"x &lt; y\")<br>x &gt; y<\/pre>\n\n\n\n<p>In the above code, the outer ternary operator checks if x and y are equal. If yes, it will display &#8216;the corresponding &#8216;Both are equal&#8217; message. Else the expression for its false_value checks if x&gt;y. If this is true, then it displays &#8216;x&gt;y&#8217;. Else it will display &#8216;x&lt;y&#8217;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Ternary_Operator_with_Python_Tuple\"><\/span>Ternary Operator with Python Tuple<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also use Ternary operator with 2-item Python tuple, where the first item is false_value and the 2nd item is true_value. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(false_value, true_value) [condition]<\/pre>\n\n\n\n<p>Here is an example to illustrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; x=1;y=2<br>&gt;&gt;&gt; ('x&gt;y','x&lt;=y')[x&gt;y]<br>'x&gt;y'<\/pre>\n\n\n\n<p>In the above example, our condition to be evaluated is x&gt;y. Since x is not greater than y, this evaluates to false. In our tuple, the first value, that is &#8216;x&gt;y&#8217;, is the false value. This is displayed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Ternary_Operator_with_Python_Dictionary\"><\/span>Ternary Operator with Python Dictionary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Similarly, you can also use ternary operator with a Python dictionary containing 2 key-value pairs. In this case, one key needs to be True and its value needs to be the true value. The other key needs to be False, and its value should be the false value. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{True: true_value, False: false_value} [condition]<\/pre>\n\n\n\n<p>Here is an example to illustrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; x=1;y=2<br>&gt;&gt;&gt; {True:'x&gt;y', False:'x&lt;=y'}[x&gt;y]<br>'x&lt;=y'<\/pre>\n\n\n\n<p>In the above example, we have a dictionary with true value as &#8216;x&gt;y&#8217; and false value as &#8216;x&lt;=y&#8217;. Our condition is x&gt;y. Since this is not true, it displays &#8216;x&lt;=y&#8217;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Ternary_Operator_with_Python_Lambda\"><\/span>Ternary Operator with Python Lambda<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also use lambda with ternary operator just as we used tuple with ternary operator. The first item of tuple needs to be false value and the second item needs to be true value. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(lambda: false_value, lambda: true_value) [condition] ()<\/pre>\n\n\n\n<p>Here is an example to show how it works.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; x=1;y=2<br>&gt;&gt;&gt; (lambda: 'x&lt;=y', lambda: 'x&gt;y') [x&gt;y] ()<br>'x&lt;=y'<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Disadvantages_of_Python_Ternary_Operator\"><\/span>Disadvantages of Python Ternary Operator<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Difficult to read<\/strong> &#8211; Since ternary operator is so concise, it can be difficult to understand for many Python developers. This is true, especially if your true\/false expressions are complex.<\/li>\n\n\n\n<li><strong>Error Prone<\/strong> &#8211; Since ternary operator tries to fit a lot of things in a single line, they are error prone. You can easily make a syntax error by incorrect order of expressions, missing colons and parentheses. This makes it even harder to debug.<\/li>\n\n\n\n<li><strong>Non-standard<\/strong> &#8211; Generally, ternary operators have the format condition ? true_value : false_value in most popular languages such as Java, C, C++, JavaScript, Ruby, Perl. But the ternary operator in Python has a different syntax. So it can be difficult for developers who are new to Python.<\/li>\n\n\n\n<li><strong>Cannot Use Statements<\/strong> &#8211; Typical if-else statements allow you to use multiple statements within if\/else blocks. Ternary operator requires you to use only expressions in if\/else block. This is a major limitation.<\/li>\n\n\n\n<li><strong>Difficult for Complex Use Cases<\/strong> &#8211; Due to the previous limitation, ternary operators are suitable for simple use cases only.<\/li>\n\n\n\n<li><strong>Too many variations<\/strong> &#8211; As we saw above, there are many different ways to use ternary operator. It leads to a non-standard syntax, making it difficult for developers to understand other people&#8217;s code and maintain it.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt what are ternary operators in Python. We have also learnt how to use them in different ways. We also looked some of its limitations. Ternary operators are great if you want to shorten simple if-else statements, or if you want to assign result of if-else statement to a variable, or even return that value from a function. But if you want to execute statements in your if\/else blocks, then you need to use the conventional if-else statement. Also, since there are so many different ways to use ternary operator in Python, it can confuse developers easily, and make your code difficult to maintain. Nevertheless, if it fits your use case, then ternary operator can be a good addition to your code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FAQ\"><\/span>FAQ<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>1. What is a Ternary Operator?<\/strong><\/p>\n\n\n\n<p>Ternary operator is a single line conditional expression that allows you to assign value based on result of condition. Its syntax is &#8216;true_value if condition else false_value&#8217;.<\/p>\n\n\n\n<p><strong>2. How to Use Ternary Operator in Python?<\/strong><\/p>\n\n\n\n<p>You can use ternary operator to evaluate condition and based on its result evaluate another expression in case the result is true. Else evaluate an expression if its result is false.<\/p>\n\n\n\n<p><strong>3. When to use Ternary Operator?<\/strong><\/p>\n\n\n\n<p>You can use ternary operator, if you want to implement a simple if-else statement with easy to understand condition and expressions. You can also use it if you want to easily assign a value or return a value as a result of if-else statement. You can also use it if your true and false expressions are one-liners and not multiple statements.<\/p>\n\n\n\n<p><strong>4. When not to use Ternary Operators?<\/strong><\/p>\n\n\n\n<p>If you want to implement complex if-else statements, or if you need to run multiple statements in if\/else blocks, then ternary operators will not be suitable. In fact, if you need to run statements instead of expressions in true_value\/false_value then also ternary operator will not work.<\/p>\n\n\n\n<p><strong>5. How to read ternary operator statements in Python?<\/strong><\/p>\n\n\n\n<p>If you encounter a ternary operator as &#8216;x = 1 if b &gt; 2 else 3&#8217; then you can read it as &#8216;x will be 1 if b is greater than 2 otherwise 3&#8217;.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-read-large-file-in-python\/\">How to Read Large File in Python<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-execute-system-command-in-python\/\">How to Execute System Command in Python<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/\">How to Check If File Exists in Python<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Software developers wonder whether Python has ternary operator. Let us learn more about ternary operator in Python.<\/p>\n","protected":false},"author":1,"featured_media":5872,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301],"tags":[349],"class_list":["post-5833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-ternary-operator"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Does Python Have Ternary Operator? - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Software developers wonder whether Python has ternary operator. Let us learn more about ternary operator in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Does Python Have Ternary Operator? - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Software developers wonder whether Python has ternary operator. Let us learn more about ternary operator in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-12T10:03:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-02-02T07:06:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/ternery-operator-in-python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/does-python-have-ternary-operator\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/does-python-have-ternary-operator\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"Does Python Have Ternary Operator?\",\"datePublished\":\"2024-12-12T10:03:24+00:00\",\"dateModified\":\"2026-02-02T07:06:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/does-python-have-ternary-operator\\\/\"},\"wordCount\":1415,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/does-python-have-ternary-operator\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/ternery-operator-in-python.jpg?fit=300%2C200&ssl=1\",\"keywords\":[\"ternary operator\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/does-python-have-ternary-operator\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/does-python-have-ternary-operator\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/does-python-have-ternary-operator\\\/\",\"name\":\"Does Python Have Ternary Operator? - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/does-python-have-ternary-operator\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/does-python-have-ternary-operator\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/ternery-operator-in-python.jpg?fit=300%2C200&ssl=1\",\"datePublished\":\"2024-12-12T10:03:24+00:00\",\"dateModified\":\"2026-02-02T07:06:02+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Software developers wonder whether Python has ternary operator. Let us learn more about ternary operator in Python.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/does-python-have-ternary-operator\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/does-python-have-ternary-operator\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/does-python-have-ternary-operator\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/ternery-operator-in-python.jpg?fit=300%2C200&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/ternery-operator-in-python.jpg?fit=300%2C200&ssl=1\",\"width\":300,\"height\":200,\"caption\":\"ternery operator in python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/does-python-have-ternary-operator\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Does Python Have Ternary Operator?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Does Python Have Ternary Operator? - Ubiq BI","description":"Software developers wonder whether Python has ternary operator. Let us learn more about ternary operator in Python.","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:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/","og_locale":"en_US","og_type":"article","og_title":"Does Python Have Ternary Operator? - Ubiq BI","og_description":"Software developers wonder whether Python has ternary operator. Let us learn more about ternary operator in Python.","og_url":"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2024-12-12T10:03:24+00:00","article_modified_time":"2026-02-02T07:06:02+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/ternery-operator-in-python.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"Does Python Have Ternary Operator?","datePublished":"2024-12-12T10:03:24+00:00","dateModified":"2026-02-02T07:06:02+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/"},"wordCount":1415,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/ternery-operator-in-python.jpg?fit=300%2C200&ssl=1","keywords":["ternary operator"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/","url":"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/","name":"Does Python Have Ternary Operator? - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/ternery-operator-in-python.jpg?fit=300%2C200&ssl=1","datePublished":"2024-12-12T10:03:24+00:00","dateModified":"2026-02-02T07:06:02+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Software developers wonder whether Python has ternary operator. Let us learn more about ternary operator in Python.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/ternery-operator-in-python.jpg?fit=300%2C200&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/ternery-operator-in-python.jpg?fit=300%2C200&ssl=1","width":300,"height":200,"caption":"ternery operator in python"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"Does Python Have Ternary Operator?"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/ternery-operator-in-python.jpg?fit=300%2C200&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1w5","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=5833"}],"version-history":[{"count":46,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5833\/revisions"}],"predecessor-version":[{"id":10496,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5833\/revisions\/10496"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/5872"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=5833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=5833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=5833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}