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

  • SEARCH
  • Home
  • 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 9164903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:47:34+00:00 2026-06-17T14:47:34+00:00

Why is it prohibited to use statements on either side of python’s ternary conditional?

  • 0

Why is it prohibited to use statements on either side of python’s ternary conditional? I can’t see any obvious reason how a few of the following naive syntax examples could be ambiguous or broken – but I’m sure there must be a good reason why it’s disallowed!

>>> x, y = 0, 0
>>> (x += 1) if random.choice([0, 1]) else (y += 1)
        ^
SyntaxError: invalid syntax

>>> (x if random.choice([0, 1]) else y) += 1
SyntaxError: can't assign to conditional expression


>>> print 'hello world' if random.choice([0, 1]) else raise StandardError()
  File "<stdin>", line 1
    print 'hello world' if random.choice([0, 1]) else raise StandardError()
                                                          ^
SyntaxError: invalid syntax

Can you give an example where allowing a statement to be used in a ternary could be dangerous or ill-defined?

  • 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. Editorial Team
    Editorial Team
    2026-06-17T14:47:35+00:00Added an answer on June 17, 2026 at 2:47 pm

    Expressions are evaluated, statements are executed. This is something completely different.

    Python developers have decided to strictly separate between expressions and statements . This will make you “fail early” instead of ending up with an error that is hard to debug.

    In other languages, like C(++), you can do things like

    if (i=15) {...}
    

    but you could also do

    if (i==15) {...}
    

    In the first example, you assign 15 to the variable i, and this new value of i is then evaluated by the if.

    Even worse are increment/decrement operators. What is the difference between

    if (i--) {...}
    

    and

    if (--i) {...}
    

    ?

    For the first example, the value of i is evaluated, then it is incremented. For the second example, the order is the other way around, so if i was 1 before, the result is different.

    In Python, such things are prohibited by the syntax, due to the experience gathered with such concepts in other languages.

    I’m not quite sure if this completely answers your question, but I hope I at least could give some insight into the pythonic approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to port an open source program to iPhone, the license prohibited any
I'd like to use define a generic class in java, that can only be
Can child process use the ptrace system call to trace its parent? Os is
In IIS 6, I can use the Web Service Extensions folder in Inetmgr to
Can I use multiple @throws javadoc tags if my application throws the same exception
Ok so I need to run wget but I'm prohibited from creating 'dot' files
The following 2 statements are to join using gifts.giftID = sentgifts.giftID: mysql> select *
I use a WSC (a COM server written in JScript and run with scrobj.dll)
I was reading today about researchers discovering that NVidia's Phys-X libraries use x87 FP
java.util.zip has well-known problems with native memory usage, so i'm trying to use a

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.