How to Print the Largest Even and Odd Numbers in a List in Python

In this tutorial, we will learn how to program "How to Print the Largest Even and Odd Numbers in a List in Python." The objective is to accurately identify and display the largest even and odd numbers from a given list. This tutorial will guide you step by step through the entire process of finding the largest numbers within a set. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively in Python, helping you strengthen your problem-solving abilities and enhance your coding skills.

This topic is straightforward to understand. Just follow the instructions provided, and you will be able to complete it with ease. The program will guide you step by step through the process of displaying the largest even and odd numbers. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. while True:
  2.     print("\n=============== Print the Largest Even and Odd Numbers in a List ===============\n")  
  3.  
  4.     # Get number of elements
  5.     n = int(input("Enter the number of elements to be in the list: "))
  6.  
  7.     # Input the list elements
  8.     b = []
  9.     for i in range(n):
  10.         a = int(input(f"Element {i + 1}: "))
  11.         b.append(a)
  12.  
  13.     # Separate even and odd numbers
  14.     even_numbers = []
  15.     odd_numbers = []
  16.  
  17.     for i in b:
  18.         if i % 2 == 0:
  19.             even_numbers.append(i)
  20.         else:
  21.             odd_numbers.append(i)
  22.  
  23.     # Check if there are any even or odd numbers before sorting
  24.     if even_numbers:
  25.         even_numbers.sort()
  26.         print("Largest even number:", even_numbers[-1])
  27.     else:
  28.         print("No even numbers in the list.")
  29.  
  30.     if odd_numbers:
  31.         odd_numbers.sort()
  32.         print("Largest odd number:", odd_numbers[-1])
  33.     else:
  34.         print("No odd numbers in the list.")
  35.  
  36.  
  37.     opt = input("\nDo you want to try again?(yes/no): ")
  38.            
  39.     if opt.lower() == 'yes':
  40.         ret=False
  41.     elif opt.lower() == 'no':
  42.         ret=True
  43.         print("Exiting program....")
  44.     else:
  45.         print("Please enter yes/no:")
  46.         break
  47.  
  48.     if ret == False:
  49.         continue

This Python program finds and displays the largest even and odd numbers from a user-provided list. It separates the numbers into even and odd categories, sorts them, and then prints the largest number from each category. If there are no even or odd numbers, it notifies the user accordingly. The program runs in a loop, allowing repeated execution based on user input.

Output:

There you have it we successfully created How to Print the Largest Even and Odd Numbers in a List in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials