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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:35:28+00:00 2026-06-09T09:35:28+00:00

I need to print some stuff only when a boolean variable is set to

  • 0

I need to print some stuff only when a boolean variable is set to True. So, after looking at this, I tried with a simple example:

>>> a = 100
>>> b = True
>>> print a if b
  File "<stdin>", line 1
    print a if b
             ^
SyntaxError: invalid syntax  

Same thing if I write print a if b==True.

What am I missing here?

  • 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-09T09:35:30+00:00Added an answer on June 9, 2026 at 9:35 am

    Python does not have a trailing if statement.

    There are two kinds of if in Python:

    1. if statement:

      if condition: statement
      if condition:
          block
      
    2. if expression (introduced in Python 2.5)

      expression_if_true if condition else expression_if_false
      

    And note, that both print a and b = a are statements. Only the a part is an expression. So if you write

    print a if b else 0
    

    it means

    print (a if b else 0)
    

    and similarly when you write

    x = a if b else 0
    

    it means

    x = (a if b else 0)
    

    Now what would it print/assign if there was no else clause? The print/assignment is still there.

    And note, that if you don’t want it to be there, you can always write the regular if statement on a single line, though it’s less readable and there is really no reason to avoid the two-line variant.

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

Sidebar

Related Questions

I need to sort then print the result increasing and decreasing. I have some
I need to know where the Sprite.graphics start to do some stuff. When I
After two days of bashing my head and trying to figure this stuff out
I'm developing a library to IOS, which at some point need to create a
At some point I need to display a disabled (greyed out by disabled=disabled attribute)
I need a script that inserts a number at some point into a file
I have some function where I need to pass a point datatype . somefunc(United
I need to print real datbase type name from DbParameter.DbType , but when I
I need to print a two-dimensional array etc: var myArray = [ [0,1,2,0], [0,1,2,0]
I need to print out receipts that inserted into my database, and the latest

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.