How to Find the Largest Even or Odd Number in Python


In this example, we will learn how to find out the largest even or the largest odd numbers in a given list with Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

n = input("Please input a list of numbers, separated by SPACE: ")
a = n.split()
b = []
c = []
for i in range(0, len(a)):
    if int(a[i]) % 2 == 0:
        b.append(int(a[i]))
    else:
        c.append(int(a[i]))
b.sort(reverse = True)
c.sort(reverse = True)
print("The largest even number is: ", b[0])
print("The largest odd number is: ", c[0])

Output:

Please input a list of numbers, separated by SPACE: 23 56 15 89 96 84
The largest even number is: 96
The largest odd number is: 89

Please input a list of numbers, separated by SPACE: 26 25 85 86 102 99 37
The largest even number is: 102
The largest odd number is: 99
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments