# Even Odd in Python

Published on (Updated: 02 May 2020) Welcome to the Even Odd in Python page! Here, you’ll find the source code for this program as well as a description of how the program works.

## Current Solution

``````import sys

def even_odd(x):
return "Even" if x % 2 == 0 else "Odd"

def exit_with_error():
sys.exit(1)

def main(args):
try:
num = int(args)
print(even_odd(num))
except (IndexError, ValueError):
exit_with_error()

if __name__ == "__main__":
main(sys.argv[1:])
``````

Even Odd in Python was written by:

• Jeremy Grifski
• Parker Johansen

If you see anything you’d like to change or update, please consider contributing.

Note: The solution shown above is the current solution in the Sample Programs repository as of Oct 15 2020 22:17:17. The solution was first committed on Dec 22 2018 22:22:54. As a result, documentation below may be outdated.

## How to Implement the Solution

Let’s look at the code in detail:

code for even-odd.py:-

``````import sys

def even_odd(x):
return "Even" if x % 2 == 0 else "Odd"

def exit_with_error():
sys.exit(1)

def main(args):
try:
num = int(args)
print(even_odd(num))
except (IndexError,ValueError):
exit_with_error()

if __name__ == "__main__":
main(sys.argv[1:])

``````

Here we have a function called even_odd that returns “Even” if a number is even and “Odd” if it is odd. We are checking this using the modulus operator (%) which returns the remainder after division. The result after a modulo operation of an even integer will always be 0 while the result after a modulo operation of an odd integer will always be 1.

Ex. 14 % 2 => 0 (14 / 2 is 7 with no remainder) 15 % 2 => 1 (15 / 2 is 7 with a remainder of 1)

## How to Run the Solution

If we want to run this program, we should probably download a copy of Hello World in Python. After that, we should make sure we have the [latest Python interpreter]. From there, we can simply run the following command in the terminal:

``````python even-odd.py <input>
``````

With being any integer. For example

``````python even-odd.py 12
``````

Alternatively, we can copy the solution into an [online Python interpreter] and hit run.