开发者

Python 2.6, what does the % mean in this context?

def f(x):
   开发者_JAVA技巧 return x % 2 != 0 and x % 3 != 0

Just learning the language, I thought % was for string formatting?

Found on the official tutorial: http://docs.python.org/release/2.6.1/tutorial/datastructures.html


In the context of numbers it means modulo -- the remainder you get when you divide x by 2 is x % 2.


That, my friend, would be the modulo operator:

http://en.wikipedia.org/wiki/Modulo_operation for what that is and http://docs.python.org/reference/expressions.html#binary-arithmetic-operations for all binary arithmetic operators in python.


It is the modulo operator, so that your function returns true if and only if the number x doesn't divide by 2 and doesn't divide by 3. x % y represents the remainder of dividing x by y. For Example 5%2 = 1 and 5%3 = 2 and thus, for number 5, your method above returns true.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