Sunday, February 28, 2010

Multiple Condition If Statements

Just a quick little note for when you are doing multiple condition if statements. Python includes AND and OR, but not && and ||. Although, in Python they are lowercase. This had me going for awhile. In PHP, some if statements.
if( $var1 > 50 && $var1 < 100 ){
echo 'hello world';
}
if( $var1 == 3 || $var1 == 7 ){
echo 'hello world';
}
And in Python.
if var1 > 50 and var1 < 100:
print('hello world')

if var1 == 3 or var1 == 7:
print('hello world')

3 comments:

  1. Just a quick note: "and" in Python acts like "&&" in PHP, not like "AND". Same goes for "or": It behaves like "||", not like "OR". That can trip you up if you're going from one to the other.

    ReplyDelete
  2. Just a side note about comparisons.
    In Python, the following is legal:
    if 50 < var1 < 100
    This is the same as:
    if var1 > 50 and var1 < 100

    ReplyDelete
  3. You might want to rethink using Python 3 specific syntax when Django (which your site is focused on) doesn't even support Python 3 yet and probably won't for quite some time.

    ReplyDelete