{"id":23780,"date":"2021-10-03T20:27:16","date_gmt":"2021-10-03T14:57:16","guid":{"rendered":"https:\/\/python-programs.com\/?p=23780"},"modified":"2021-11-22T18:33:26","modified_gmt":"2021-11-22T13:03:26","slug":"python-program-to-write-inline-if-statement","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/","title":{"rendered":"Python Program To Write Inline If Statement"},"content":{"rendered":"

Python’s if-else statement is logical. Python does not use the ternary operator as other languages do for inline if statements. It instead provides a one-line code to evaluate the first expression if the condition is met. Otherwise, the second expression is evaluated.<\/p>\n

Program To Write Inline If Statement in Python<\/h2>\n

inline if statement in Python:<\/strong><\/p>\n

In Python, the inline if-else expression contains two statements and executes one of them based on the condition’s evaluation. In Python, there are two kinds of if statements.<\/p>\n

    \n
  1. Simple if statement<\/li>\n
  2. if-else expression statement<\/li>\n<\/ol>\n

    In a simple, if statement, we write a condition, and the if block is executed based on that condition.<\/p>\n

    Syntax:<\/strong><\/p>\n

    if<\/span> condition<\/span>: statement \r\nif<\/span> condition<\/span>:\r\n    block<\/pre>\n

    Let us write an inline if statement that will execute the expression if the condition is True.<\/p>\n

    Examples:<\/strong><\/p>\n

    Example1:<\/strong><\/p>\n

    numb = 12\r\nif numb > 10: print(\"hello btechgeeks\")<\/pre>\n

    Output:<\/strong><\/p>\n

    hello btechgeeks<\/pre>\n

    Explanation:<\/strong><\/p>\n

    Here the given number 12 is greater than 10. So, it prints the given statement \"hello btechgeeks\".<\/pre>\n

    Example2:<\/strong><\/p>\n

    numb = 32\r\nif numb % 2 == 0 : print(\"The Given number is Even\")<\/pre>\n

    Output:<\/strong><\/p>\n

    The Given number is Even<\/pre>\n

    Explanation:<\/strong><\/p>\n

    Here the given number 32 is Even. So, it prints the given statement \"The Given number is Even\".<\/pre>\n

    inline if-else statement in python:<\/strong><\/p>\n

    Python’s inline if-else expression must include an else clause.<\/p>\n

    Examples:<\/strong><\/p>\n

    Example1:<\/strong><\/p>\n

    numb = 10\r\ntemp = 3\r\nprint(numb if temp else 0)<\/pre>\n

    Output:<\/strong><\/p>\n

    10<\/pre>\n

    Explanation:<\/strong><\/p>\n

    Here it prints the given number value because the given if condition is True \r\n( temp is not equal to 0 so, it returns True)<\/pre>\n

    Example2:<\/strong><\/p>\n

    numb = 15\r\ntemp = 0\r\nprint(numb if temp else 0)<\/pre>\n

    Output:<\/strong><\/p>\n

    0<\/pre>\n

    Explanation:<\/strong><\/p>\n

    Here it prints 0 because the given if condition is False (given temp is 0)\r\nsuppose the temp given is any other number other than 0, then it prints the given number<\/pre>\n

    User Input:<\/strong><\/p>\n

    Example1:<\/strong><\/p>\n

    numb = int(input(\"Enter some random number = \")) \r\ntemp = 3 \r\nprint(numb if temp else 0)<\/pre>\n

    Output:<\/strong><\/p>\n

    Enter some random number = 10\r\n10<\/pre>\n

    Example 2:<\/strong><\/p>\n

    numb = int(input(\"Enter some random number = \")) \r\nprint(\"Even\" if numb%2==0 else \"Odd\")<\/pre>\n

    Output:<\/strong><\/p>\n

    Enter some random number = 13\r\nOdd<\/pre>\n

    Example 3:<\/strong><\/p>\n

    gvn_str = input(\"Enter some random String = \") \r\nprint(\"True\" if gvn_str==\"hello btechgeeks\" else \"False\")<\/pre>\n

    Output:<\/strong><\/p>\n

    Enter some random String = hello btechgeeks\r\nTrue<\/pre>\n

    Example 4:<\/strong><\/p>\n

    gvn_str = input(\"Enter some random String = \")\r\nprint(\"True\" if gvn_str==\"hello btechgeeks\" else \"False\")<\/pre>\n

    Output:<\/strong><\/p>\n

    Enter some random String = hello world\r\nFalse<\/pre>\n","protected":false},"excerpt":{"rendered":"

    Python’s if-else statement is logical. Python does not use the ternary operator as other languages do for inline if statements. It instead provides a one-line code to evaluate the first expression if the condition is met. Otherwise, the second expression is evaluated. Program To Write Inline If Statement in Python inline if statement in Python: …<\/p>\n

    Python Program To Write Inline If Statement<\/span> Read More »<\/a><\/p>\n","protected":false},"author":7,"featured_media":23825,"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 Program To Write Inline If Statement - 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\/python-program-to-write-inline-if-statement\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program To Write Inline If Statement - Python Programs\" \/>\n<meta property=\"og:description\" content=\"Python’s if-else statement is logical. Python does not use the ternary operator as other languages do for inline if statements. It instead provides a one-line code to evaluate the first expression if the condition is met. Otherwise, the second expression is evaluated. Program To Write Inline If Statement in Python inline if statement in Python: … Python Program To Write Inline If Statement Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/\" \/>\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=\"2021-10-03T14:57:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-22T13:03:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-To-Write-Inline-If-Statement.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\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=\"Vikram Chiluka\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 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\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#primaryimage\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-To-Write-Inline-If-Statement.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-To-Write-Inline-If-Statement.png\",\"width\":1280,\"height\":720,\"caption\":\"Program To Write Inline If Statement\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/\",\"name\":\"Python Program To Write Inline If Statement - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#primaryimage\"},\"datePublished\":\"2021-10-03T14:57:16+00:00\",\"dateModified\":\"2021-11-22T13:03:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Program To Write Inline If Statement\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602\"},\"headline\":\"Python Program To Write Inline If Statement\",\"datePublished\":\"2021-10-03T14:57:16+00:00\",\"dateModified\":\"2021-11-22T13:03:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#webpage\"},\"wordCount\":171,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-To-Write-Inline-If-Statement.png\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602\",\"name\":\"Vikram Chiluka\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g\",\"caption\":\"Vikram Chiluka\"},\"url\":\"https:\/\/python-programs.com\/author\/vikram\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Program To Write Inline If Statement - 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\/python-program-to-write-inline-if-statement\/","og_locale":"en_US","og_type":"article","og_title":"Python Program To Write Inline If Statement - Python Programs","og_description":"Python’s if-else statement is logical. Python does not use the ternary operator as other languages do for inline if statements. It instead provides a one-line code to evaluate the first expression if the condition is met. Otherwise, the second expression is evaluated. Program To Write Inline If Statement in Python inline if statement in Python: … Python Program To Write Inline If Statement Read More »","og_url":"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2021-10-03T14:57:16+00:00","article_modified_time":"2021-11-22T13:03:26+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-To-Write-Inline-If-Statement.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Vikram Chiluka","Est. reading time":"2 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":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#primaryimage","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-To-Write-Inline-If-Statement.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-To-Write-Inline-If-Statement.png","width":1280,"height":720,"caption":"Program To Write Inline If Statement"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#webpage","url":"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/","name":"Python Program To Write Inline If Statement - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#primaryimage"},"datePublished":"2021-10-03T14:57:16+00:00","dateModified":"2021-11-22T13:03:26+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Program To Write Inline If Statement"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602"},"headline":"Python Program To Write Inline If Statement","datePublished":"2021-10-03T14:57:16+00:00","dateModified":"2021-11-22T13:03:26+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#webpage"},"wordCount":171,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"image":{"@id":"https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#primaryimage"},"thumbnailUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-To-Write-Inline-If-Statement.png","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-program-to-write-inline-if-statement\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/fb109fd98e2d5a15fd4dac9970797602","name":"Vikram Chiluka","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c56c77f578d45de43af6feb443618ed7?s=96&d=mm&r=g","caption":"Vikram Chiluka"},"url":"https:\/\/python-programs.com\/author\/vikram\/"}]}},"jetpack_featured_media_url":"https:\/\/python-programs.com\/wp-content\/uploads\/2021\/10\/Program-To-Write-Inline-If-Statement.png","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/23780"}],"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\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=23780"}],"version-history":[{"count":9,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/23780\/revisions"}],"predecessor-version":[{"id":23810,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/23780\/revisions\/23810"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media\/23825"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=23780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=23780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=23780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}