Odd and even numbers in python

Python Function To Check Number is Even or Odd

In this Python Tutorial, we will learn the Python function to check whether a given number is even or odd. In addition, we will learn about even or odd numbers in Python.

Even or Odd Number in Python

In mathematics, integers can be classified into two major categories, namely: even and odd numbers. An even number is an integer that is exactly divisible by 2, i.e., when it’s divided by 2, there is no remainder. On the other hand, an odd number is an integer that is not evenly divisible by 2.

Читайте также:  Extends type parameter java

Python Program to Find odd or even using function

  • To determine whether a number is even or odd, we use the modulus operator ( % ), which returns the remainder of the division. In Python, the modulus operator is the percentage sign ( % ).
  • When a number is divided by 2, if it leaves a remainder of 0, then it is an even number.
  • If it leaves a remainder of 1, then it is an odd number.

Python Function to check whether a given number is even or odd

Let’s now implement the function in Python:

def check_even_odd(number): if number % 2 == 0: return "Even" else: return "Odd" print(check_even_odd(7)) print(check_even_odd(8)) print(check_even_odd(0)) 
  • In this code snippet, we defined a function named check_even_odd which takes a single argument number .
  • Inside the function, we used an if-else statement with the condition number % 2 == 0 . This condition will be true if number is evenly divisible by 2 (i.e., it’s an even number), and the function will return the string “Even”.
  • If the condition is false (i.e., number is not evenly divisible by 2, making it an odd number), the function will return the string “Odd”.
  • Next, we can see that the function correctly identifies 10 as an even number, 13 as an odd number, and 0 (which is considered even) as an even number.

Python Function To Check Number is Even or Odd

Conclusion

In this article, we’ve learned how to create a Python function that can determine whether a given number is even or odd.

You may also like to read the following Python tutorials.

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

Читайте также:  Php creating object with new

Источник

Even Odd Program in Python

Python Certification Course: Master the essentials

Even numbers are the numbers that can be completely divided by 2 without leaving any remainder. On the other hand, odd numbers are those numbers that can not be completely divided by 2 (leaves a remainder when divided by 2 ). We can code the even-odd program in python using divisibility logic and a bitwise operator.

Introduction

Before learning about the even-odd program in python, we should first know about the even and odd numbers.

Even numbers are the numbers that can be completely divided by 2 without leaving any remainder. The even numbers always end in 0, 2, 4, 6, or 8 . If a number is very large, by just looking at the ending digit, we can easily check that the number is even or not.

Example:
74 is an even number. Since 74 ends with 4 as well as 74 are completely divisible by 2 .

On the other hand, odd numbers are the numbers that can not be completely divided by 2 (leaves a remainder when divided by 2 ). The odd numbers always end in 1, 3, 5, 7, or 9 . Just like even numbers, if a number is very large, we can easily check if the number is odd or not by looking at the ending digit.

Example:
71 is an odd number. Since 71 ends with 1 as well as 71 leave the remainder 1 when divided by 2 .

So, as we have seen the number 2 2 2 plays an important role in finding the even and odd numbers. So, let us code the even and odd program in python by using the divisibility logic.

Note: The number 0 is considered an even number. As 0 ends with one of the numbers ( 0, 2, 4, 6, 8 ).

Examples

Let us code the even and odd program in python.

Even Odd Program in Python Using Division

We will divide the given number by 2. If the number gets completely divided by 2 then the number is an even number. Else, the number is not an even number (i.e. odd number).

The pseudoscope for the even off program in python can be:

Function Name: even_odd(number)

Note: We can use the modulo operator in python % to find the remainder. The modulo operator in python returns the remainder of a division between two numbers.

Even Odd Program in Python Using Bitwise Operator

We can also use Bitwise Operators in even-odd programs in python. Bitwise Operators are the operators that help us to perform bit (bitwise) operations. Bit operators are ~ , > , & , ^ , | .

  • ~ is the bitwise NOT operator that inverts all the bits of a number.
  • >> is the right shift operator that shifts the number of places to the right.
  • | is the bitwise OR operator that results in 1 if any of the two bits is 1.
  • & is the bitwise AND operator that results in 1 if both of the two bits is 1.

Using the bitwise operator , we will check if the last bit of the number is set or not. If the last bit is set then the number must be odd. Else, the number will be an even number.

Refer to the image below for a better understanding and visualization of the XOR concept .

check number is even or odd using xor operator

Conclusion

  • Even numbers are the numbers that can be completely divided by 2 without leaving any remainder.
  • On the other hand, odd numbers are the numbers that can not be completely divided by 2 (leaves a remainder when divided by 2 ).
  • We can code the even-odd program in python using divisibility logic and bitwise operator.

See Also:

Источник

Python How to Check If a Number Is Odd or Even (Examples)

