Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 108319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:53:35+00:00 2026-05-11T01:53:35+00:00

Is there a ternary conditional operator in Python?

  • 0

Is there a ternary conditional operator in Python?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:53:36+00:00Added an answer on May 11, 2026 at 1:53 am

    Yes, it was added in version 2.5. The expression syntax is:

    a if condition else b 

    First condition is evaluated, then exactly one of either a or b is evaluated and returned based on the Boolean value of condition. If condition evaluates to True, then a is evaluated and returned but b is ignored, or else when b is evaluated and returned but a is ignored.

    This allows short-circuiting because when condition is true only a is evaluated and b is not evaluated at all, but when condition is false only b is evaluated and a is not evaluated at all.

    For example:

    >>> 'true' if True else 'false' 'true' >>> 'true' if False else 'false' 'false' 

    Note that conditionals are an expression, not a statement. This means you can’t use statements such as pass, or assignments with = (or "augmented" assignments like +=), within a conditional expression:

    >>> pass if False else pass   File "<stdin>", line 1     pass if False else pass          ^ SyntaxError: invalid syntax  >>> # Python parses this as `x = (1 if False else y) = 2` >>> # The `(1 if False else x)` part is actually valid, but >>> # it can't be on the left-hand side of `=`. >>> x = 1 if False else y = 2   File "<stdin>", line 1 SyntaxError: cannot assign to conditional expression  >>> # If we parenthesize it instead... >>> (x = 1) if False else (y = 2)   File "<stdin>", line 1     (x = 1) if False else (y = 2)        ^ SyntaxError: invalid syntax 

    (In 3.8 and above, the := "walrus" operator allows simple assignment of values as an expression, which is then compatible with this syntax. But please don’t write code like that; it will quickly become very difficult to understand.)

    Similarly, because it is an expression, the else part is mandatory:

    # Invalid syntax: we didn't specify what the value should be if the  # condition isn't met. It doesn't matter if we can verify that # ahead of time. a if True 

    You can, however, use conditional expressions to assign a variable like so:

    x = a if True else b 

    Or for example to return a value:

    # Of course we should just use the standard library `max`; # this is just for demonstration purposes. def my_max(a, b):     return a if a > b else b 

    Think of the conditional expression as switching between two values. We can use it when we are in a ‘one value or another’ situation, where we will do the same thing with the result, regardless of whether the condition is met. We use the expression to compute the value, and then do something with it. If you need to do something different depending on the condition, then use a normal if statement instead.


    Keep in mind that it’s frowned upon by some Pythonistas for several reasons:

    • The order of the arguments is different from those of the classic condition ? a : b ternary operator from many other languages (such as C, C++, Go, Perl, Ruby, Java, JavaScript, etc.), which may lead to bugs when people unfamiliar with Python’s "surprising" behaviour use it (they may reverse the argument order).
    • Some find it "unwieldy", since it goes contrary to the normal flow of thought (thinking of the condition first and then the effects).
    • Stylistic reasons. (Although the ‘inline if‘ can be really useful, and make your script more concise, it really does complicate your code)

    If you’re having trouble remembering the order, then remember that when read aloud, you (almost) say what you mean. For example, x = 4 if b > 8 else 9 is read aloud as x will be 4 if b is greater than 8 otherwise 9.

    Official documentation:

    • Conditional expressions
    • Is there an equivalent of C’s ”?:” ternary operator?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 83k
  • Answers 83k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use Beyond Compare's "Text Compare Report" command in… May 11, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer There are two schools of thought on this (at least).… May 11, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer You can access the Request object directly. It is a… May 11, 2026 at 4:53 pm

Related Questions

In Perl (and other languages) a conditional ternary operator can be expressed like this:
I've got a DateTime? that I'm trying to insert into a field using a
We are developing a web application that will be sold to many clients. There
I have a large classic ASP app that I have to maintain, and I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.