Informatics Practices
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.
Answer
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]
Related Questions
Write the output of the following Python program code:
Str2 = list("Cam@123*") for i in range(len(Str2)-1): if i==5: Str2[i] = Str2[i]*2 elif (Str2 [i].isupper()): Str2 [i] = Str2 [i]*2 elif (Str2 [i].isdigit()): Str2 [i] = 'D' print(Str2)Differentiate between append() and insert() methods of list.
Write a program to find the largest and the second largest elements in a given list of elements.
Write a program to read a list of n integers and find their median.
Note: The median value of a list of values is the middle one when they are arranged in order. If there are two middle values, then take their average.
Hint: Use an inbuilt function to sort the list.