Difference between "number% 2:" and "number% 2 == 0"?

I am learning about Python boolean logic and how you can trim everything. Are the two expressions in the title equivalent? If not, what are the differences between them?

+3


source to share


3 answers


number % 2

is 0 (so False) if the number is

number % 2 == 0

True, the number is



The first returns a int

, where the second returns a bool

. Python right-hander lets you handle them the same way though.

+11


source


number % 2

equals (abbreviated)



number % 2 != 0

because 1 evaluates to True and 0 to False.

+2


source


Simple. you can try in your terminal:

Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> if 5%2:
...     print "T"
... 
T
>>> if 5%2 == 0:
...     print "T"
... 
>>> 

      

1) if the condition is looking for an answer> 1.2
) In a) you are looking if the answer is> 1 in b) you are looking if the answer is == 0 (if so, as in all other language ==, it will return 1)

+1


source







All Articles