In this python tutorial, you will learn how to Check If a Number is Even or Odd using the if and else statements along with the modulus operator of the python programming language.
How to Check If a Number is Even or Odd in Python?
Let’s take a look at the first source code , here the values are assigned in the code, the == equality operator and the modulus operator along with the if and else statements carry out the function.
RUN CODE SNIPPETnum = 12
if (num % 2) == 0:
print("{0} is an Even number".format(num))
else:
print("{0} is an Odd number".format(num))OUTPUT:
12 is an Even number
- At the start, we declare the variable
numand assign the integer value12. - We declare the
ifstatement with the condition(num % 2) == 0in which the%operator divides the integer value by2and if the value equals0, the condition is true, if it is not equal to0, then the condition is false. - Now, we display the output value using the
printand display the statement/string("{0} is an Even number".format(num))followed by theelsestatement and the statement/string("{0} is an Odd number".foramt(num)) - In the above statement/string, the variables
{0}will hold the valuenumwhere theformatfunction helps in variable substitution and data formatting. - As given in the second point, with respect to whether the condition is true or false the respective print statement will be displayed with the help of the
ifandelsestatement.
Let’s take a look at the second source code , here the values are given as input by the user in the code, the == equality operator and the modulus operator along with the if and else statements carry out the function.
RUN CODE SNIPPETnum = int(input("Enter a number: "))
if (num % 2) == 0:
print("\n{0} is an Even number".format(num))
else:
print("\n{0} is an Odd number".format(num))INPUT:
13
OUTPUT:
Enter a number: 13 is an Odd number
- Here we give the user the option to enter the values and the input values are scanned using the
inputfunction and are stored the variablesnumwith the statements/stringsEnter a number:. - In the STDIN section of the code editor the input values are entered.
- Now, we display the output value using the
printand display the statement/string("{0} is an Even number".format(num))followed by theelsestatement and the statement/string("{0} is an Odd number".foramt(num)) - In the above statement/string, the variables
{0}will hold the valuenumwhere theformatfunction helps in variable substitution and data formatting. - As given in the second point, with respect to whether the condition is true or false the respective print statement will be displayed with the help of the
ifandelsestatement.
NOTE:
- The == equality operator is a comparison operator which returns True is the two items are equal and returns False if not equal.
- The modulus operator % is used to compute the reminder.
- The if and else statements evaluates whether an expression is true or false. If a condition is true, the “if” statement is executed otherwise, the “else” statement is executed.
- The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
- The statement for the input function are enclosed in single quotes and parenthesis.
- The \n in the code indicates a new line or the end of a statement line or a string.
- The variables used by the format function for substitution is enclosed in curly braces.
- The print statement is followed by a period, to initiate the format function.
- The print statement/string to be displayed in enclosed in double quotes.