Sum Negative, Even, and Odd Numbers in a List Using Python

In this tutorial, we will learn how to program "Sum Negative, Even, and Odd Numbers in a List Using Python." The objective is to print the sum of negative, even, and odd numbers in a list. This tutorial will guide you step by step through methods for displaying the values of a list. 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 improve your coding skills.

This topic is straightforward and easy to understand. Simply follow the instructions provided, and you will complete it with ease. The program will guide you step by step through the process of printing the sum of negative, even, and odd numbers in a list. 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============ Sum Negative, Even, and Odd Numbers in a List ============\n")
  3.  
  4.     n = int(input("Enter the number of elements to be in the list: "))
  5.     b = []
  6.     for i in range(n):
  7.         a = int(input("Element: "))
  8.         b.append(a)
  9.  
  10.     sum_even = 0
  11.     sum_odd = 0
  12.     sum_neg = 0
  13.  
  14.     for j in b:
  15.         if j > 0:
  16.             if j % 2 == 0:
  17.                 sum_even += j
  18.             else:
  19.                 sum_odd += j
  20.         else:
  21.             sum_neg += j
  22.  
  23.     print("Sum of all positive even numbers:", sum_even)
  24.     print("Sum of all positive odd numbers:", sum_odd)
  25.     print("Sum of all negative numbers:", sum_neg)
  26.  
  27.     # Ask user if they want to try again
  28.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  29.     if opt == "no":
  30.         print("Exiting program...")
  31.         break
  32.     elif opt != "yes":
  33.         print("Invalid choice. Exiting program...")
  34.         break

This program takes a list of integers from the user and calculates three separate sums: the sum of all positive even numbers, the sum of all positive odd numbers, and the sum of all negative numbers. It iterates through the list, categorizes each number, and updates the corresponding sum. The results are then displayed, and the user is prompted to repeat the process or exit the program.

Output:

There you have it we successfully created Sum Negative, Even, and Odd Numbers in a List 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

Python Tutorials