Q:

Python program to print positive or negative numbers in a list

belongs to collection: Python List Programs

0

Printing positive 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 positive.

Example:

Input:
[4, -1, 5, 9, -6, 2, -9, 8]

Output:
[4, 5, 9, 2, 8]

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

Example:

Input:
[4, -1, 5, 9, -6, 2, -9, 8]

Output:
[-1, -6, -9]

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:

  • 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]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python List Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to extract keywords from the list... >>
<< Check all elements are unique or not in Python...