What does even mean in python?

  1. HowTo
  2. Python How-To's
  3. Check if a Number Is Even or Odd in Python

Created: November-09, 2021

  1. Check Whether a Number Is Even or Odd With the % Operator in Python
  2. Check Whether a Number Is Even or Odd With the & Operator in Python

This tutorial will discuss the methods to check whether a number is even or odd in Python.

Check Whether a Number Is Even or Odd With the % Operator in Python

By definition, a whole number that is completely divisible by 2 is known as an even number. In other words, a whole number is even if after division by 2 we get 0 as remainder. In mathematics, all whole numbers other than even numbers are odd numbers. According to another definition, even numbers are called even because we can evenly split them into two halves. For example, 10 is an even number because we can evenly divide 10 into two halves of 5. On the contrary, 11 cannot be divided into two whole equal numbers because 11 is an odd number.

In python, the modulus operator % divides the first number by the second number and gives us the remainder of the division. There is a way to determine whether a number is odd or even by checking if the remainder after division is equal to 0 or not. The following code snippet shows us how to check whether a number is even or odd with the modulus operator %.

def check(num):
    if num % 2 == 0:
        print("even")
    else:
        print("odd")
check(22)

Output:

even

We defined the check(num) that checks whether the num is completely divisible by 2 with the help of the % operator. If the remainder is equal to 0, the number is even. If the remainder isn’t 0, the number is odd.

Check Whether a Number Is Even or Odd With the & Operator in Python

Another clever way of determining whether a number is even or odd is by using the bitwise AND operator &. As we all know, everything in the computer is stored in the form of 1s and 0s, or binary language in other words. The bitwise AND operator & converts the values to binary and then performs an AND operation on each bit of the binary expression.

For example, the binary value of the decimal number 11 is (1011), and the decimal number 1 is (0001), respectively. If we perform bitwise and operation on these two decimal numbers, the & operator takes each bit in both numbers, performs AND operator on them, and returns the results bit-by-bit. In the above case, the returned value would be (0001), which is equal to 1 in decimal.

Another interesting fact about this phenomenon is that if we take the bitwise AND operation of an even number and 1, the result would always be 0. Otherwise, if we take the bitwise AND operation of an odd number and 1, the result would always be 1.

The sample code below shows how we can use the bitwise AND operator & to check whether a number is odd or even.

def check(num): 
    if num & 1 == 0:
        print("even")
    else:
        print("odd")
check(33)

Output:

odd

We defined the check(num) that checks whether the bitwise AND operation of num and 1 is equal to 0 or not. If the result is equal to 0, the number is even. If the result isn’t 0, the number is odd.

Related Article - Python Math

  • Calculate Factorial in Python
  • Calculate Inverse of Cosine in Python
  • Calculate Modular Multiplicative Inverse in Python
  • Define an Infinite Value in Python
  • Last update on August 19 2022 21:50:48 (UTC/GMT +8 hours)

    Python Basic: Exercise-21 with Solution

    Write a Python program to find whether a given number (accept from the user) is even or odd, print out an appropriate message to the user.

    Pictorial Presentation of Even Numbers:

    What does even mean in python?

    Pictorial Presentation of Odd Numbers:

    What does even mean in python?

    Sample Solution:-

    Python Code:

    num = int(input("Enter a number: "))
    mod = num % 2
    if mod > 0:
        print("This is an odd number.")
    else:
        print("This is an even number.")	
    

    Sample Output:

    Enter a number: 5                                                                                             
    This is an odd number. 
    

    Even Numbers between 1 to 100:

    What does even mean in python?

    Odd Numbers between 1 to 100:

    What does even mean in python?

    Flowchart:

    What does even mean in python?

    Visualize Python code execution:

    The following tool visualize what the computer is doing step-by-step as it executes the said program:

    Python Code Editor:

    Have another way to solve this solution? Contribute your code (and comments) through Disqus.

    Previous: Write a Python program to get a string which is n (non-negative integer) copies of a given string.
    Next: Write a Python program to count the number 4 in a given list.

    Python: Tips of the Day

    Unknown Arguments Using *arguments:

    If your function can take in any number of arguments then add a * in front of the parameter name:

    def myfunc(*arguments):
     for a in arguments:
       print a
    myfunc(a)
    myfunc(a,b)
    myfunc(a,b,c)
    

    How do you use even in Python?

    Also, if…else statements will be used. The required code is provided below. num = int (input (“Enter any number to test whether it is odd or even: “) if (num % 2) == 0: print (“The number is even”) else: print (“The provided number is odd”) Output: Enter any number to test whether it is odd or even: 887 887 is odd.

    What is an even in programming?

    An even number is an integer that is exactly divisible by 2. For example: 0, 8, -24. An odd number is an integer that is not exactly divisible by 2. For example: 1, 7, -11, 15.

    Is named even in Python?

    num = int(input("Enter a number: ")) mod = num % 2 if mod > 0: print("This is an odd number. ") else: print("This is an even number. ")

    Is zero even in Python?

    When input is 0 it is said to be even. The same conditions apply to negative integers too.