3

Someone came with this example to me (python2):

num = int(input("num")) 
den = int(input("den"))

quot = 0
rest = den
i = num

for i in range(i,i>den, -den):
  quot = quot + 1
  rest = i - den

print quot
print rest     

The code runs fine, does what it needs to be doing, and doesn't produce errors.

I don't understand why. To me, range() requires a lower and an upper limit, and to me, i would be the lower value, while i>den should evaluate to a boolean?

The context is a tutorial function which implements division with a for loop.

5
  • can you add a bit more of the surrounding code? as written we don't know all the preconditions (like the initial values of i and num). Commented Mar 9, 2015 at 1:33
  • You can have a condition in a for loop but not a range function. Commented Mar 9, 2015 at 1:34
  • @MalikBrahimi No, you can, because True evaluates to 1 and False to 0. But it certainly wouldn't behave like a C-like's for statement. It's just setting the stop point of iteration to either 0 or 1. Commented Mar 9, 2015 at 1:35
  • Yes, I meant for the intended purpose. Commented Mar 9, 2015 at 1:36
  • 1
    Looks to me that whoever wrote this code got Python for-loop syntax mixed up with the for-loop syntax in C, Java, JS, etc. Even if it is technically correct, it is absolutely not pythonic. Commented Mar 9, 2015 at 1:50

2 Answers 2

3

This is an application of "duck typing". In this case, i>num evaluates to a boolean value of True or False, but in the context of the range function, True is equivalent to the integer 1, and False is equivalent to the integer 0.

So, for example, if i>num is False, then that code is equivalent to

for i in range(i, 0, -num):
  #do stuff
Sign up to request clarification or add additional context in comments.

10 Comments

Having said that, there is almost certainly a more clear way to write out the conditions for the iteration than using a clever trick like this, which in Python 2.X is technically relying on an implementation detail.
For sure - it's a weird bit of code, and not one I'd care to debug or maintain.
Gah, seeing the OP's update it's even crazier, since i = num entering the loop so it will always be False. WTF?
The update opened my eyes: as i gets initialized to num, then the condition i > num evaluates to True. The range(i, True) evaluates to for example (16, 1), which is an empty range []. BUT, as we have the step change -dev, if dev is 2 for example, the range results in [16, 14, 12, 10, 8, 6, 4, 2] and that's why the code does what it needs to do!
@aruisdante What implementation detail?
|
2

This is because Python considers True == 1 and False == 0.

See here for more information: Is False == 0 and True == 1 in Python an implementation detail or is it guaranteed by the language?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.