Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you make multiple "if" conditions in Python? [duplicate]

Tags:

In JavaScript, one could do this:

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

Is this possible in Python?

like image 535
please delete me Avatar asked Oct 18 '11 15:10

please delete me


People also ask

Can there be 2 if statements in Python?

It works that way in real life, and it works that way in Python. if statements can be nested within other if statements. This can actually be done indefinitely, and it doesn't matter where they are nested. You could put a second if within the initial if .

How do you repeat a condition in Python?

Assign a bool value to a variable called playing, and then use that as the loop condition. So you would have; playing = True while playing: choice = input("would you like to play again? y/n: ") if choice == "n": print "Thanks for playing" playing = False else: print "play again.. etc..."


2 Answers

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 
like image 148
Nico Avatar answered Nov 08 '22 18:11

Nico


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.

like image 34
andronikus Avatar answered Nov 08 '22 17:11

andronikus