Python Program for even or odd

Here we will understand how to create a program and check the condition of whether the number is even or odd in Python. And also we are going to discuss the following given topics.

  • Python program for even or odd
  • Write a Python program to check even or odd using function
  • Write a Python program to find whether the number is even or odd
  • How to write a Python program to put even and odd numbers in a different list

Python Program for even or odd

  • In this section, we will discuss Python Program to Determine Whether a Given Number is Even or Odd.
  • The modulus operator is used to determine if a number in Python is even or odd. The remainder acquired when a division is performed is returned by the modulus operator.
  • The program will print that the input number is even if the value returned after the modulus operator application is zero. The number is odd and will be printed if not. Also utilized are if…else statements.
  • In simple words, we can say that if you divide a value by 2 then it will return a remainder of 0. So it is known as an even number, otherwise, it will return an odd number.
  • Mathematically, the even numbers are 2,4,6,10 and the odd numbers are 1,3,5,7,9. If a number is exactly divisible by two, it is even. We use the remainder operator% to calculate the remainder after dividing the value by 2. The integer is odd if the remainder does not equal zero.

Example:

new_number = int(input("Enter the value: "))  
if (new_number  % 2) == 0:  
   print("It is Even number".format(new_number ))  
else:  
   print("It is Odd number".format(new_number ))  

In the following given code first, we will take the input value as a user and then will set the condition num%2==0 and it will check the condition after getting the remainder whether it is even or odd.

Here is the Screenshot of the following given code.

Python Program for even or odd
Python Program for even or odd

This is how we can create a python program for even or odd numbers.

Read: Check if a number is a prime Python

Python program to check even or odd using function

  • In this example, we will discuss how to check even or odd numbers in Python by using a function.
  • The element that divides by two If there is no leftover, the number is said to be even. If a given integer is exactly divisible by 2, it is referred to as an even number.
  • The output is referred to as the odd number if the input is divided by 2 and the result is something other than 0.

Example:

def evoddfunc(num):
  if num%2==0:
    print(num,"is an even number")
  else:
    print(num,"is an odd number")
evoddfunc(20)

Here is the execution of the following given code.

Python program to check even or odd using function
Python program to check even or odd using function

In this example, we have understood how to check the number whether is even or odd.

Read: Python convert tuple to list

Python program to find whether the number is even or odd

  • In this example, we will discuss how to check whether the number is even or odd in Python.
  • Mathematically, the even numbers are 2,4,6,10 and the odd numbers are 1,3,5,7,9. If a number is exactly divisible by two, it is even. We use the bitwise operator to calculate the remainder after dividing the value by 2. The integer is odd if the remainder does not equal zero.
  • A bitwise operation in computer programming manipulates a bit string, bit array, or binary number at the level of its bits. It is a quick and easy action that is directly supported by the CPU and fundamental to higher-level arithmetic operations.

Example:

def even_func(new_number):
  return not new_number&1
if __name__ == "__main__":
  new_number = 19
  if even_func(new_number):
    print('It is Even number')
  else:
    print('It is an Odd number')

Here is the implementation of the following given code

Python program to find whether the number is even or odd
Python program to find whether the number is even or odd

This is how we can create a program to find whether the number is even or odd in Python.

Read: Get current directory Python

Python program to put even and odd numbers in a different list

  • Here we will discuss how to put even and odd numbers in a different list in Python.
  • To perform this particular task we are going to use the list comprehension method and a Python list comprehension comprised of brackets enclosing the expression, which is run for each element, and the for loop iterates through each element in the Python list.
  • Use a list comprehension to store all even members in the provided list. For example, take the list evenList and then Use a list comprehension to store all the odd members in the existing list, and declare it as oddlist.
  • As a result, two separate lists are created for the even and odd entries. It is necessary to print both the evenList and the oddList and then execute the program.

Example:


input_list = [14, 18, 31, 11, 13, 87, 103,
              27, 64, 96, 22, 48, 17, 15, 11, 28, 47]

new_even_list = [m for m in input_list if m % 2 == 0]
new_odd_List = [m for m in input_list if m % 2 != 0]

print("Even numbers are available in list are :")
print(new_even_list)
print("Odd numbers are available in list are :")
print(new_odd_List)

Here is the Screenshot of the following given code.

Python program to put even and odd numbers in a different list
Python program to put even and odd numbers in a different list

You may also like to read the following Python tutorials.

In this tutorial, we have discussed how to create a program and check the condition of whether the number is even or odd in Python. And also we have covered the following given topics.

  • Python program for even or odd
  • Write a Python program to check even or odd using function
  • Write a Python program to find whether the number is even or odd
  • How to write a Python program to put even and odd numbers in a different list