Embarrasing questio

  • Thread starter Catherine Heathcote
  • Start date
C

Catherine Heathcote

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.
 
T

TechieInsights

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
 
T

TechieInsights

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)
 
A

Aahz

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.
 
M

Michele Simionato

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).
 
C

Catherine Heathcote

Aahz said:
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
 
T

Tim Rowe

2009/2/12 km said:
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...
 
M

MRAB

Michele said:
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'.
 
M

Michele Simionato

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.
 
H

Hrvoje Niksic

Michele Simionato said:
Michele said:
On Feb 12, 9:03 am, Catherine Heathcote
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))

:)
 
S

Steven D'Aprano

TechieInsights said:
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.

:)
 
M

marika

But I just cant find it.

Sorry to hear about it. Gosh that’s a tough one to weather. My
prayers will be going out for you.

mk5000

" In the future my son will lead mankind in a war against Skynet, a
computer system designed to destroy the world. It has sent machines
back through time to kill him, one to protect him. Today we fight to
stop Skynet from ever being created. To change our future. To change
his fate. The war to save mankind begins now!"--sarah connor
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top