Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (900 points)
edited by

I want 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 Answer

0 votes
by (119k points)

Here is the syntax to write inline if statement:

(expression_if_true) if condition else (expression_if_false)

For this example, you cannot write an inline statement without else condition. Here is the way to write inline if statement for print:

print ( a if b else 0)

If you want to learn Python then register for this Python Certification course that provides instructor-led training, certification, and also job assistance.

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Aug 26, 2019 in Python by Sammy (47.6k points)

Browse Categories

...