{"id":35,"date":"2024-01-24T13:49:14","date_gmt":"2024-01-24T13:49:14","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=35"},"modified":"2024-01-24T13:49:15","modified_gmt":"2024-01-24T13:49:15","slug":"python-ternary-operator","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-ternary-operator\/","title":{"rendered":"Python Ternary Operator"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the Python ternary operator and how to use it to make your code more concise.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python Ternary Operator<\/h2>\n\n\n\n<p>The following program prompts you for your age and determines the ticket price based on it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>age = input('Enter your age:') if int(age) >= 18: ticket_price = 20 else: ticket_price = 5 print(f\"The ticket price is {ticket_price}\")<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Here is the output when you enter 18:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Enter your age:18 The ticket price is $20<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the following\u00a0<code>if...else<\/code>\u00a0statement assigns 20 to the\u00a0<code>ticket_price<\/code>\u00a0if the\u00a0<code>age<\/code>\u00a0is greater than or equal to 18. Otherwise, it assigns the\u00a0<code>ticket_price<\/code>\u00a05:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>if int(age) >= 18: ticket_price = 20 else: ticket_price = 5<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>To make it more concise, you can use an alternative syntax like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>ticket_price = 20 if int(age) >= 18 else 5<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this statement, the left side of the assignment operator (<code>=<\/code>) is the variable&nbsp;<code>ticket_price<\/code>.<\/p>\n\n\n\n<p>The expression on the right side returns 20 if the&nbsp;<code>age<\/code>&nbsp;is greater than or equal to&nbsp;<code>18<\/code>&nbsp;or&nbsp;<code>5<\/code>&nbsp;otherwise.<\/p>\n\n\n\n<p>The following syntax is called a\u00a0ternary operator\u00a0in Python:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>value_if_true if condition else value_if_false<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The ternary operator evaluates the&nbsp;<code>condition<\/code>. If the result is&nbsp;<code>True<\/code>, it returns the&nbsp;<code>value_if_true<\/code>. Otherwise, it returns the&nbsp;<code>value_if_false<\/code>.<\/p>\n\n\n\n<p>The ternary operator is equivalent to the following\u00a0<code>if...else<\/code>\u00a0statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>if condition: value_if_true else: value_if_true<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Note that you have been programming languages such as C# or Java, and you\u2019re familiar with the following ternary operator syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>condition ? value_if_true : value_if_false<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>However, Python doesn\u2019t support this ternary operator syntax.<\/p>\n\n\n\n<p>The following program uses the ternary operator instead of the\u00a0<code>if<\/code>\u00a0statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>age = input('Enter your age:') ticket_price = 20 if int(age) >= 18 else 5 print(f\"The ticket price is {ticket_price}\")<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about the Python ternary operator and how to use it to make your code more concise. Introduction to Python Ternary Operator The following program prompts you for your age and determines the ticket price based on it: Here is the output when you enter 18: In this example, the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-3-control-flow"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=35"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":36,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/35\/revisions\/36"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}