{"id":381,"date":"2024-10-19T01:49:18","date_gmt":"2024-10-19T01:49:18","guid":{"rendered":"https:\/\/itsmycode.com\/?p=381"},"modified":"2024-10-19T01:49:19","modified_gmt":"2024-10-19T01:49:19","slug":"python-ternary-operator","status":"publish","type":"post","link":"https:\/\/itsmycode.com\/python-ternary-operator\/","title":{"rendered":"Python Ternary Operator"},"content":{"rendered":"\n<p>Python Ternary operators also called conditional expressions, are operators that evaluate something based on a binary condition. Ternary operators provide a shorthand way to write conditional statements, which makes the code more concise.<\/p>\n\n\n\n<p>In this tutorial, let us look at what is the ternary operator in Python and how we can use it in our code with some examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax-of-ternary-operator\">Syntax of Ternary Operator<\/h2>\n\n\n\n<p>The ternary operator is available from <strong>Python<\/strong> onwards, and the syntax is:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[value_if_true] if [expression] else [value_if_false]&nbsp;<\/pre>\n\n\n\n<p>The simpler way is to represent ternary operator Syntax is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>&lt;expression 1&gt;<\/em>&nbsp;<strong>if<\/strong>&nbsp;<em>&lt;condition&gt;<\/em>&nbsp;<strong>else<\/strong>&nbsp;<em>&lt;expression 2&gt;<\/em>&nbsp;<\/pre>\n\n\n\n<p><strong>Note:&nbsp;<\/strong>The conditionals are an&nbsp;<em>expression<\/em>, not a&nbsp;<em>statement<\/em>. It means that you can\u2019t use assignment statements or pass other&nbsp;<strong>statements<\/strong>&nbsp;within a conditional&nbsp;<strong>expression.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"introduction-to-python-ternary-operators\">Introduction to Python Ternary Operators<\/h2>\n\n\n\n<p>Let us take a simple example to check if the student\u2019s result is pass or fail based on the marks obtained.&nbsp;<\/p>\n\n\n\n<p><strong>Using a traditional approach<\/strong><\/p>\n\n\n\n<p>We are familiar with \u201c<strong><code>if-else<\/code><\/strong>\u201d conditions lets first write a program that prompts to enter student marks and returns either pass or fail based on the condition specified.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>marks = input('Enter the marks: ')\n\nif int(marks) &gt;= 35:\n    print(\"The result is Pass\")\nelse:\n    print(\"The result is Fail\")\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the marks: 55\nThe result is Pass<\/code><\/pre>\n\n\n\n<p>Now instead of using a typical <strong><code>if-else<\/code><\/strong> condition, let us try using ternary operators.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-of-ternary-operator-in-python\">Example of Ternary Operator in Python<\/h2>\n\n\n\n<p>The ternary operator evaluates the condition first. If the result is true, it returns the <em>value_if_true<\/em>. Otherwise, it returns <em>value_if_false<\/em><\/p>\n\n\n\n<p>The ternary operator is equivalent to the <code>if-else<\/code> condition.&nbsp;<\/p>\n\n\n\n<p>If you are from a programming background like C#, Java, etc., the ternary syntax looks like below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if condition:\n    value_if_true\nelse:\n    value_if_true<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>condition ? value_if_true : value_if_false<\/code><\/pre>\n\n\n\n<p>However, In Python, the syntax of the ternary operator is slightly different. The following example demonstrates how we can use the ternary operator in Python.<\/p>\n\n\n\n<p><strong>Program to demonstrate ternary operators in Python<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>marks = input('Enter the marks: ')\nprint(\"The result is Pass\" if int(marks)&gt;=35 else \"The result is Fail\")<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the marks: 34\nThe result is Fail<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python Ternary operators also called conditional expressions, are operators that evaluate something based on a binary condition. Ternary operators provide a shorthand way to write conditional statements, which makes the code more concise. In this tutorial, let us look at what is the ternary operator in Python and how we can use it in our [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":382,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,1],"tags":[],"class_list":["post-381","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-basics","category-python"],"_links":{"self":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/comments?post=381"}],"version-history":[{"count":1,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/381\/revisions"}],"predecessor-version":[{"id":383,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/381\/revisions\/383"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media\/382"}],"wp:attachment":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media?parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/categories?post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/tags?post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}