An odd number is a number you cannot divide into two equal parts. An even number is a number you can evenly divide into two parts. In Python, you can use the modulo operator (%) to check if a number is odd or even.

n = 9 # 1. Check if a number is odd is_odd = n % 2 != 0 # 2. Check if a number is even is_even = n % 2 == 0

This guide teaches you how to check if a number is odd or even and how the modulo operator works in Python.

The Modulo Operator in Python

Modulo in python

In Python, the modulo operator (%) returns the remainder in the division between two numbers.

If this operation returns 0, it means a is evenly divisible by b.

For example, if you have 15 slices of pizza for 3 guests and you want to check if you can share the 15 slices with your guests evenly, you can use the modulo operator:

Make sure to check my complete guide to modulos in Python.

How to Use Modulo to Figure Out If a Number Is Odd or Even?

The modulo operator returns the remainder in the division.

  • An odd number isn’t evenly divisible by 2.
  • An even number is evenly divisible by 2.

In the context of modulos, this means:

  • Calculating modulo between an odd number and 2 returns 1 as a leftover value.
  • Calculating modulo between an even number and 2 returns 0.

How to Check If a Number Is Even in Python?

Using modulo to check if a value is even

You can use the modulo operator in Python to check if a number is even.

If you take a modulo between an even number and 2, the result is 0.

To write a program that checks if a number is even:

  1. Take the modulo between your target number and 2.
  2. Use a comparison operator to check if the result is equal to 0.

For example, let’s check if the number 9 is even:

number = 9 is_even = number % 2 == 0 print(is_even)

How to Check If a Number is Odd in Python

Using modulo to check if a value is odd

You can also use the modulo operator in Python to check if a number is odd.

If you take a modulo between an odd number and 2, the result is 1.

To write a program that checks if a number is odd:

  1. Take the modulo between your target number and 2.
  2. Use a comparison operator to check if the result is 1.

For instance, let’s see if the number 11 is odd:

number = 11 is_odd = number % 2 == 1 print(is_odd)

Example 1. Write a Function That Checks If a Number Is Odd/Even

In the previous examples, you learned how to use the modulo operator to check if a value is odd or even in Python.

But the above examples are just standalone code expressions. If you want to reuse the odd/even logic, you’d have to rewrite the comparisons over and over again.

To improve your code quality and readability, you can implement functions for checking if an input number is odd/even. This way you can reuse the code by calling the name of the function.

Here are two functions for calculating if a value is odd or even:

def is_even(n): return n % 2 == 0 def is_odd(n): return n % 2 != 0

Now you can use these functions anywhere and any number of times in your code.

print(is_odd(10)) print(is_even(6))

Example 2. Check If User Input Is Odd/Even

A common beginner-friendly Python example is to take user input and check if the input is odd/even.

Here’s a program that does it for you:

number = int(input("Enter a number: ")) if number % 2 == 0: print("The number is even") else: print("The number is odd")
Enter a number: 16 The number is even
  1. The input() function takes the user input in the console.
  2. The int() call converts the input to an integer from a string (text).
  3. The if-else statement checks if the input number is divisible by 2 or not.

Summary

Today you learned how to use Python to check if a number is odd or even.

To take home, you can use the modulo operator (%) to check for odd/even values. If the % operator returns 0 between a number and 2, the value is evenly divisible by 2 and is thus even. If the result is 1, the number is not divisible by 2, which means it’s odd.

Thanks for reading. Happy coding!

Read Also

Источник

Python Program to Check Even or Odd Number

allinpython.com allinpython.com

In this post, you will learn a Python Program to Check Even or Odd Numbers with detailed explanation and algorithm but before we dive into the coding part, let us first understand what is an even and odd number.

What is an Even and Odd number?

A number that is divisible by 2 is known as an Even number whereas a number that is not divisible by 2 is known as an Odd number.

For Example: 2, 4, 6, 8, 10 are even numbers, while 1, 3, 5, 7, 9 are odd numbers.

Algorithm to Check Even or Odd Number

  1. Take the input from the User ( num )
  2. check if num % 2 == 0 then print num is Even.
  3. else print num is Odd.

These three basic steps you have to follow to write a python program to check even or odd numbers.

But before writing this program few programming concepts you have to know:

Source Code

num = int(input("Enter a number to check Odd and Even: ")) if num % 2 == 0: print(f" is Even Number") else: print(f" is Odd Number")

Output-1

Output-2

Let us modify the above program and write it using the function.

Python Program to Check Even or Odd Number Using the function

Before writing this program few programming concepts you have to know:

Source Code

def isEven(num): if num %2 == 0: return f" is Even number" else: return f" is Odd number" number = int(input("Enter a number to check Odd and Even: ")) print(isEven(number))

Output-1

Output-2

Источник

Оцените статью