-3

For a single statement, I can do this:

if 1: print("hello")

The output:

hello

But when I tried something like this:

if 1: print("hello") print("end")

I got an error:

  File "<stdin>", line 1
    if 1: print("hello") print("end")
                             ^
SyntaxError: invalid syntax

Why there is a syntax error? What is the right way to write multiple execution statement in single line with the if condition? I want to print the "hello" and "end" when the condition is satisfied.

I want a single-liner for the following statement:

if 1:
    print("hello")
    print("end")
E_net4
  • 27,810
  • 13
  • 101
  • 139
Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • 1
    What is print('end') doing after the hello? or, and, else?? – NoorJafri Apr 08 '19 at 10:46
  • Have a look here: https://stackoverflow.com/questions/6167127/how-to-put-multiple-statements-in-one-line – Konstantin Grigorov Apr 08 '19 at 10:51
  • 2
    What do you gain by compressing code to one line? – khelwood Apr 08 '19 at 10:51
  • @khelwood Its not always gain or loss matters. It is the approach. Actually I have few statements which were ruining my codes way or look. I am trying to debug the code and do not want that others should see the mess I performed. Ionly want that others should view the cleanest code that I ave written for the main purpose part from what I tried to debug. – Jaffer Wilson Apr 08 '19 at 11:06
  • 1
    `if 1: print("hello\nend")` ;) – furas Apr 08 '19 at 11:25
  • @furas Yes you are absolutely right. But there statement is not only print but class functions. I was not able to mention as it might have become too broad and no one answers. Hence, I cut it short enough to make sense. – Jaffer Wilson Apr 08 '19 at 11:45

3 Answers3

4

You should separate the two statements with ;

if 1: print("hello"); print("end")

But don't do it. write it normally

if 1:
   print("hello")
   print("end")

The compact form is probably acceptable with one statement only, but anything more complex is harder to read

blue_note
  • 27,712
  • 9
  • 72
  • 90
  • how does that work? does it create a tuple in-place? – blue_note Apr 08 '19 at 11:09
  • 1
    @blue_note yest it creates tuple - you can also use list `[print("hello"), print("end")]`. Sometimes you can see this list in `lambda:[ ..., ... ]` - it lets you run many functions in lambda. – furas Apr 08 '19 at 11:24
  • @furas: ok, but then it's kind of misleading. it creates an iterable just for the side-effect – blue_note Apr 08 '19 at 11:42
  • @blue_note yes, it is misleading, use side-effect and it is not prefered. But it is good to know it when you see this in `lambda` or you want to create short one-liner to run it fast directly in command line :) – furas Apr 08 '19 at 12:02
1
if 1: print("hello") or print("end")
hello
end

More information on the use of or: Strange use of "and" / "or" operator


if 1: print("hello"); print("end")
hello
end
cs95
  • 379,657
  • 97
  • 704
  • 746
0
print("hello" if 1 else 'end')

Check this for more details

NoorJafri
  • 1,787
  • 16
  • 27