views:

292

answers:

6

In Python, if I had a range, and I wanted to iterate over it and divide each number by another number, could I do that in a if statement.

a = range(20)
for i in a:
   if i / 3 == True:
      print i
+7  A: 

Yes, but.

Please, please, please. Never say if some expression == True. It's redundant and causes many people to wonder what you're thinking.

More importantly.

i/3 is the quotient.

i%3 is the remainder. If i is a multiple of 3, i%3 == 0.

S.Lott
Try to avoid "if some expression == False" as well.
Brian
actually, "if some expression == True" makes it very clear that you really want the expression to be true, and that you didn't just absent-mindedly omit the comparison. just like putting "o = null" in your constructor makes it clear that you really want the variable to be null, and that you didn't just forget to initialize it.there's literally no downside to being more clear.
Christopher
@ Christopher: That's wrong. If you are able to do x == True, then you be able to use if y == False, which isn't possible in languages where False is implemented as 0.
Georg
There's a difference between "explicit" and "redundant". In the case of `if expr == True`, we've crossed the line. The `if expression:` syntax means True, and only True in Python. (Other languages aren't part of this question).
S.Lott
In this single case True is not redundant is it equivalent to i /3 == 1 but this is obviously not the user intention
Nadia Alramli
@Nadia: Correct -- my assumption is that the user had a different intent, in which case the True was redundant. In the unlikely event they actually meant for integer division to produce a boolean result, then... well... That's possibly even worse than a `if expression == True:`
S.Lott
A: 

Hmm seems you want a weird thing - you divide i by 3 and check if it is equal to 1. As 4 == True => False.

Anton Kazennikov
A: 

Short answer: no. You cannot make assignments in if statements in python.

But really, I don't understand what you are trying to do here. Your sample code will only print out the numbers 3, 4, and 5 because every other value of i divided by 3 evaluates to something other than 1 (and therefore false).

If you want to divide everything in a list by 3, you want map(lambda x: x / 3, range(20)). if you want decimal answers, map(lambda x : x / 3.0, range(20)). These will return a new list where each element is a the number in the original list divided by three.

Thomas Cowart
+2  A: 

At the command prompt:

>>> [i for i in range(20) if i%3 == 0]
>>> [0, 3, 6, 9, 12, 15, 18]

OR

>>> a = [i for i in range(20) if i%3 == 0]
>>> print a
[0, 3, 6, 9, 12, 15, 18]
>>>
Jeff
Just to clarify what you did there, read it all into a list and divided them all with a remainder, and if the remainder = 0, added them to the list, right?
Kevin
Exactly. The piece within the "[...]" is called a "list comprehension"...if you want some more information, check out section 5.1.4 of http://docs.python.org/tutorial/datastructures.html
Jeff
A: 

While working on Project Euler myself, I found "if not x % y" to be the cleanest way to represent "if x is a multiple of y". This is equivalent to "if x % y == 0", seen in other answers. I don't believe there is a significant difference between the two; which you use is simply a matter of personal preference.

Ben Blank
+2  A: 

Everyone here has done a good job explaining how to do it right. I just want to explain what you are doing wrong.

if i / 3 == True

Is equivalent to:

if i / 3 == 1

Because True == 1. So you are basicly checking if i when divided by 3 equals 1. Your code will actually print 3 4 5.

I think what you wanted to do is to check if i is a multiple of 3. Like this:

if i % 3 == 0

You can of course use an if statement to do it. Or you can use list comprehension with if

[x for x in range(20) if x % 3 == 0]


To those how are down voting this, from python documentation:

Boolean values are the two constant objects False and True. They are used to represent truth values (although other values can also be considered false or true). In numeric contexts (for example when used as the argument to an arithmetic operator), they behave like the integers 0 and 1, respectively.

Nadia Alramli
All non-zero values are True. i/3 will be False for i < 3 and True for all others. In a version of Python that uses the "classical" division operator. If you are using future division in Python 3, then i/3 will be false only when i == 0.
S.Lott
@S.Lott, that's right all non-zero values are True, but True equals 1. Try True + 3 you'll get 4. Kevin used print not print() in his sample. That's why I'm assuming this is a "classical" division operator. I'm just trying to lay it down simply. Kevin is obviously new to programming.
Nadia Alramli
@S.Lott, to proof my point try to evaluate 5 / 2 == True and you'll see that the result will actually be False not True
Nadia Alramli