In this python program, we will learn how to put positive and negative numbers in a separate list.
Here is the source code of the program to put positive and negative numbers in a separate list.
# Python Program to Put Positive and Negative Numbers in a Separate List Using For Loop
NumList = []
Positive = []
Negative = []
# Take the Input From the User
Number = int(input("Enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
NumList.append(value)
# Print the List
print("\nOriginal List: ",NumList)
for j in range(Number):
if(NumList[j] >= 0):
Positive.append(NumList[j])
else:
Negative.append(NumList[j])
print("\nElement in Positive List is: ", Positive)
print("Element in Negative List is: ", Negative)
Enter the Total Number of List Elements : 8
Enter the Value of 1 Element: 12
Enter the Value of 2 Element: -45
Enter the Value of 3 Element: -5
Enter the Value of 4 Element: 56
Enter the Value of 5 Element: 52
Enter the Value of 6 Element: -85
Enter the Value of 7 Element: 56
Enter the Value of 8 Element: 63
Original List: [12, -45, -5, 56, 52, -85, 56, 63]
Element in Positive List is: [12, 56, 52, 56, 63]
Element in Negative List is: [-45, -5, -85]
# Python Program to Put Positive and Negative Numbers in a Separate List Using While Loop
NumList = []
Positive = []
Negative = []
j = 0
# Take the Input From the User
Number = int(input("Enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
NumList.append(value)
# Print the List
print("\nOriginal List: ",NumList)
while(j < Number):
if(NumList[j] >= 0):
Positive.append(NumList[j])
else:
Negative.append(NumList[j])
j = j + 1
print("\nElement in Positive List is: ", Positive)
print("Element in Negative List is: ", Negative)
Enter the Total Number of List Elements : 6
Enter the Value of 1 Element: 45
Enter the Value of 2 Element: -85
Enter the Value of 3 Element: -52
Enter the Value of 4 Element: 65
Enter the Value of 5 Element: 85
Enter the Value of 6 Element: -96
Original List: [45, -85, -52, 65, 85, -96]
Element in Positive List is: [45, 65, 85]
Element in Negative List is: [-85, -52, -96]
Comments