Check if odd 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.

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 programming and developing gui applications with pyqt

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.

Источник

Odd Number in Python

Python program to check given number is an odd number or not? We will discuss different methods to check number is an odd number or not. A number will be given to the program and the python program will check the given number is an odd number or not. We will also find all odd numbers in between a given range in Python.

Odd number:- A number is called an odd number if it is not divisible by 2.

Example:- 1, 3, 5, 7 e.t.c. are odd numbers but 2, 4, 6, 8 are not an odd number because they are completely divisible by number 2, they are even numbers.

Check if the Number is Odd in Python

This is the simplest and easiest python program to check given number is an odd number or not. We will take one number while declaring the variables. Python program will check given number is an odd number or not using mathematical calculation and finally, it will be displayed on the screen.

Program description:- Write a Python program to check number is odd?

# Python program to check given number is an odd or not # take inputs num = 3 # check number is odd or not if(num % 2 != 0): print(' is an odd number'.format(num)) else: print(' is not an odd number'.format(num))

In this program, value for the number was hardcoded in the program.

Then, checked the given number is an odd number or not using the if-else statement and finally, display the result.

if(num % 2 != 0): print(' is an odd number'.format(num)) else: print(' is not an odd number'.format(num))

Odd Numbers in Python

In the previous program, inputs were hardcoded in the program but in this program, inputs will be provided by the user.

# Python program to check given number is an odd or not # take inputs num = int(input('Enter a number: ')) # check number is odd or not if(num % 2 != 0): print(' is an odd number'.format(num)) else: print(' is not an odd number'.format(num))

Output for the different input values:-

Enter a number: 1
1 is an odd number

Enter a number: 10
10 is not an odd number

Enter a number: 253
253 is an odd number

In this program, inputs are scanned using the input() function and stored in variable num.

num = int(input('Enter a number: '))

Then, check the given number is an odd number or not using the if-else statement and finally, display the result.

Using Functions

We can also take the help of a function to check number is odd or not in python. A function is a block of code that performs a specific task.

# Python program to check given number is an odd or not using function # Returns true if num is odd, else not odd def isOdd(num): # check number is odd or not return (num % 2 != 0) #take inputs num = int(input('Enter a number: ')) # display result if isOdd(num): print(num,"is an odd number") else: print(num,"is not an odd number")

Output for the different input values:-

Enter a number: 15
15 is an odd number

Enter a number: 20
20 is not an odd number

In this program, first of all a function isOdd() is defined which checks the passed value is an odd number or not?

# Returns true if num is odd, else not odd def isOdd(num): # check number is odd or not return (num % 2 != 0)

Inputs are scanned using the input() function and stored in variable num. Then call the function and print the result condition.

Find Odd Numbers in the Range

In this program, We will learn how to print odd numbers in the given range. Example:- Python program will be print all odd numbers in the 1 to 10 number range.

# Python program to print all odd numbers in given range # take range start = int(input('Start: ')) end = int(input('End: ')) print('All odd number in the range') for num in range(start, end + 1): # check number is odd or not if num % 2 != 0: print(num, end = " ")

Output for the different input values:-

Start: 1
End: 10
All odd number in the range
1 3 5 7 9

Start: 5
End: 25
All odd number in the range
5 7 9 11 13 15 17 19 21 23 25

Program to Print Odd Number from 1 to 100

# Python program to print all odd numbers in given range # take range start, end = 1, 100 print('All odd number in the range') for num in range(start, end + 1): # check number is odd or not if num % 2 != 0: print(num, end = " ")

All odd number in the range
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Источник

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

Источник

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