Re: Embarrasing questio

2009-02-13 Thread Steven D'Aprano
TechieInsights wrote:

 On Feb 12, 9:03 am, Catherine Heathcote
 catherine.heathc...@gmail.com wrote:
 But I just cant find it. How do I do an or, as in c/c++'s ||? Just
 trying to do something simple, the python equivilent of:

 if(i % 3 == 0 || i % 5 == 0)

 Thanks.
 
 in 2.5 and above you can do
 if any(i%3 == 0, i%5 == 0)

[nitpick: you need an additional set of brackets inside the call to any]

I love it! A totally unnecessary creation of a tuple and a function call
instead of a straight-forward 'or' expression. Can we make this simple task
even more obfuscated?

not all( (i%3 != 0, i%5 !=0) )

Still not convoluted enough. 

all( (sum(int(c) for c in str(i))%3 in (1, 2), 
str(i).endswith(chr(53)) - 1, 
str(i).endswith(chr(48)) - 1) ) - 1

Hmmm... how can I get rid of that pesky %3?

all( (divmod( sum(int(c) for c in str(i)), 
41^42)[1] in (1, 2), 
str(i).endswith(chr(53)) - 1, 
str(i).endswith(chr(48)) - 1) ) - 1

Okay, that makes my brain hurt. I think it will do.

:-)



-- 
Steven

--
http://mail.python.org/mailman/listinfo/python-list


Re: Embarrasing questio

2009-02-12 Thread TechieInsights
On Feb 12, 9:03 am, Catherine Heathcote
catherine.heathc...@gmail.com wrote:
 But I just cant find it. How do I do an or, as in c/c++'s ||? Just
 trying to do something simple, the python equivilent of:

 if(i % 3 == 0 || i % 5 == 0)

 Thanks.

if i % 3  == 0 or i % 5 == 0

--
http://mail.python.org/mailman/listinfo/python-list


Re: Embarrasing questio

2009-02-12 Thread TechieInsights
On Feb 12, 9:03 am, Catherine Heathcote
catherine.heathc...@gmail.com wrote:
 But I just cant find it. How do I do an or, as in c/c++'s ||? Just
 trying to do something simple, the python equivilent of:

 if(i % 3 == 0 || i % 5 == 0)

 Thanks.

in 2.5 and above you can do
if any(i%3 == 0, i%5 == 0)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Embarrasing questio

2009-02-12 Thread Aahz
In article zkxkl.34048$sp5.7...@text.news.virginmedia.com,
Catherine Heathcote  catherine.heathc...@gmail.com wrote:

But I just cant find it. How do I do an or, as in c/c++'s ||? Just 
trying to do something simple, the python equivilent of:

if(i % 3 == 0 || i % 5 == 0)

if i % 3 == 0 or i % 5 == 0:

You may find it worthwhile to quickly step through everything in the
standard Python tutorial, it covers lots of stuff like this.
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Embarrasing questio

2009-02-12 Thread Catherine Heathcote

TechieInsights wrote:

On Feb 12, 9:03 am, Catherine Heathcote
catherine.heathc...@gmail.com wrote:

But I just cant find it. How do I do an or, as in c/c++'s ||? Just
trying to do something simple, the python equivilent of:

if(i % 3 == 0 || i % 5 == 0)

Thanks.


if i % 3  == 0 or i % 5 == 0



Yea, new it would be embarrasing, thanks!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Embarrasing questio

2009-02-12 Thread Michele Simionato
On Feb 12, 5:07 pm, TechieInsights gdoerm...@gmail.com wrote:
 On Feb 12, 9:03 am, Catherine Heathcote

 catherine.heathc...@gmail.com wrote:
  But I just cant find it. How do I do an or, as in c/c++'s ||? Just
  trying to do something simple, the python equivilent of:

  if(i % 3 == 0 || i % 5 == 0)

  Thanks.

 in 2.5 and above you can do
 if any(i%3 == 0, i%5 == 0)

You are missing a few parenthesis: if any([i%3 == 0, i%5 == 0]) (but
the idiomatic solution is to use or).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Embarrasing questio

2009-02-12 Thread km
Hi,

you could do it this way also :

if i in [3,5]:
do something...

KM
~
On Fri, Feb 13, 2009 at 1:19 AM, Michele Simionato 
michele.simion...@gmail.com wrote:

 On Feb 12, 5:07 pm, TechieInsights gdoerm...@gmail.com wrote:
  On Feb 12, 9:03 am, Catherine Heathcote
 
  catherine.heathc...@gmail.com wrote:
   But I just cant find it. How do I do an or, as in c/c++'s ||? Just
   trying to do something simple, the python equivilent of:
 
   if(i % 3 == 0 || i % 5 == 0)
 
   Thanks.
 
  in 2.5 and above you can do
  if any(i%3 == 0, i%5 == 0)

 You are missing a few parenthesis: if any([i%3 == 0, i%5 == 0]) (but
 the idiomatic solution is to use or).
 --
 http://mail.python.org/mailman/listinfo/python-list

