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 7669443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:35:49+00:00 2026-05-31T15:35:49+00:00

I know the conditional expression in Python is X if C else Y, but

  • 0

I know the conditional expression in Python is X if C else Y, but I got some problems in using it.

I have two codes to be compared.

Code 1:

def fun(p):
    if len(p) >= 2:
        p[1] = 'Ok'
    else:
        p.append('Ok')
p = [1]
fun(p)
print p

Output of code 1:

[1, 'Ok']

Code 2:

def fun(p):
    (p[1] = 'Ok') if (len(p) >= 2) else p.append('OK')

p = [1]
fun(p)
print p

Output of code 2:

    (p[1] = 'Ok') if (len(p) >= 2) else p.append('OK')
      ^
SyntaxError: invalid syntax

I know in code 1, whose format is “if C : X else: Y”, the evaluation order is:

  1. C
  2. X
  3. Y

Code 2 throws a syntax error, the reason may be p[1] doesn’t exist. So I guess the format “X if C else Y” is evaluated as follows:

  1. X
  2. C
  3. Y

But that is only my guess. does anyone know the real reason why code 2 is wrong while code 1 is right?

  • 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-05-31T15:35:50+00:00Added an answer on May 31, 2026 at 3:35 pm

    The reason you have a SyntaxError is because Python differentiates between statements and expressions.

    Assignments, like

    p[1] = 'Ok'
    

    are statements and can’t be part of an expression, including the conditional expression. See What is the difference between an expression and a statement in Python? for more info.

    Order of evaluation doesn’t come into it — SyntaxErrors happen before any code is evaluated, when its being parsed.

    In both if statements and conditional expressions, The order of evaluations is ether

    1. Condition
    2. True Statement

    or

    1. Condition
    2. False Statement

    So, in

    if condition:
        true_statement
    else:
        false_statement
    

    or

    true_statement if condition else false_statement
    

    only the true or the fase statement is evaluated, depending on the truthiness of the condition.

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

Sidebar

Related Questions

Just for curiosity/convenience: C# provides two cool conditional expression features I know of: string
do you know if Python supports some keyword or expression like in C++ to
I m just learning regular expression matching using javascript, and have a doubt regarding
I'm trying to do a conditional statement with Expression Language, but can't get it
I have got a strange compile error while using condition operator. a,b are int
I use C#, I have a conditional statement in my code. I need to
As far as I know when writing a conditional statement in C such as
Does anyone know of a Sharepoint webpart that provides conditional formatting? For example, if
I would like to do conditional compilation in a program of mine. I know
I'd like to know if having to conditionals when using a JOIN keyword is

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.