To print only positive numbers from a list, we need to check if the number from the list is greater than or equal to zero.
Method 1: Using loop
We will loop through the list and then print all the numbers that are positive (greater than or equal to 0).
Algorithm:
Loop over the list,
if ( i >= 0) : print i
Program to print positive numbers in a list
# Python program to find positive numbers from a list
# Getting list from user
myList = []
length = int(input("Enter number of elements : "))
for i in range(0, length):
value = int(input())
myList.append(value)
# printing all positive values of the list
print("All positive numbers of the list : ")
for ele in myList:
if ele >= 0:
print(ele, end = " ")
Output:
Enter number of elements : 5
10
-20
30
-40
50
All positive numbers of the list :
10 30 50
Method 2: Using lambda expression
Lambda function is an anonymous function - that means the function which does not have any name.
Syntax:
Lambda parameters : expression
This function can be used to find and print positive numbers from a list.
For this, we will filter the array using lambda expression for values greater than or equal to 0.
Program to print positive numbers in a list
# Python program to find positive numbers from a list
# Getting list from user
myList = []
length = int(input("Enter number of elements : "))
for i in range(0, length):
value = int(input())
myList.append(value)
# finding all positive number from the list
posNoList = list(filter(lambda i: (i >= 0), myList))
# printing all positive values of the list
print("All positive numbers of the list : ", posNoList)
Output:
Enter number of elements : 5
10
-20
30
-40
50
All positive numbers of the list : [10, 30, 50]
Printing negative numbers in a list
In this problem, we will take the list as input from the user and then print only those values from the list that are negative.
To print only negative numbers from a list, we need to check if the number from the list is smaller than zero.
Method 1: Using loop
We will loop through the list and then print all the numbers that are negative (lesser than 0).
Algorithm:
Loop over the list,
if ( i < 0) : print i
Program to print negative numbers in a list
# Python program to find negative numbers from a list
# Getting list from user
myList = []
length = int(input("Enter number of elements : "))
for i in range(0, length):
value = int(input())
myList.append(value)
# printing all negative values of the list
print("All negative numbers of the list : ")
for ele in myList:
if ele < 0:
print(ele, end = " ")
Output:
Enter number of elements : 5
10
-20
30
-40
50
All negative numbers of the list :
-20 -40
Method 2: Using lambda expression
Lambda function is an anonymous function - that means the function which does not have any name.
Syntax:
Lambda parameters : expression
This function can be used to find and print negative numbers from a list.
For this, we will filter the array using lambda expression for values less than 0.
Program to print positive numbers in a list
# Python program to find negative numbers from a list
# Getting list from user
myList = []
length = int(input("Enter number of elements : "))
for i in range(0, length):
value = int(input())
myList.append(value)
# finding all negative number from the list
negNoList = list(filter(lambda i: (i < 0), myList))
# printing all negative values of the list
print("All negative numbers of the list : ", negNoList)
Output:
Enter number of elements : 5
10
-20
30
-40
50
All negative numbers of the list : [-20, -40]
To print only positive numbers from a list, we need to check if the number from the list is greater than or equal to zero.
Method 1: Using loop
We will loop through the list and then print all the numbers that are positive (greater than or equal to 0).
Algorithm:
Program to print positive numbers in a list
Output:
Method 2: Using lambda expression
Lambda function is an anonymous function - that means the function which does not have any name.
Syntax:
This function can be used to find and print positive numbers from a list.
For this, we will filter the array using lambda expression for values greater than or equal to 0.
Program to print positive numbers in a list
Output:
Printing negative numbers in a list
In this problem, we will take the list as input from the user and then print only those values from the list that are negative.
Example:
Solution:
To print only negative numbers from a list, we need to check if the number from the list is smaller than zero.
Method 1: Using loop
We will loop through the list and then print all the numbers that are negative (lesser than 0).
Algorithm:
Program to print negative numbers in a list
Output:
Method 2: Using lambda expression
Lambda function is an anonymous function - that means the function which does not have any name.
Syntax:
This function can be used to find and print negative numbers from a list.
For this, we will filter the array using lambda expression for values less than 0.
Program to print positive numbers in a list
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer