CBSE Class 11 Informatics Practices Question 55 of 80

Lists in Python — Question 18

Back to all questions
18
Question

Question 13

Write a program to read a list of n integers (positive as well as negative). Create two new lists, one having all positive numbers and the other having all negative numbers from the given list. Print all three lists.

Solution
n = int(input("Enter number of elements in the list: "))
nums = []

print("Enter the elements of the list:")
for i in range(n):
    n1 = int(input("Enter element: "))
    nums.append(n1)

positive = []
negative = []

for num in nums:
    if num < 0:
        negative.append(num)
    else:
        positive.append(num)

print("Original List:", nums)
print("Positive Numbers:", positive)
print("Negative Numbers:", negative)
Output
Enter number of elements in the list: 6
Enter the elements of the list:
Enter element: 1
Enter element: -4
Enter element: 0
Enter element: -3
Enter element: 5
Enter element: 9
Original List: [1, -4, 0, -3, 5, 9]
Positive Numbers: [1, 0, 5, 9]
Negative Numbers: [-4, -3]
Answer