Take the 2-minute tour ×

I'm trying to learn Python from a background in javascript. I saw someone make a recursive function to find the least common denominator and wondered why they didn't just use a loop, so, both for the experience and to amuse myself, I wrote a simpler one:

I came up with:

def LCM(n,d):
    while(n%d++ != 0 ):
    return d-1

Needless to say, for those of you that know Python, ++ isn't a valid operator. I also tried

def LCM(n,d):
    while(n%(d+=1) != 0 ):
    return d-1

To make sure it wasn't my thinking that was off, I tried the same thing in javascript:

function LCM(b,d){
   while(b%d++ != 0){
return d-1;

So does Python not allow expressions like in javascript? Also, is indentation the only way to define something? I know semicolons aren't required but can be used, is there anything like that in terms of closing a loop or function definition?

Finally, are is and is not the Python equality-without-type-coersion operator?

P.S. I realize the function isn't practical without checking the input for various things, but that wasn't the point in writing it.

P.P.S Also, is there a Python equivalent of the javascript evaluation ? on true : on false if statement abbreviation?

share|improve this question
avoid recursive functions. –  JBernardo Sep 14 '11 at 16:55
Well, in this case, it wasn't necessary, but for something like listing file trees, it would be. –  mowwwalker Sep 14 '11 at 16:56
Why? Python is perfectly capable of doing recursion and that is the best solution for some problems. –  Tom Zych Sep 14 '11 at 16:56
@tom-zychThank, thanks. I'd love to hear an explanation of why he thinks that though. –  mowwwalker Sep 14 '11 at 16:58
If you want to know about how Python does coercion, read this docs.python.org/reference/datamodel.html#coercion-rules –  dtanders Sep 14 '11 at 17:10
show 2 more comments

2 Answers

up vote 8 down vote accepted

Python does not allow assignments (such as i+=1) in expressions since those can lead to confusing code, and Python is designed to make it hard to write confusing code, and make it simple to write obvious code.

You can simply write this:

def LCM(n,d):
    while n%d != 0:
        d += 1
    return d-1

Python's is tests for two objects being the same object instead of just equal. Consider the following:

d = {}
e = d
assert d == {}     # Empty dictionaries equal each other
assert d is not {} # .. but are not identical
assert d is e      # d and e refer to the same object

There is no equivalent operator to is in JavaScript, and there is no equivalent to JavaScript's == in Python. Python's == does type checking for the built-in types.

The conditional operator (a ? b : c in JavaScript) is written out in Python as:

 b if a else c
share|improve this answer
Thanks, way to get most of my questions in one go :D. So Python's == doesn't allow for type coersion anyway? Also, what about statement closure? Is it only indentation? Also, are semicolons just optional, or are you NOT supposed to use them? –  mowwwalker Sep 14 '11 at 17:03
@Walkerneo Python does no type coercion by default, but you can overwrite the behavior of nearly all operators if you desire to do so. There are no braces (try from __future__ import braces) in Python, it's all indentation. Semicolons are a hack for command-line programs and should not occur at all in a Python program. –  phihag Sep 14 '11 at 17:07
@Walkerneo: (1) Try it out. See docs.python.org/library/__future__.html#module-future for __future__ in general. (2) Because booleans aren't the only things that have a truth value. Nonzero numbers are considered true. In fact, the booleans are basically the integers 1 and 0 with a prettified string representation. Similarily, collections are false if empty and true otherwise. –  delnan Sep 14 '11 at 17:12
@Walkerneo: Well, under the hood if does ask the object in question to create a boolean that represents its truth value, and you can do the same conversion explicitly with the bool function - I guess it could be thought of as implicit conversion. But conceptually, it's not that the object is being converted to a boolean, it's the object itself having a truth value. (And your remark on binary representation doesn't make sense.) –  delnan Sep 14 '11 at 17:19
@Walker No, there is a True (with a capitaqlized T, for whatever reason), but True is not 1. Instead, True is an instance of the class bool, which is a subclass of int. –  phihag Sep 14 '11 at 17:46
show 11 more comments

There's no need for semicolons in Python, everything is done with indentation. Also, mixing up tabs and spaces can lead to problems. Stick to one or the other. :)

share|improve this answer
That's my fear though. I don't feel comfortable without my semicolons and closing braces. –  mowwwalker Sep 14 '11 at 17:08
Stick to 4-space indention python.org/dev/peps/pep-0008 –  tMC Sep 14 '11 at 17:09
@Walkerneo: Then get used to it. It's just aesthetics, and for most even a matter of habit. –  delnan Sep 14 '11 at 17:10
@Walkerneo or just use a editor that indents consistently. –  Federico Culloca Sep 14 '11 at 17:15
add comment

Your Answer


