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

importsysdefeven_odd(x):return"Even"ifx%2==0else"Odd"defexit_with_error():print('Usage: please input a number')sys.exit(1)defmain(args):try:num=int(args[0])print(even_odd(num))except(IndexError,ValueError):exit_with_error()if__name__=="__main__":main(sys.argv[1:])

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:

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: