How do you assign odd and even numbers in python?

A number is even if it is perfectly divisible by 2. When the number is divided by 2, we use the remainder operator % to compute the remainder. If the remainder is not zero, the number is odd.

Source Code

# Python program to check if the input number is odd or even.
# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.

num = int(input("Enter a number: "))
if (num % 2) == 0:
   print("{0} is Even".format(num))
else:
   print("{0} is Odd".format(num))

Output 1

Enter a number: 43
43 is Odd

Output 2

Enter a number: 18
18 is Even

In this program, we ask the user for the input and check if the number is odd or even. Please note that { } is a replacement field for num.

Python Program to Check if a Number is Odd or Even

Odd and Even numbers:

If you divide a number by 2 and it gives a remainder of 0 then it is known as even number, otherwise an odd number.

Even number examples: 2, 4, 6, 8, 10, etc.

Odd number examples:1, 3, 5, 7, 9 etc.

See this example:

Output:

How do you assign odd and even numbers in python?


How do you assign odd and even numbers in python?
For Videos Join Our Youtube Channel: Join Now


Feedback

  • Send your Feedback to [email protected]

Help Others, Please Share

How do you assign odd and even numbers in python?
How do you assign odd and even numbers in python?
How do you assign odd and even numbers in python?





Last update on August 19 2022 21:51:46 (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:

How do you assign odd and even numbers in python?

Pictorial Presentation of Odd Numbers:

How do you assign odd and even numbers 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:

How do you assign odd and even numbers in python?

Odd Numbers between 1 to 100:

How do you assign odd and even numbers in python?

Flowchart:

How do you assign odd and even numbers 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

Creating a sequence of numbers (zero to ten with skips):

>>> range(0,10,2)
[0, 2, 4, 6, 8]  

Given a list of numbers, write a Python program to count Even and Odd numbers in a List. Example:

Input: list1 = [2, 7, 5, 64, 14]
Output: Even = 3, odd = 2
Input: list2 = [12, 14, 95, 3]
Output: Even = 2, odd = 2

Example 1: 

count Even and Odd numbers from the given list using for loop Iterate each element in the list using for loop and check if num % 2 == 0, the condition to check even numbers. If the condition satisfies, then increase the even count else increase odd count. 

Python3

list1 = [10, 21, 4, 45, 66, 93, 1]

even_count, odd_count = 0, 0

for num in list1:

    if num % 2 == 0:

        even_count += 1

    else:

        odd_count += 1

print("Even numbers in the list: ", even_count)

print("Odd numbers in the list: ", odd_count)

Output:

Even numbers in the list:  3
Odd numbers in the list:  4

Example 2: Using while loop 

Python3

list1 = [10, 21, 4, 45, 66, 93, 11]

even_count, odd_count = 0, 0

num = 0

while(num < len(list1)):

    if list1[num] % 2 == 0:

        even_count += 1

    else:

        odd_count += 1

    num += 1

print("Even numbers in the list: ", even_count)

print("Odd numbers in the list: ", odd_count)

Output:

Even numbers in the list:  3
Odd numbers in the list:  4

Example 3: Using Python Lambda Expressions 

Python3

list1 = [10, 21, 4, 45, 66, 93, 11]

odd_count = len(list(filter(lambda x: (x%2 != 0) , list1)))

even_count = len(list(filter(lambda x: (x%2 == 0) , list1)))

print("Even numbers in the list: ", even_count)

print("Odd numbers in the list: ", odd_count)

Output:

Even numbers in the list:  3
Odd numbers in the list:  4

Example 4: Using List Comprehension 

Python3

list1 = [10, 21, 4, 45, 66, 93, 11]

only_odd = [num for num in list1 if num % 2 == 1]

odd_count = len(only_odd)

print("Even numbers in the list: ", len(list1) - odd_count)

print("Odd numbers in the list: ", odd_count)

Output:

Even numbers in the list:  3
Odd numbers in the list:  4

Example 5: Using Recursion

Python3

even_count = 0 

i = 0 

odd_count = 0 

def evenoddcount(lst):

    global even_count

    global odd_count

    global i

    if lst[i] % 2 == 0

        even_count += 1

    else

        odd_count += 1

    if i in range(len(lst)-1):

        i += 1 

        evenoddcount(lst) 

    else:

        print("Even numbers in the list: ", even_count)

        print("Odd numbers in the list: ", odd_count)

list1 = [10, 21, 4, 45, 66, 93, 1]

evenoddcount(list1)

Output

Even numbers in the list:  3
Odd numbers in the list:  4

Example 6: Using Bitwise XOR operator

The idea is to check whether the last bit of the number is set or not. If the last bit is set then the number is odd, otherwise even. As we know bitwise XOR Operation of the Number by 1 increments the value of the number by 1 if the number is even otherwise it decrements the value of the number by 1 if the value is odd.

How do you assign odd and even numbers in python?

CHECK IF NUMBER IS EVEN OR ODD USING XOR OPERATOR


Python3

list1 = [10, 21, 4, 45, 66, 93, 1]

even_count, odd_count = 0, 0

for num in list1:

    if num ^ 1 == num + 1:

        even_count += 1

    else:

        odd_count += 1

print("Even numbers in the list: ", even_count)

print("Odd numbers in the list: ", odd_count)

Output

Even numbers in the list:  3
Odd numbers in the list:  4

Example 7: Using Bitwise AND operator

The idea is to check whether the last bit of the number is set or not. If the last bit is set then the number is odd, otherwise even.
As we know bitwise AND Operation of the Number by 1 will be 1, If it is odd because the last bit will be already set. Otherwise, it will give 0 as output. 

Python3

list1 = [10, 21, 4, 45, 66, 93, 1]

even_count, odd_count = 0, 0

for num in list1:

    if not num & 1:

        even_count += 1

    else:

        odd_count += 1

print("Even numbers in the list: ", even_count)

print("Odd numbers in the list: ", odd_count)

Output

Even numbers in the list:  3
Odd numbers in the list:  4

Example 8: Using Bitwise OR operator

The idea is to check whether the last bit of the number is set or not. If the last bit is set then the number is odd, otherwise even. As we know bitwise OR Operation of the Number by 1 increments the value of the number by 1 if the number is even otherwise it will remain unchanged. So, if after OR operation of number with 1 gives a result which is greater than the number then it is even and we will return true otherwise it is odd and we will return false.

Python3

list1 = [10, 21, 4, 45, 66, 93, 1]

even_count, odd_count = 0, 0

for num in list1:

    if num | 1 > num:

        even_count += 1

    else:

        odd_count += 1

print("Even numbers in the list: ", even_count)

print("Odd numbers in the list: ", odd_count)

Output

Even numbers in the list:  3
Odd numbers in the list:  4

Method: Using the enumerate function 

Python3

lst = [12, 14, 95, 3];c=0;c1=0

for i,a in enumerate(lst):

    if a%2==0:

        c+=1

    else:

        c1+=1

print("even number count",c,"odd number count",c1)

Output

even number count 2 odd number count 2

Auxiliary Space: O(1)

Method: Using Numpy.Array : 

Python

import numpy as np

List = [10, 21, 4, 45, 66, 93, 11]

list1 = np.array(List)

Even_list = list1[list1 % 2 == 0]

print("Even numbers in the list: ", len(Even_list))

print("Odd numbers in the list: ", len(list1)-len(Even_list))

Output:

Even numbers in the list:  3
Odd numbers in the list:  4

How do you code odd and even numbers 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.

How do you represent odd numbers in Python?

“how to represent an odd number in python” Code Answer.
num = int(input("Enter a number: ")).
if (num % 2) == 0:.
print("{0} is Even number". format(num)).
print("{0} is Odd number". format(num)).

How do you pick even numbers in Python?

15 ways to print even numbers in Python.
With just one print. The simplest way is: print(0,2,4,6,8,10).
For loop. The first method that comes into my mind: for i in range(0,11,2): ... .
For and % for i in range(11): ... .
Generators and % print([i for i in range(11) if i%2 == 0]).
Generators and Binary. ... .
Bitwise AND..