How to Put Multiple Statements in One Line

How can I put multiple statements in one line?

Unfortunately, what you want is not possible with Python (which makes Python close to useless for command-line one-liner programs). Even explicit use of parentheses does not avoid the syntax exception. You can get away with a sequence of simple statements, separated by semicolon:

for i in range(10): print "foo"; print "bar"

But as soon as you add a construct that introduces an indented block (like if), you need the line break. Also,

for i in range(10): print "i equals 9" if i==9 else None

is legal and might approximate what you want.

If you are still determined to use one-liners, see the answer by elecprog.

As for the try ... except thing: It would be totally useless without the except. try says "I want to run this code, but it might throw an exception". If you don't care about the exception, leave out the try. But as soon as you put it in, you're saying "I want to handle a potential exception". The pass then says you wish to not handle it specifically. But that means your code will continue running, which it wouldn't otherwise.

How can I put multiple statements in one line in python?without using ; and exec

Done.

print(' '.join(sorted([letter if (ord(letter) -97) % 2 == 0 else letter.upper() for letter in str(input())],reverse=True)))

Can i include multiple statements when creating a one-line for loop?

You can use a string comprehension:

numbers = [float(p.replace('€','').replace('M','')) for p in a]

which gives:

[110.5, 210.5, 310.5]

Flake 8: multiple statements on one line (colon) only for variable name starting with if

Edit: The problem is in pycodestyle (pep8), which is called by flake8. The rest still stands.

Second edit: I've made some more research and the issue is fixed here. The fix hasn't been released yet, though.

Definitely looks like a flake8 bug to me:

flakebug.py:

innocuous: str = ""
ifstarting_string: str = ""
forfalse_positivetoo: str = ""
whilethis_lookslikeabug: str = ""
elsehaha: str = ""

In the shell:

$ # python3.6 -m pycodestyle flakebug.py gives the same results
$ python3.6 -m flake8 flakebug.py
flakebug.py:2:18: E701 multiple statements on one line (colon)
flakebug.py:3:21: E701 multiple statements on one line (colon)
flakebug.py:4:24: E701 multiple statements on one line (colon)
flakebug.py:5:9: E701 multiple statements on one line (colon)

Looks like every line starting with a control flow statement triggers it.
I'd wager it uses a regex like (if|else|while|for).*:.

I'll try to get to the bottom of this and update this answer if I can, meanwhile you could add some # noqa annotations and you'll be set :)

Ruby multiple statements on single line with condition

You could use :

string = "dasff1"

case string
when "dasff1" then print "dasff1"; print " hoiff1"
when "dasff2" then print "dasff2"; print " hoiff2"
when "dasff3" then print "dasff3"; print " hoiff3"
end

Or just :

print string+" "+string.sub('das','hoi') if string=~/^dasff[1-3]$/

You shouldn't do that, but since you asked for it :

if    string == "dasff1" then print "dasff1" ; print " hoiff1"
elsif string == "dasff2" then print "dasff2" ; print " hoiff2"
elsif string == "dasff3" then print "dasff3" ; print " hoiff3"
end


Related Topics



Leave a reply



Submit