--
http://mail.python.org/mailman/listinfo/python-list


Re: Embarrasing questio

2009-02-12 Thread Catherine Heathcote

Aahz wrote:

In article zkxkl.34048$sp5.7...@text.news.virginmedia.com,
Catherine Heathcote  catherine.heathc...@gmail.com wrote:
But I just cant find it. How do I do an or, as in c/c++'s ||? Just 
trying to do something simple, the python equivilent of:


if(i % 3 == 0 || i % 5 == 0)


if i % 3 == 0 or i % 5 == 0:

You may find it worthwhile to quickly step through everything in the
standard Python tutorial, it covers lots of stuff like this.


I did, though perhapse a little to quickly! lol
--
http://mail.python.org/mailman/listinfo/python-list


Re: Embarrasing questio

2009-02-12 Thread Tim Rowe
2009/2/12 km srikrishnamo...@gmail.com:
 Hi,

 you could do it this way also :

 if i in [3,5]:
 do something...

True, you could do it, but it would be wrong. The original is true for
i = 6, 9, 10, 12 and so on, but yours doesn't seem to be...

-- 
Tim Rowe
--
http://mail.python.org/mailman/listinfo/python-list


Re: Embarrasing questio

2009-02-12 Thread TechieInsights
On Feb 12, 9:19 am, Michele Simionato michele.simion...@gmail.com
wrote:
 On Feb 12, 5:07 pm, TechieInsights gdoerm...@gmail.com wrote:

  On Feb 12, 9:03 am, Catherine Heathcote

  catherine.heathc...@gmail.com wrote:
   But I just cant find it. How do I do an or, as in c/c++'s ||? Just
   trying to do something simple, the python equivilent of:

   if(i % 3 == 0 || i % 5 == 0)

   Thanks.

  in 2.5 and above you can do
  if any(i%3 == 0, i%5 == 0)

 You are missing a few parenthesis: if any([i%3 == 0, i%5 == 0]) (but
 the idiomatic solution is to use or).

Thanks... my mistake
--
http://mail.python.org/mailman/listinfo/python-list


Re: Embarrasing questio

2009-02-12 Thread MRAB

Michele Simionato wrote:

On Feb 12, 5:07 pm, TechieInsights gdoerm...@gmail.com wrote:

On Feb 12, 9:03 am, Catherine Heathcote

catherine.heathc...@gmail.com wrote:

But I just cant find it. How do I do an or, as in c/c++'s ||? Just
trying to do something simple, the python equivilent of:
if(i % 3 == 0 || i % 5 == 0)
Thanks.

in 2.5 and above you can do
if any(i%3 == 0, i%5 == 0)


You are missing a few parenthesis: if any([i%3 == 0, i%5 == 0]) (but
the idiomatic solution is to use or).


any() is really only useful with a generator, otherwise you're always
evaluating both conditions, unlike the solution with 'or'.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Embarrasing questio

2009-02-12 Thread Michele Simionato
On Feb 12, 6:22 pm, MRAB goo...@mrabarnett.plus.com wrote:
 Michele Simionato wrote:
  On Feb 12, 5:07 pm, TechieInsights gdoerm...@gmail.com wrote:
  On Feb 12, 9:03 am, Catherine Heathcote

  catherine.heathc...@gmail.com wrote:
  But I just cant find it. How do I do an or, as in c/c++'s ||? Just
  trying to do something simple, the python equivilent of:
  if(i % 3 == 0 || i % 5 == 0)
  Thanks.
  in 2.5 and above you can do
  if any(i%3 == 0, i%5 == 0)

  You are missing a few parenthesis: if any([i%3 == 0, i%5 == 0]) (but
  the idiomatic solution is to use or).

 any() is really only useful with a generator, otherwise you're always
 evaluating both conditions, unlike the solution with 'or'.

Indeed.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Embarrasing questio

2009-02-12 Thread Hrvoje Niksic
Michele Simionato michele.simion...@gmail.com writes:

 On Feb 12, 6:22 pm, MRAB goo...@mrabarnett.plus.com wrote:
 Michele Simionato wrote:
  On Feb 12, 5:07 pm, TechieInsights gdoerm...@gmail.com wrote:
  On Feb 12, 9:03 am, Catherine Heathcote

  catherine.heathc...@gmail.com wrote:
  But I just cant find it. How do I do an or, as in c/c++'s ||? Just
  trying to do something simple, the python equivilent of:
  if(i % 3 == 0 || i % 5 == 0)
  Thanks.
  in 2.5 and above you can do
  if any(i%3 == 0, i%5 == 0)

  You are missing a few parenthesis: if any([i%3 == 0, i%5 == 0]) (but
  the idiomatic solution is to use or).

 any() is really only useful with a generator, otherwise you're always
 evaluating both conditions, unlike the solution with 'or'.

 Indeed.

any(f() for f in (lambda: i % 3 == 0, lambda: i % 5 == 0))

:-)
--
http://mail.python.org/mailman/listinfo/python-list