Write a Program to Check Whether a Number is Even or Odd in Python
In this odd and even program, we learn 5 different ways to find the number is odd or even. In the method, we will see to print the odd number and even number, and in some, we will print the boolean value in the place of odd and even number. Even number means True and Odd number means False. We will use the function to find odd and even numbers.
Syntax:
def function_name(parameter):
statement1
statement2
.........
Odd and Even Functions Examples-1 in Python
In this method, we will append method to append the values from the list. We will separate the odd and even numbers.
def odd_even(num):
odd=[]
even=[]
for i in num:
if i%2 == 0:
even.append(i)
else:
odd.append(i)
output_num=[odd,even]
return output_num
number=[1,2,3,12,15,6,7,8,9]
print('Odd Number, Even Number',odd_even(number))
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Odd Number , Even Number [[1, 3, 15, 7, 9], [2, 12, 6, 8]]
Process finished with exit code 0
Odd and Even Functions Examples-2 in Python
In the second method of printing odd and even numbers, we will take the user input values and then we will print that value as odd or even number. If the number will even then print ‘even’ and if the number will be odd then print ‘odd’. In this method, we are using the if and else statement.
def odd_even(num):
if num%2==0:
return "even"
else:
return 'odd'
number = int(input('enter your number: '))
print(odd_even(number))
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
enter your number: 3
odd
Process finished with exit code 0
Odd and Even Functions Examples-3 in Python
In method number 3rd, we will see the same as in method number 2 that we saw but the difference is we are only using if statement not else statement. we are minimizing the code size.
def odd_even(num):
if num%2==0:
return "even"
return "odd"
numb=int(input("enter any no:"))
print(odd_even(numb))
Output:
print(odd_even(numb))
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
enter any no:12
even
Process finished with exit code 0
Odd and Even Functions Examples-4 in Python
In method number 4th, we will learn the same as method number 3 but the difference is that we are directly assigning value to the function odd_even and then we are returning True and False.
def odd_even(num):
if num%2==0:
return True
return False
print(odd_even(112))
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
True
Process finished with exit code 0
Odd and Even Functions Examples-5 in Python
In method number 4 we say that we are using the if condition to return the True and False statement but in method no. 5 we are returning True and False without any comparison.
def is_even(num):
return num%2==0
numb = int(input('enter any number'))
print(is_even(numb))
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
enter any number1234
True
Process finished with exit code 0
Recommended Post:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforce Answers