{"id":6318,"date":"2019-12-15T11:34:57","date_gmt":"2019-12-15T10:34:57","guid":{"rendered":"https:\/\/monodes.com\/predaelli\/?p=6318"},"modified":"2019-12-15T11:34:57","modified_gmt":"2019-12-15T10:34:57","slug":"eiffels-conditional-expressions","status":"publish","type":"post","link":"https:\/\/monodes.com\/predaelli\/2019\/12\/15\/eiffels-conditional-expressions\/","title":{"rendered":"Eiffel&#8217;s conditional expressions"},"content":{"rendered":"<blockquote><p>The ternary conditional operator is a short-hand method for writing an if\/else statement.<\/p><\/blockquote>\n<p><em><a href=\"https:\/\/medium.com\/better-programming\/does-python-have-a-ternary-conditional-operator-830454c34de7\">Does Python Have a Ternary Conditional Operator?<\/a> Yes, it does.<\/em><\/p>\n<p>This remids me that Eiffel, at least &#8220;ISE Eiffel&#8221; does actually have <a href=\"https:\/\/www.eiffel.org\/doc\/eiffel\/Conditional_expression\">conditional expressions<\/a> that are the same: <code class=\"\" data-line=\"\"><\/code><code class=\"\" data-line=\"\">&lt;span class=&quot;kwd&quot;&gt;answer := if&lt;\/span&gt;&lt;span class=&quot;pln&quot;&gt; time &lt; noon &lt;\/span&gt;&lt;span class=&quot;kwd&quot;&gt;then&lt;\/span&gt;<br \/>\n      &lt;span class=&quot;str&quot;&gt;&quot;Good morning&quot;&lt;\/span&gt;<br \/>\n   &lt;span class=&quot;kwd&quot;&gt;else &quot;Good afternoon&quot;&lt;\/span&gt;<br \/>\n   &lt;span class=&quot;kwd&quot;&gt;end&lt;\/span&gt;<\/code><\/p>\n<p><!--more--><!--nextpage--><\/p>\n<blockquote>\n<div>\n<div id=\"37ac\" class=\"dz ea eb at ec b ed ee ef eg eh ei ej\">\n<h1 class=\"ec b ed ek eb\">Does Python Have a Ternary Conditional Operator?<\/h1>\n<\/div>\n<\/div>\n<div id=\"93b9\" class=\"el ea ax at as cu em en eo ep eq er es\">\n<h2 class=\"as cu et eu ax\">Python\u2019s short-hand method for writing an if\/else statement<\/h2>\n<\/div>\n<div class=\"ev\">\n<div class=\"n ew ex ey ez\">\n<div class=\"o n\">\n<div>\n<div class=\"fa fb fc\">\n<div class=\"fd n fe o p do ff fg fh fi fj ds\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div>\n<div class=\"fa fb fc\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"r fk fc fb\" src=\"https:\/\/i0.wp.com\/miro.medium.com\/fit\/c\/58\/58\/2%2A_KGzadiy9s83D4vzhsCyyg.png?resize=48%2C48&#038;ssl=1\" alt=\"Jonathan Hsu\" width=\"48\" height=\"48\" \/><\/div>\n<\/div>\n<div class=\"fl ak r\">\n<div class=\"n\">\n<div>\n<div class=\"fm n o fn\"><span class=\"as cu fo au cc fp fq fr fs ft eb\"><a class=\"cy cz bb bc bd be bf bg bh bi fu bl bm dc dd\" href=\"https:\/\/medium.com\/@jhsu98?source=post_page-----830454c34de7----------------------\" rel=\"noopener\">Jonathan Hsu<\/a><\/span><\/p>\n<div class=\"fv r ap h\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><a class=\"cy cz bb bc bd be bf bg bh bi fu bl bm dc dd\" href=\"https:\/\/medium.com\/better-programming\/does-python-have-a-ternary-conditional-operator-830454c34de7?source=post_page-----830454c34de7----------------------\" rel=\"noopener\">Oct 14<\/a> \u00b7 2 min read<\/div>\n<div class=\"n o\">\n<div class=\"gq r ap\"><\/div>\n<\/div>\n<div class=\"gq r ap\"><\/div>\n<div class=\"gr r ao\">\n<div>\n<div class=\"bx\" role=\"tooltip\" aria-hidden=\"true\" aria-describedby=\"1\" aria-labelledby=\"1\"><\/div>\n<\/div>\n<\/div>\n<figure class=\"gt gu gv gw gx gy dk dl paragraph-image\">\n<div class=\"gz ha fa hb ak\">\n<div class=\"dk dl gs\">\n<div class=\"hh r fa hi\">\n<div class=\"hj r\">\n<div class=\"hc hd do t u he ak cc hf hg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"do t u he ak hk hl aq ow\" role=\"presentation\" src=\"https:\/\/i0.wp.com\/miro.medium.com\/max\/36\/1%2A7i3ZJcl1yL6jjEOsVHz0hw.jpeg?resize=910%2C607&#038;ssl=1\" width=\"910\" height=\"607\" \/><\/div>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"mi ov do t u he ak hn\" role=\"presentation\" src=\"https:\/\/i0.wp.com\/miro.medium.com\/max\/5881\/1%2A7i3ZJcl1yL6jjEOsVHz0hw.jpeg?resize=910%2C607&#038;ssl=1\" width=\"910\" height=\"607\" \/><\/div>\n<\/div>\n<\/div>\n<\/div><figcaption class=\"ax fo ho hp hq dm dk dl hr hs as cu\" data-selectable-paragraph=\"\">Photo by <a class=\"cy by ht hu hv hw\" href=\"https:\/\/unsplash.com\/@thevisualiza?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" target=\"_blank\" rel=\"noopener nofollow noreferrer\">Dil<\/a> on <a class=\"cy by ht hu hv hw\" href=\"https:\/\/unsplash.com\/s\/photos\/doors?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" target=\"_blank\" rel=\"noopener nofollow noreferrer\">Unsplash<\/a><\/figcaption><\/figure>\n<p id=\"d45d\" class=\"hx hy eb at hz b ia ib ic id ie if ig ih ii ij ik\" data-selectable-paragraph=\"\"><strong class=\"hz il\">The short answer is <em class=\"im\">yes<\/em><\/strong>.<\/p>\n<p id=\"4b72\" class=\"hx hy eb at hz b ia ib ic id ie if ig ih ii ij ik\" data-selectable-paragraph=\"\">The ternary conditional operator is a short-hand method for writing an if\/else statement. There are three components to the ternary operator: the expression\/condition, the positive value, and the negative value.<\/p>\n<p id=\"f14f\" class=\"hx hy eb at hz b ia ib ic id ie if ig ih ii ij ik\" data-selectable-paragraph=\"\">The ternary operator is traditionally expressed using the question mark and colon organized as <code class=\"\" data-line=\"\">expression ? positive value : negative value<\/code><\/p>\n<p id=\"a12d\" class=\"hx hy eb at hz b ia ib ic id ie if ig ih ii ij ik\" data-selectable-paragraph=\"\">When the expression evaluates to true, the positive value is used\u2014otherwise the negative value is used.<\/p>\n<p id=\"4943\" class=\"hx hy eb at hz b ia ib ic id ie if ig ih ii ij ik\" data-selectable-paragraph=\"\">Python does not follow the same syntax as previous languages; however, the technique does exist. In Python, the components are reorganized and the keywords <code class=\"\" data-line=\"\">if<\/code> and <code class=\"\" data-line=\"\">else<\/code> are used, which reads <code class=\"\" data-line=\"\">positive value if expression else negative value<\/code><\/p>\n<p id=\"876f\" class=\"hx hy eb at hz b ia ib ic id ie if ig ih ii ij ik\" data-selectable-paragraph=\"\">Given the Python syntax, the ternary operator is converted as follows:<\/p>\n<pre class=\"gt gu gv gw gx ir is cl\"><span id=\"5899\" class=\"it iu eb at iq b fo iv iw r ix\" data-selectable-paragraph=\"\"># Traditional Ternary Operator\ncan_vote = (age &gt;= 18) true : false;<\/span><span id=\"ae38\" class=\"it iu eb at iq b fo iy iz ja jb jc iw r ix\" data-selectable-paragraph=\"\"># Python Ternary Operator\ncan_vote = True if age &gt;= 18 else False<\/span><\/pre>\n<p id=\"56b9\" class=\"hx hy eb at hz b ia ib ic id ie if ig ih ii ij ik\" data-selectable-paragraph=\"\">Keep in mind that the purpose of the ternary operator is to write more concise code, improving readability. However, the Python implementation may produce the opposite effect, being confusing to read. Additionally, the lack of explicit operators can lead to order of operations discrepancies.<\/p>\n<pre class=\"gt gu gv gw gx ir is cl\"><span id=\"7a1d\" class=\"it iu eb at iq b fo iv iw r ix\" data-selectable-paragraph=\"\">x = 5\ny = 10<\/span><span id=\"76dd\" class=\"it iu eb at iq b fo iy iz ja jb jc iw r ix\" data-selectable-paragraph=\"\">z = 1 + x if x == 1 else y   # 10\nz = 1 + (x if x == 1 else y) # 11<\/span><\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">The ternary conditional operator is a short-hand method for writing an if\/else statement. Does Python Have a Ternary Conditional Operator? Yes, it does. This remids me that Eiffel, at least &#8220;ISE Eiffel&#8221; does actually have conditional expressions that are the same: &lt;span class=&quot;kwd&quot;&gt;answer := if&lt;\/span&gt;&lt;span class=&quot;pln&quot;&gt; time &lt; noon &lt;\/span&gt;&lt;span class=&quot;kwd&quot;&gt;then&lt;\/span&gt; &lt;span class=&quot;str&quot;&gt;&quot;Good morning&quot;&lt;\/span&gt; &lt;span&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/monodes.com\/predaelli\/2019\/12\/15\/eiffels-conditional-expressions\/\">Read more &rarr;<\/a><\/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,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[34,113],"tags":[],"class_list":["post-6318","post","type-post","status-publish","format-standard","hentry","category-eiffel","category-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6daft-1DU","jetpack-related-posts":[{"id":4677,"url":"https:\/\/monodes.com\/predaelli\/2018\/10\/22\/conditional-javascript-for-experts-hacker-noon\/","url_meta":{"origin":6318,"position":0},"title":"Conditional JavaScript for Experts \u2013 Hacker Noon","author":"Paolo Redaelli","date":"2018-10-22","format":false,"excerpt":"Mastery of conditional expressions for cleaner codes They say in Conditional JavaScript for Experts \u2013 Hacker Noon. What they are suggesting is to use side-effects of\u00a0 \"expression\", as Javascript does not separate commands and queries. Call me dogmatic but this behavior to look like a fundamental ingredient for disaster. In\u2026","rel":"","context":"In &quot;Eiffel&quot;","block_context":{"text":"Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11033,"url":"https:\/\/monodes.com\/predaelli\/2023\/12\/17\/textual\/","url_meta":{"origin":6318,"position":1},"title":"Textual","author":"Paolo Redaelli","date":"2023-12-17","format":false,"excerpt":"Textual is a TUI framework for Python, inspired by modern web development. Textual is a Rapid Application Development framework for Python, built by Textualize.io. Build sophisticated user interfaces with a simple Python API. Run your apps in the terminal or a web browser! Well, I just wish I could have\u2026","rel":"","context":"In &quot;Eiffel&quot;","block_context":{"text":"Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9687,"url":"https:\/\/monodes.com\/predaelli\/2022\/10\/01\/spacevim-has-eiffel-support\/","url_meta":{"origin":6318,"position":2},"title":"SpaceVim has Eiffel support!","author":"Paolo Redaelli","date":"2022-10-01","format":false,"excerpt":"SpaceVim, a community-driven vim distribution that seeks to provide layer feature, besides turning Vim into a nifty IDE for several languages (C\/C++, Rust, Kotlin, Go, Python, Java and JavaScript plus others), it offers among the available layers one for Eiffel! \u00a0","rel":"","context":"In &quot;Eiffel&quot;","block_context":{"text":"Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9245,"url":"https:\/\/monodes.com\/predaelli\/2022\/04\/05\/so-many-things-to-fix\/","url_meta":{"origin":6318,"position":3},"title":"So many things to fix!","author":"Paolo Redaelli","date":"2022-04-05","format":false,"excerpt":"Well, instead of reading Write Your Own C-extension to Speed Up Python by 100x | by Mike Huls | Towards Data Science How to write, compile, package and import your own, superfast C-module into\u00a0Python We should have read Write Your Own Eiffel-extension to Speed Up Python by 100x How to\u2026","rel":"","context":"In &quot;Mood&quot;","block_context":{"text":"Mood","link":"https:\/\/monodes.com\/predaelli\/category\/mood\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":8612,"url":"https:\/\/monodes.com\/predaelli\/2022\/05\/30\/eiffel-rust-and-memory-management\/","url_meta":{"origin":6318,"position":4},"title":"Eiffel, Rust and memory management","author":"Paolo Redaelli","date":"2022-05-30","format":false,"excerpt":"It already passed one year almost two years since in \"Oh my lazyness!\" when I wrote I shall restart my efforts on Liberty EIffel and Monodes\u2026 \ud83d\ude41 I hope to integrate them in my current effort. While I haven't been able to integrate Eiffel in the endeavour that is keeping\u2026","rel":"","context":"In &quot;Eiffel&quot;","block_context":{"text":"Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9725,"url":"https:\/\/monodes.com\/predaelli\/2022\/10\/15\/python-is-inefficient\/","url_meta":{"origin":6318,"position":5},"title":"Python is inefficient","author":"Paolo Redaelli","date":"2022-10-15","format":false,"excerpt":"The most significant damage caused by Python Python is Destroying the Planet.writes Mohammed Ayar of course, I shall say as it is much less efficient, i.e. it's a lot slower than C. It's quite simple, and it's clearly showed by The\u00a0Computer Language Benchmarks Game, once known as \"The great language\u2026","rel":"","context":"In &quot;Senza categoria&quot;","block_context":{"text":"Senza categoria","link":"https:\/\/monodes.com\/predaelli\/category\/senza-categoria\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/6318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/comments?post=6318"}],"version-history":[{"count":0,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/6318\/revisions"}],"wp:attachment":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/media?parent=6318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/categories?post=6318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/tags?post=6318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}