Having both if() and for() statements in one liner

F

Ferrous Cranus

o want to avoid having to type somehting like this:

if person="George":
times in range(0, 5):


Why it gives me an error when i'm trying to write it like this:


if person="George" for times in range(0, 5):

Can't i ahve both if and for in a one liner?
 
R

Robert Kern

o want to avoid having to type somehting like this:

if person="George":
times in range(0, 5):


Why it gives me an error when i'm trying to write it like this:


if person="George" for times in range(0, 5):

Can't i ahve both if and for in a one liner?

Not in Python, no.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
R

Roy Smith

Ferrous Cranus said:
o want to avoid having to type somehting like this:

if person="George":
times in range(0, 5):


Why it gives me an error when i'm trying to write it like this:


if person="George" for times in range(0, 5):

Step One when reporting a problem is don't just tell us you got an
error. Tell us what the error is. Cut and paste the exact text of the
full error message.

Although, in this case, it's pretty easy to guess that it was a syntax
error :)

I'm not sure where to start. First, the '=' in 'person="George"' should
be '=='. In Python, '=' is used for assignment, '==' is used for
equality testing.

Next, if you want to use the 1-line version of 'if', you need a ':'
after the condition. Something like:

if person == 'George': print 'foo'

but it's generally considered poor style to use 1-line if statements.
Just write it on two lines:

if person == 'George':
print 'foo'

They just discovered a huge newline vein in Montana and they're mining
the things like crazy. There's no shortage of them so feel free to use
as many as you like. They even get recycled.

But, I'm not even sure you can put a 'for' statement as the body of a
1-line 'if'. I've never tried it before, and my one experiment now got
me a syntax error. Even if it turns out to be legal and I just haven't
got the details right, it's just The Wrong Thing To Do.
 
F

Ferrous Cranus

Στις 17/9/2013 4:00 μμ, ο/η Roy Smith έγÏαψε:
Step One when reporting a problem is don't just tell us you got an
error. Tell us what the error is. Cut and paste the exact text of the
full error message.

Although, in this case, it's pretty easy to guess that it was a syntax
error :)

I'm not sure where to start. First, the '=' in 'person="George"' should
be '=='. In Python, '=' is used for assignment, '==' is used for
equality testing.

Next, if you want to use the 1-line version of 'if', you need a ':'
after the condition. Something like:

if person == 'George': print 'foo'

but it's generally considered poor style to use 1-line if statements.
Just write it on two lines:

if person == 'George':
print 'foo'

They just discovered a huge newline vein in Montana and they're mining
the things like crazy. There's no shortage of them so feel free to use
as many as you like. They even get recycled.

But, I'm not even sure you can put a 'for' statement as the body of a
1-line 'if'. I've never tried it before, and my one experiment now got
me a syntax error. Even if it turns out to be legal and I just haven't
got the details right, it's just The Wrong Thing To Do.


I just want to say tot he program that

that only run the for statement if and only if person=='George'

I dont see nay reason as to why this fails

perhaps like:

for times in range(0, 5) if person=='George':

but that fails too...
there must be written on soem way.
 
H

Heiko Wundram

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 17.09.2013 15:21, schrieb Ferrous Cranus:
... there must be written on soem way.

You've already given yourself the answer in the initial post. The
Python way to write this is:

if person == "George":
for times in range(5):
...

Why not just use what works and get some actual work done?

- --
- --- Heiko.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.20 (MingW32)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSOF0gAAoJEDMqpHf921/Sv0oH/AyuaOk5sFlx4j7CKzv4Bb9i
+REyAtLJXpgcziviFXjIbnPsNLtGqMU6yOgp9OV7LGwfn0mnZtmI+SoYp08t7G9U
3WSMC6BOCugg419EEMmf+Gkf4fWvv/aZYWBTd8MhyiJLsQ9R7Sg9LlGYheDQ6m+S
RwWpYSHYCaJu3iy2xBJ+8AqQjOqACcMREtW1Rt1uHiydO93Dn2Abm0XLq11psYeR
OV3sftEJ2EpMEcR4I/HLx95KWIh7wvQcZywTF9y+pe1uOnLrKW/1NdkUxNdkMofy
RBNOjYJjT9JAnB2UHI1wVtbipwSi4A4zIIYsE6exv4s1IjnInrVERdDOOlqjwzQ=
=rxPO
-----END PGP SIGNATURE-----
 
T

Tim Chase

I just want to say tot he program that

that only run the for statement if and only if person=='George'

I dont see nay reason as to why this fails

perhaps like:

for times in range(0, 5) if person=='George':

but that fails too...
there must be written on soem way.

The canonical way to do this is the obvious:

if person == "George":
for times in range(0, 5):
...

That said, you can do stupid things to abstract the logic like

def iterate_if(condition, iterable):
if condition:
for item in iterable:
yield item

which you can use something like

for times in iterate_if(person == "George", range(0,5)):
...

but I don't advise it. Mainly, because the iterable will be
evaluated when passed as an argument, which incurs the runtime cost.
In the canonical form, if the test isn't passed, the range(n,m) is
never even evaluated.

-tkc
 
J

Joel Goldstick

The canonical way to do this is the obvious:

if person == "George":
for times in range(0, 5):
...

That said, you can do stupid things to abstract the logic like

def iterate_if(condition, iterable):
if condition:
for item in iterable:
yield item

which you can use something like

for times in iterate_if(person == "George", range(0,5)):
...

but I don't advise it. Mainly, because the iterable will be
evaluated when passed as an argument, which incurs the runtime cost.
In the canonical form, if the test isn't passed, the range(n,m) is
never even evaluated.

-tkc


Tim, that's great! or in the wonderful world of Onslow, "Oh nice" http://en.wikipedia.org/wiki/Onslow_(Keeping_Up_Appearances)
 
D

Dave Angel

I just want to say tot he program that

that only run the for statement if and only if person=='George'

I dont see nay reason as to why this fails

perhaps like:

for times in range(0, 5) if person=='George':

but that fails too...
there must be written on soem way.

untested:

for times in range(0, 5 if person=="George" else 0):

But i also greately prefer the canonical version.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top