2

I am new to python and I'm not sure about the rules of the if condition:

Condition 1:

if 0:
   Print "true"

output:

Condition 2:

if -1:
   Print "true"

output: true

Condition 3:

if 1:
   Print "true"

output: true

I will explain my code from,

Condition 1: It didn't print the statement, because it treated as false(boolean)

Condition 2: It print the statement, because it treated as true(boolean)

Why It is takes true, It is negative value(my assumption it takes only false) ?

Condition 3: It print the statement, because it treated as true(boolean).

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83

5 Answers5

5

It's as simple as the following:

Conditions in Python if statements (and many other languages) are converted to booleans, no matter what the type it is usually. This is done using a defined mechanism which is specifies whether an argument is True or False for different values given.

In Python, arguments are converted to booleans as follows (paraphrased slightly):

4.1 Truth Value Testing

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. If the argument is one of the following:

  • None
  • False
  • The number 0 represented as any numeric type (integer, float, long, complex)
  • Any empty sequence ( '', (), [] )
  • Any empty map ( {} )
  • an instance of an user-defined class, if the class defines a __bool__() or __len__() method, when that method returns the integer zero or bool value False.

Then it is False. Otherwise, it is True.

As -1 is not falsy, it is therefore truthy and therefore runs the if statement, even though it is negative.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
2

What you're probably looking for:

Truth Values in Python: http://docs.python.org/2.4/lib/truth.html

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

None

False

zero of any numeric type, for example, 0, 0L, 0.0, 0j.

any empty sequence, for example, '', (), [].

any empty mapping, for example, {}.

instances of user-defined classes, if the class defines a nonzero() or len() method, when that method returns the integer zero or bool value False.2.5

All other values are considered true -- so objects of many types are always true.

aIKid
  • 26,968
  • 4
  • 39
  • 65
LeShadow
  • 61
  • 4
  • Even if the content seems to be the same, linking [here](http://docs.python.org/2/library/stdtypes.html#truth-value-testing) seems better to me. – glglgl Oct 28 '13 at 11:15
1

Just because the number is negative does not mean it's False:

>>> bool(-1)
True

When you do if <something>:, think of it as python doing if bool(<something>).

If the bool(<something>) is True, then the if statement will run. Otherwise, it won't.

TerryA
  • 58,805
  • 11
  • 114
  • 143
1

Only 0 is treated as False. Negative number does not mean that it is False. Try any negative number result will be True.

Vardan
  • 454
  • 1
  • 5
  • 18
1

The documentation exactly tells you which values are considered false:

  • None
  • False
  • zero of any numeric type, for example, 0, 0L, 0.0, 0j.
  • any empty sequence, for example, '', (), [].
  • any empty mapping, for example, {}.
  • instances of user-defined classes, if the class defines a __nonzero__() or __len__() method, when that method returns the integer zero or bool value False. Additional information on these special methods may be found in the Python Reference Manual (Basic customization).
glglgl
  • 89,107
  • 13
  • 149
  • 217