{"id":115391,"date":"2020-07-14T07:44:23","date_gmt":"2020-07-14T07:44:23","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=10753"},"modified":"2020-07-14T07:44:23","modified_gmt":"2020-07-14T07:44:23","slug":"python-ternary-multiple-lines","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/07\/14\/python-ternary-multiple-lines\/","title":{"rendered":"Python Ternary Multiple Lines"},"content":{"rendered":"<p>What if you have a ternary operator that&#8217;s very long?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var = 'I want to learn Python' if 42**2&lt;166 else 'I want to learn Go programming'\nprint(var)\n# I want to learn Go programming\n<\/pre>\n<p><strong>Problem<\/strong>: How to write the ternary operator in multiple lines?<\/p>\n<h2>Short Recap: Ternary Operator <\/h2>\n<p><a href=\"https:\/\/blog.finxter.com\/python-one-line-ternary\/\" title=\"Python One Line Ternary\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Ternary Operator<\/strong><\/a>: The most basic ternary operator <code>x if c else y<\/code> consists of three operands <code>x<\/code>, <code>c<\/code>, and <code>y<\/code>. It is an expression with a return value. The ternary operator returns <code>x<\/code> if the Boolean expression <code>c<\/code> evaluates to <code>True<\/code>. Otherwise, if the expression <code>c<\/code> evaluates to <code>False<\/code>, the ternary operator returns the alternative <code>y<\/code>.<\/p>\n<p><strong>Syntax<\/strong>: The three operands are written as <code>x if c else y<\/code> which reads as &#8220;return <code>x<\/code> if <code>c<\/code> else return <code>y<\/code>&#8220;. Let&#8217;s write this more intuitively as:<\/p>\n<pre class=\"wp-block-preformatted\">&lt;OnTrue&gt; if &lt;Condition&gt; else &lt;OnFalse&gt;<\/pre>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Operand<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>&lt;OnTrue&gt;<\/td>\n<td>The return expression of the operator in case the condition evaluates to <code>True<\/code><\/td>\n<\/tr>\n<tr>\n<td>&lt;Condition&gt;<\/td>\n<td>The condition that determines whether to return the &lt;On True&gt; or the &lt;On False&gt; branch.<\/td>\n<\/tr>\n<tr>\n<td>&lt;OnFalse&gt;<\/td>\n<td>The return expression of the operator in case the condition evaluates to <code>False<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table><figcaption><em>Operands of the Ternary Operator<\/em><\/figcaption><\/figure>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"The Python Ternary Operator -- And a Surprising One-Liner Hack\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/9XXcUHXrqZ4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p><strong>Related article<\/strong>: For a full tutorial on the ternary operator, <a href=\"https:\/\/blog.finxter.com\/python-one-line-ternary\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python One Line Ternary\">check out our detailed blog article<\/a>.<\/p>\n<h2>Method: Parenthesis to Extend Logical Line Over Multiple Physical Lines<\/h2>\n<p><strong>Solution<\/strong>: You can extend any <a href=\"https:\/\/docs.python.org\/2\/reference\/lexical_analysis.html#line-structure\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/docs.python.org\/2\/reference\/lexical_analysis.html#line-structure\">logical line in Python over multiple physical lines<\/a> by using the parenthesis.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var = 'I want to learn Python' if 42**2&lt;166 else 'I want to learn Go programming'\nprint(var) var = ('I want to learn Python' if 42**2&lt;166 else 'I want to learn Go programming')\nprint(var)\n# I want to learn Go programming<\/pre>\n<p>This is the <a href=\"https:\/\/pythonchecker.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">PEP8 <\/a>standard way of breaking long lines&#8212;if you cannot do it in a more natural way (such as avoiding the ternary operator and using the if statement in this example). <\/p>\n<p><strong>Try it yourself:<\/strong><\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/4ff1b0b336\" width=\"100%\" height=\"356\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: Write a nested ternary operator and break it into multiple lines!<\/em><\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What if you have a ternary operator that&#8217;s very long? var = &#8216;I want to learn Python&#8217; if 42**2&lt;166 else &#8216;I want to learn Go programming&#8217; print(var) # I want to learn Go programming Problem: How to write the ternary operator in multiple lines? Short Recap: Ternary Operator Ternary Operator: The most basic ternary operator [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-115391","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/115391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=115391"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/115391\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=115391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=115391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=115391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}