How to Divide a List into Even and Odd Numbers Using Python
In this tutorial, we will learn how to program "How to Divide a List into Even and Odd Numbers Using Python." The objective is to split the given numbers into two categories: odd numbers and even numbers. This tutorial will guide you step by step through the entire process of separating the numbers based on their parity. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively, helping you strengthen your problem-solving abilities and enhance your Python 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 dividing numbers into two categories. 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.- while True:
- print("\n=============== Divide a List into Even and Odd Numbers ===============\n")
- try:
- n = int(input("Enter number of elements: "))
- except ValueError:
- print("Invalid input. Please enter an integer.")
- continue
- a = []
- for i in range(n):
- while True:
- try:
- num = int(input(f"Enter element {i+1}: "))
- a.append(num)
- break
- except ValueError:
- print("Invalid number. Please enter an integer.")
- even = [x for x in a if x % 2 == 0]
- odd = [x for x in a if x % 2 != 0]
- print("Even list:", even)
- print("Odd list:", odd)
- opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
- if opt == 'no':
- print("Exiting program...")
- break
- elif opt != 'yes':
- print("Invalid choice. Exiting program...")
- break
This Python program separates a list of user-input numbers into even and odd categories. It first asks the user for the number of elements, then collects each element with input validation to ensure only integers are entered. The program uses list comprehensions to create separate lists for even and odd numbers, which are then displayed to the user. It runs in a loop, allowing repeated execution until the user chooses to exit.
Output:

There you have it we successfully created How to Divide a List into Even and Odd Numbers Using 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