{"id":3177,"date":"2023-10-20T08:44:22","date_gmt":"2023-10-20T03:14:22","guid":{"rendered":"https:\/\/python-programs.com\/?p=3177"},"modified":"2023-11-10T11:50:45","modified_gmt":"2023-11-10T06:20:45","slug":"pythonif-else-in-one-line-ternary-operator-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/","title":{"rendered":"Python: if-else in One Line\/Ternary Operator in Python"},"content":{"rendered":"

How to write an if-else statement in one line in python<\/h2>\n

In this article, we will discuss how we can implement the if-else statement in python in a single line. This is also called the ternary operator. In python, the ternary operator doesn’t have syntax like that in c++ or java but working is the same. Let us see this concept with some examples.<\/p>\n

Syntax: val1 if [expression] else val2<\/p>\n

Explanation: If our if the statement becomes true then we get output as val1 otherwise we get output as val2.Let us see this with an example.<\/p>\n

a=10\r\nprint(1 if a%2==0 else 0)\r\na=9\r\nprint(1 if a%2==0 else 0)<\/pre>\n

Output<\/p>\n

1\r\n0<\/pre>\n

Here we see that we get output 1 when our number is even else we get output as 0. When the condition evaluates\u00a0to True, then the result of this one-liner if..else expression will be val1 in this case 1. Whereas, if the condition evaluates to False, then the result of this one-liner expression will be val2 in this case 0.<\/p>\n

Here we use integer with our if-else statement. Let us see an example of how to deal with a string.<\/p>\n

a=10\r\nprint(\"Number is even\" if a%2==0 else \"Number is odd\")<\/pre>\n

Output<\/p>\n

Number is even<\/pre>\n

Here we see that how we easily deal with string with this ternary operator or one-liner if-else statement. In a similar way, we can deal with different data types.<\/p>\n

With the increase in the simplicity of code, we also have to take some precaution otherwise a situation may arise in which we understand the code in a different way while the code work in a different way. As different operator have different associativity and precedence which increase the responsibility of the programmer to use this operator in a correct way. Let us understand the concept with help of an example.<\/p>\n

a=10 \r\nb=5\r\nprint(a\/b*3 if a%2==0 else 1)\r\nprint(a\/(b*3 if a%2==0 else 1))<\/pre>\n

Output<\/p>\n

6.0\r\n0.6666666666666666<\/pre>\n

Here we see with change in one bracket our output differ but code seems to be similar.This confusion tend to be increase when we write big and complex code.Hence we have to use operator in a proper way.<\/p>\n

Note: Conditional expressions have the lowest priority amongst all Python operations.<\/p>\n

Nested if-else in one line<\/h3>\n

We see that we can execute single if-else in a single line easily.Now we see how we can execute multiple if else statement in a single line with the help of an example.<\/p>\n

a=10 \r\nprint(\"a is negative\" if a<0 else \"a is positive\" if a>0 else \"a is 0\")\r\n\r\n#above code is similar as\r\nif(a<0):\r\n    print(\"a is negative\")\r\nelse:\r\n    if(a>0):\r\n        print(\"a is positive\")\r\n    else:\r\n        print(\"a is 0\")<\/pre>\n

Output<\/p>\n

a is positive\r\na is positive<\/pre>\n

Both the code give a similar result. We see how we save 5-6 lines by using the ternary operator.<\/p>\n

So in this article, we see how to use the if-else and nested if-else statement in one line and with that, we see how to use the operator effectively otherwise the result may differ.<\/p>\n","protected":false},"excerpt":{"rendered":"

How to write an if-else statement in one line in python In this article, we will discuss how we can implement the if-else statement in python in a single line. This is also called the ternary operator. In python, the ternary operator doesn’t have syntax like that in c++ or java but working is the …<\/p>\n

Python: if-else in One Line\/Ternary Operator in Python<\/span> Read More »<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython: if-else in One Line\/Ternary Operator in Python - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python: if-else in One Line\/Ternary Operator in Python - Python Programs\" \/>\n<meta property=\"og:description\" content=\"How to write an if-else statement in one line in python In this article, we will discuss how we can implement the if-else statement in python in a single line. This is also called the ternary operator. In python, the ternary operator doesn’t have syntax like that in c++ or java but working is the … Python: if-else in One Line\/Ternary Operator in Python Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-20T03:14:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:20:45+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mayank Gupta\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/#webpage\",\"url\":\"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/\",\"name\":\"Python: if-else in One Line\/Ternary Operator in Python - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-20T03:14:22+00:00\",\"dateModified\":\"2023-11-10T06:20:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python: if-else in One Line\/Ternary Operator in Python\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/2deaa2ec7f26a4b5e527737709032a41\"},\"headline\":\"Python: if-else in One Line\/Ternary Operator in Python\",\"datePublished\":\"2023-10-20T03:14:22+00:00\",\"dateModified\":\"2023-11-10T06:20:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/#webpage\"},\"wordCount\":431,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/2deaa2ec7f26a4b5e527737709032a41\",\"name\":\"Mayank Gupta\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d90409993b088dd1f731cb0e2d8b231c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d90409993b088dd1f731cb0e2d8b231c?s=96&d=mm&r=g\",\"caption\":\"Mayank Gupta\"},\"url\":\"https:\/\/python-programs.com\/author\/mayank\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python: if-else in One Line\/Ternary Operator in Python - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Python: if-else in One Line\/Ternary Operator in Python - Python Programs","og_description":"How to write an if-else statement in one line in python In this article, we will discuss how we can implement the if-else statement in python in a single line. This is also called the ternary operator. In python, the ternary operator doesn’t have syntax like that in c++ or java but working is the … Python: if-else in One Line\/Ternary Operator in Python Read More »","og_url":"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-20T03:14:22+00:00","article_modified_time":"2023-11-10T06:20:45+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Mayank Gupta","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/#webpage","url":"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/","name":"Python: if-else in One Line\/Ternary Operator in Python - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-20T03:14:22+00:00","dateModified":"2023-11-10T06:20:45+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python: if-else in One Line\/Ternary Operator in Python"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/2deaa2ec7f26a4b5e527737709032a41"},"headline":"Python: if-else in One Line\/Ternary Operator in Python","datePublished":"2023-10-20T03:14:22+00:00","dateModified":"2023-11-10T06:20:45+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/#webpage"},"wordCount":431,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/pythonif-else-in-one-line-ternary-operator-in-python\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/2deaa2ec7f26a4b5e527737709032a41","name":"Mayank Gupta","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d90409993b088dd1f731cb0e2d8b231c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d90409993b088dd1f731cb0e2d8b231c?s=96&d=mm&r=g","caption":"Mayank Gupta"},"url":"https:\/\/python-programs.com\/author\/mayank\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3177"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=3177"}],"version-history":[{"count":6,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3177\/revisions"}],"predecessor-version":[{"id":3219,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3177\/revisions\/3219"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}