-2

I have been trying to make a Basic if-else code of Even-odd number in one line whose output must contain only one type of number(even or odd), I tried the following code:

for i in range(10):
    print(i) if i%2!=0 else pass

But the output shows me a syntax error like this:

    File "<stdin>", line 2
    print(i) if i%2!=0 else pass
                               ^
SyntaxError: invalid syntax

After searching and trying, I got to know that "pass" or "continue" do not work in this way. I wanted to know the reason behind this.

Sarques
  • 465
  • 7
  • 17
  • possible duplicate https://stackoverflow.com/questions/11880430/how-to-write-inline-if-statement-for-print – bergerg Oct 24 '18 at 13:03
  • Possible duplicate of [Python Ternary Operator Without else](https://stackoverflow.com/questions/12199757/python-ternary-operator-without-else) – jfowkes Oct 24 '18 at 13:03
  • 3
    Possible duplicate of [How to write inline if statement for print?](https://stackoverflow.com/questions/11880430/how-to-write-inline-if-statement-for-print) – bergerg Oct 24 '18 at 13:04
  • @clearshot66 I tried, no effect though. – Sarques Oct 24 '18 at 13:51

1 Answers1

0

Using if and else inline like that only works with expressions. You can't put arbitrary statements. This is the kind of thing the Python designers intentionally don't want people doing, misusing language syntax to create dense, hard-to-read code.

The Zen of Python

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one—and preferably only one—obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Use multiple lines:

if i % 2 != 0:
    print(i)
else:
    pass

Of course, the else isn't needed. If you remove it, I still recommend you leave the if block on two lines. There's no reason to compact it.

if i % 2 != 0:
    print(i)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • While this is certainly not wrong, I find the first paragraph a bit misleading. The "normal" `if` (albeit without `else`) _can_ be used with statements in a single line, it's explicitly supported by the language, so can't be universally frowned upon, either. The point is that the `...if...else...` OP is using is something entirely different than `if ...: ... else: ...`, which IMHO is not clear from the answer. (I think this is a general problem with Python's simple syntax; I've never seen problems like this with Java's `...?...:...`, or the common `if x == a or b` problem with `||`...) – tobias_k Oct 24 '18 at 13:24