if num % 2 == 0:
pass # Even
else:
pass # Odd
The %
sign is like division only it checks for the remainder, so if the number divided by 2
has a remainder of 0
it's even otherwise odd.
Or reverse them for a little speed improvement, since any number above 0 is also considered "True" you can skip needing to do any equality check:
if num % 2:
pass # Odd
else:
pass # Even
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…