Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python checking whether a number is even

Python checking whether a number is even

Document Object Reference Manual Python3 Examples

In Python, we can use the modulo operator % to check whether a number is even. If a number divided by 2 has a remainder of 0, then the number is even.

Example

def

is_even

(

number

)

:

   

return

number %

2

==

0

Testing

num

=

4

if

is_even

(

num

)

:

   

print

(

f

"{num} is even"

)

else

:

   

print

(

f

"{num} is not even"

)

Code analysis:

Output result:

4 是偶数

Document Object Reference Manual Python3 Examples