Check whether a number is positive, negative, or zero I have a program which generates a number between 0 and 4 for both the player and the computer.  If the number is positive the player wins, and if it is negative the computer wins.

if dif == 1 or dif == 2 or dif == 3 or dif == 4:
    print 'Player wins!'
elif dif == -1 or dif == -2 or dif == -3 or dif == -4:
    print 'Computer wins!'    
else:
    print 'Player and computer tie!'


How can I make the if and elif statments simpler?
We should use the comparison operators.  Greater than, less than.

if dif &gt; 0:
    print 'Player wins!'
elif dif &lt; 0:
    print 'Computer wins!'
else:
    print 'Player and computer tie!'


Another option, which I'm led to believe is "more Pythonic" (although, I'm not a Python dev, so don't take my word for it...), looks like this:

def tie():
    print 'Player and computer tie!'

def player():
    print 'Player wins!'

def computer():
    print 'Computer wins!'

options = {
     0 : tie,
     1 : player,
     2 : player,
     3 : player,
     4 : player,
    -1 : computer,
    -2 : computer,
    -3 : computer,
    -4 : computer
}

options[dif]()


If explicit values are important, than this solution is probably good.  However, if you really only care whether a value is greater than, less than, or equal to a specific value, than a simple if/elif/else as I first demonstrated is a good option.

Note that this bottom option will run into a KeyError if dif is not in the range of expected values.  The if/elif/else will work for any number.