{"id":19732,"date":"2022-05-19T11:48:13","date_gmt":"2022-05-19T06:18:13","guid":{"rendered":"https:\/\/java2blog.com\/?p=19732"},"modified":"2023-11-25T11:30:30","modified_gmt":"2023-11-25T06:00:30","slug":"if-else-one-line-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/if-else-one-line-python\/","title":{"rendered":"If-else in one line in Python"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#The_syntax_for_if-else_in_one_line_in_Python\">The syntax for if-else in one line in Python<\/a><\/li><li><a href=\"#Using_tuples_to_emulate_if-else_in_one_line_in_Python\">Using tuples to emulate if-else in one line in Python<\/a><\/li><li><a href=\"#Using_dictionaries_to_emulate_if-else_in_one_line_in_Python\">Using dictionaries to emulate if-else in one line in Python<\/a><\/li><li><a href=\"#Using_and_and_or_operator_to_emulate_if-else_in_one_line_in_Python\">Using and and or operator to emulate if-else in one line in Python<\/a><\/li><li><a href=\"#Conclusion\">Conclusion<\/a><\/li><\/ul><\/div>\n<p>The if-else statement is one of the common statements of many programming languages like Python, C\/C++, and Java. It is a conditional statement and is used to perform some action based on a condition. <\/p>\n<p>With the if-else statement, we run a block of code based on the evaluation of a provided condition. If the condition is True, the code from the <code>if<\/code> block is executed, otherwise the code from the <code>else<\/code> block is executed. <\/p>\n<p>We can also have an if-else ladder where we can use multiple <code>elif<\/code> statements to evaluate many conditions.<\/p>\n<p>In some other programming languages, there also exists a ternary operator which can be thought of as a compact form of the if-else statements in one line. Python does not support this operator. However, we can use the if-else in one line in Python.<\/p>\n<h2><span id=\"The_syntax_for_if-else_in_one_line_in_Python\">The syntax for if-else in one line in Python<\/span><\/h2>\n<p>To use the if-else in one line in Python, we can use the expression: <code>a if condition else b<\/code>. In this expression, <code>a<\/code> is evaluated if the <code>condition<\/code> is True. If the <code>condition<\/code> is false, then <code>b<\/code> is evaluated.<\/p>\n<p>Let us use this as an example.<\/p>\n<pre code=\"python\" title=\"Syntax of if else in one line\">\na = 5\nx = 10 if a==5 else 15\nprint(\"X is 15\") if x != 10 else print(\"X is 10\")    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nX is 10\n<\/div>\n<p>In the above example,<\/p>\n<ul>\n<li>The value of variable <code>x<\/code> is assigned using the single line if-else statement.<\/li>\n<li>Since the condition is True, the value of <code>x<\/code> is assigned as 10.<\/li>\n<li>We again use the if-else to print the value of <code>x<\/code>.<\/li>\n<li>Since the condition is False, the expression of the <code>else<\/code> statement is evaluated.<\/li>\n<\/ul>\n<p>This is the traditional way to use the if-else in one line in Python. There are other ways also to emulate this. However, it is not recommended to use these because there is less consistency and usually both the expressions are evaluated first.<\/p>\n<section class=\"read-more-posts\">\n<div class=\"rm-header\">\n<h2>Further reading:<\/h2>\n<\/div>\n<div class=\"rm-wrap\">\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/python-if-and\/\" target=\"_blank\" rel=\"noopener\">Python If with And Operator<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/python-if-and\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/python-if-not\/\" target=\"_blank\" rel=\"noopener\">Python If with NOT Operator<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/python-if-not\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n<h2><span id=\"Using_tuples_to_emulate_if-else_in_one_line_in_Python\">Using tuples to emulate if-else in one line in Python<\/span><\/h2>\n<p>A tuple can store multiple items in Python. We can use tuple-indexing to emulate if-else in one line in Python. The expression for this is given as: <code>(b,a)[condition]<\/code><\/p>\n<p>We will use a tuple with two values. The value <code>a<\/code> is evaluated if the <code>condition<\/code> is True, and <code>b<\/code> for when it returns False.<\/p>\n<p>See the code below.<\/p>\n<pre code=\"python\" title=\"Using tuple\">\na = 5\nx = (15,10)[a==5]\nprint(x)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n10\n<\/div>\n<p>The above code returns the value of <code>x<\/code> as 10 because the given condition is True.<\/p>\n<h2><span id=\"Using_dictionaries_to_emulate_if-else_in_one_line_in_Python\">Using dictionaries to emulate if-else in one line in Python<\/span><\/h2>\n<p>A dictionary contains key-value pairs. It can be used to perform if-else in one line in Python. This method is similar to the previous one. The expression used in this method: <code>{True:a, False:b}[condition]<\/code>.<\/p>\n<p>We will have a dictionary containing two keys, <code>True<\/code> and <code>False<\/code>. These keys will have some value associated with them. A condition will be evaluated in the <code>[]<\/code> and based on the returned Boolean value the corresponding element will be accessed.<\/p>\n<p>We implement this in the code below.<\/p>\n<pre code=\"python\" title=\"Using dictionaries\">\na = 5\nx = {False:15,True:10}[a==5]\nprint(x)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n10\n<\/div>\n<p>The condition in the above example returns True. Hence, the value associated with the key <code>True<\/code> is retrieved from the dictionary.<\/p>\n<h2><span id=\"Using_and_and_or_operator_to_emulate_if-else_in_one_line_in_Python\">Using <code>and<\/code> and <code>or<\/code> operator to emulate if-else in one line in Python<\/span><\/h2>\n<p>The <code>and<\/code> and <code>or<\/code> are logical operators in Python. The <code>and<\/code> operator returns True when both values are True and the latter returns True if at least one value is True.<\/p>\n<p>We can use them to emulate  if-else in one line in Python using the following expression. <code>condition and a or b<\/code>. In this expression, <code>a<\/code> is evaluated when the <code>condition<\/code> is True and <code>b<\/code> when it is False. However, this is not a consistent method.<\/p>\n<p>The inconsistency arises when the value of <code>a<\/code> is False. During that case, <code>b<\/code> will be evaluated every time. Still, it can be used in simple cases.<\/p>\n<p>For example,<\/p>\n<pre code=\"python\" title=\"Using and or operator\">\na = 5\nx = a==5 and 10 or 15\nprint(x)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n10\n<\/div>\n<h2><span id=\"Conclusion\">Conclusion<\/span><\/h2>\n<p>This tutorial demonstrated the basics of the if-else statement and how to use if-else in one line in Python. We also demonstrated several methods to emulate the behavior of if-else in one line. These methods included the use of tuples, dictionaries, and logical operators. <\/p>\n<p>Although these methods work similarly to if-else in one line in Python, their downside is that both the conditions are evaluated first before going to the condition. This is avoided in the if-else statements as one block is ignored based on the condition. Therefore, one should focus on the main syntax of if-else statement to use it in one line.<\/p>\n<p>That&#8217;s all about If-else in one line in Python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsThe syntax for if-else in one line in PythonUsing tuples to emulate if-else in one line in PythonUsing dictionaries to emulate if-else in one line in PythonUsing and and or operator to emulate if-else in one line in PythonConclusion The if-else statement is one of the common statements of many programming languages like [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/19732"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=19732"}],"version-history":[{"count":1,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/19732\/revisions"}],"predecessor-version":[{"id":25801,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/19732\/revisions\/25801"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=19732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=19732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=19732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}