Can you make multiple “if” conditions in Python?

In JavaScript, one could do this:

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

Is this possible in Python?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.

Method 3

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

Method 4

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

Method 5

yes like this:

if 3 < integer < 34:
    pass

Method 6

Yes, it is:

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x