Python program to print positive or negative numbers in a list

Here, we are going to learn different methods to find and print all positive or negative numbers in a list in Python.
Submitted by Shivang Yadav, on April 08, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered sets of values enclosed in square brackets [].

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]

Solution

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: Print positive numbers 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 using loop

# 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: Print positive numbers 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 using lambda expression

# 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: Print negative numbers 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 using loop

# 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: Print negative numbers 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 negative numbers in a list using lambda expression

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

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.