21
votes

In JavaScript, one could do this:

if (integer > 3 && integer < 34){
    document.write("Something")
}

Is this possible in Python?

6

6 Answers

57
votes

Python indeed allows you to do such a thing

if integer > 3 and integer < 34

Python is also smart enough to handle:

if 3 < integer < 34:
    # do your stuff
13
votes

Python replaces the usual C-style boolean operators (&&, ||, !) with words: and, or, and not respectively.

So you can do things like:

if (isLarge and isHappy) or (isSmall and not isBlue):

which makes things more readable.

11
votes

Just on formatting. If you have very long conditions, I like this way of formatting

if (isLarge and isHappy) \
or (isSmall and not isBlue):
     pass

It fits in nicely with Python's comb formatting

5
votes
if integer > 3 and integer < 34:
    # do work
3
votes

yes like this:

if 3 < integer < 34:
    pass
1
votes

Yes, it is:

if integer > 3 and integer < 34:
   document.write("something")