Table of Contents
Introduction
In this section of python programming, we will understand the code to print negative numbers in the list. Previously we have seen the condition of positive number, unlike to that the condition for negative number is number should be less than 0.
Program
input_list, negative_list, negative_list1 = [], [], [] k = 0 n = int(input("Enter the length of list ")) for i in range(0,n): value = int(input()) input_list.append(value) # Using lambda expression result = list(filter(lambda x: (x < 0), input_list)) print("Negative numbers in the list using lambda expression: ",*result) # Using for loop for j in input_list: if j < 0: negative_list.append(j) print("Negative numbers in the given list {0} using for loop are {1}".format(input_list, negative_list)) # Using while loop while (k < len(input_list)): if input_list[k] < 0: negative_list1.append(input_list[k]) k += 1 print("Negative numbers in the given {0} are {1}".format(input_list, negative_list1))
Output
Explanation
In the above python code we have used three methods to find the negative numbers in the list. The value variable will take the user input that is stored as elements of list. The lambda expression, for and while loop will check for the condition of negative number (i.e. num < 0). If the condition is satisfied, the value is printed on the screen.
0 Comments