Check If a Number Is Odd or Even in Python

Check if a number is odd or even in Python

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

Python Beginning: Can't figure out how to tell user their number is odd or even (for 5 numbers)

You are trying to use num for two different purposes:

  • the cumulative sum
  • the number that has just been entered

and as a result you end up testing the odd/evenness of the cumulative sum rather than the number that has just been entered.

Separate them out into two different variables num and total and then it will become easier.

I suggest also using a for instead of while loop for x:

total = 0   
for x in range(5):
num = int(input("Enter a number: "))
mod = num % 2
if mod > 0:
print(num, "is an odd number.")
else:
print(num, "is an even number.")
total += num

print("Your total is", total)

python function definition to find if all values in a list are even odd or neither

return will immediately break the loop, so use a holding boolean variable like:

def IsListEven(my_list):

allEven = True

for i in range(len(my_list)):

if my_list[i] % 2 != 0:

allEven = False

return allEven

def IsListOdd(my_list):

allOdd = True

for i in range(len(my_list)):

if my_list[i] % 2 != 1:

allOdd = False

return allOdd

def GetUserValues():

if IsListOdd(my_list) == True:

print("all odd")

elif IsListEven(my_list) == True:

print("all Even")

But your functions can be one liner if you use all(), an example to check if all is odd

my_list = [1,3,5]
print(all(x % 2 == 1 for x in my_list))

How to determines whether two integers are even or odd

Your current code is not checking all four combinations. I would use this logic:

if (num1 + num2) % 2 == 1:
print("One Number is Even and the other is Odd")
elif num1 % 2 == 0 and num2 % 2 == 0:
print("The Numbers are Even")
else:
print("The Numbers are Odd")

Note that the first condition if the above if block rests on that an odd number plus an even number will always result in an odd number, whose mod 2 remainder will be 1.

How do I check if an integer is even or odd?

Use the modulo (%) operator to check if there's a remainder when dividing by 2:

if (x % 2) { /* x is odd */ }

A few people have criticized my answer above stating that using x & 1 is "faster" or "more efficient". I do not believe this to be the case.

Out of curiosity, I created two trivial test case programs:

/* modulo.c */
#include <stdio.h>

int main(void)
{
int x;
for (x = 0; x < 10; x++)
if (x % 2)
printf("%d is odd\n", x);
return 0;
}

/* and.c */
#include <stdio.h>

int main(void)
{
int x;
for (x = 0; x < 10; x++)
if (x & 1)
printf("%d is odd\n", x);
return 0;
}

I then compiled these with gcc 4.1.3 on one of my machines 5 different times:

  • With no optimization flags.
  • With -O
  • With -Os
  • With -O2
  • With -O3

I examined the assembly output of each compile (using gcc -S) and found that in each case, the output for and.c and modulo.c were identical (they both used the andl $1, %eax instruction). I doubt this is a "new" feature, and I suspect it dates back to ancient versions. I also doubt any modern (made in the past 20 years) non-arcane compiler, commercial or open source, lacks such optimization. I would test on other compilers, but I don't have any available at the moment.

If anyone else would care to test other compilers and/or platform targets, and gets a different result, I'd be very interested to know.

Finally, the modulo version is guaranteed by the standard to work whether the integer is positive, negative or zero, regardless of the implementation's representation of signed integers. The bitwise-and version is not. Yes, I realise two's complement is somewhat ubiquitous, so this is not really an issue.



Related Topics



Leave a reply



Submit