find odd and even numbers in python

To find odd and even numbers in Python, we can use the modulo operator (%). If a number is divisible by 2, then it is even, otherwise it is odd. Here is an example code:

main.py
number = int(input("Enter a number: "))

if number % 2 == 0:
    print(number, "is even")
else:
    print(number, "is odd")
124 chars
7 lines

In this code, we prompt the user to enter a number and store it in the variable number. Then, we use an if statement to check if number is even, i.e. if it is divisible by 2 with no remainder. If it is, we print a message saying that number is even. Otherwise, we print a message saying that it is odd.

related categories

gistlibby LogSnag