-3

I have seen the if x%2==1 and x&2 but I don't understand how that works I don't understand the x%2==1 and x&2 means in the if statement

x=int(input("Write your number to define if either is even or odd"))
if x%2==1:
   print('odd')
else:
   print('even')

This work well but I don't understand how they define numbers

Al Imran
  • 882
  • 7
  • 29

1 Answers1

0

% represents modulus which, when applied to integers, measures the remainder of dividing the numerator by the denominator.

In Python, X % 2 == 0 is used to check whether X / 2 has a remainder or not.

Another way to think of it is, X % 2 == 0 checks whether or not 2 is a factor of X.

if X % 2 == 0:
    # 2 is a factor of X, therefore X is even
    is_even = True
else:
    # 2 is not a factor of X, therefore X is odd
    is_even = False