Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #54293
| From | Roy Smith <roy@panix.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Having both if() and for() statements in one liner |
| Date | 2013-09-17 09:00 -0400 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <roy-7F8454.09003417092013@news.panix.com> (permalink) |
| References | <l19gdf$psh$1@dont-email.me> |
In article <l19gdf$psh$1@dont-email.me>,
Ferrous Cranus <nikos.gr33k@gmail.com> wrote:
> 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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Having both if() and for() statements in one liner Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-09-17 15:02 +0300
Re: Having both if() and for() statements in one liner Robert Kern <robert.kern@gmail.com> - 2013-09-17 13:52 +0100
Re: Having both if() and for() statements in one liner Roy Smith <roy@panix.com> - 2013-09-17 09:00 -0400
Re: Having both if() and for() statements in one liner Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-09-17 16:21 +0300
Re: Having both if() and for() statements in one liner Heiko Wundram <modelnine@modelnine.org> - 2013-09-17 15:46 +0200
Re: Having both if() and for() statements in one liner Tim Chase <python.list@tim.thechases.com> - 2013-09-17 09:17 -0500
Re: Having both if() and for() statements in one liner Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-17 10:32 -0400
Re: Having both if() and for() statements in one liner Dave Angel <davea@davea.name> - 2013-09-17 18:54 +0000
Re: Having both if() and for() statements in one liner Steven D'Aprano <steve@pearwood.info> - 2013-09-18 02:10 +0000
Re: Having both if() and for() statements in one liner Joshua Landau <joshua@landau.ws> - 2013-09-18 06:55 +0100
Re: Having both if() and for() statements in one liner Chris Angelico <rosuav@gmail.com> - 2013-09-18 17:00 +1000
Re: Having both if() and for() statements in one liner Chris Angelico <rosuav@gmail.com> - 2013-09-18 01:04 +1000
Re: Having both if() and for() statements in one liner Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-17 11:15 -0400
csiph-web