Menu Close

Python Program to Print Positive Numbers in a List

Python Program to print positive numbers in a list

In this Python example guide, we are going to write a Python program to print positive numbers in a list. Here we will see various ways to find all the positive numbers from the list using Python.

Before going through this article, you should have basic knowledge of the following Python topics.

What is positive number?

A number greater than 0 is called a positive number.

What is negative number?

A number less than 0 is called a negative number.

Python Program to Print Positive Numbers in a List

There are various ways to write a Python program to print the positive numbers from the list.

Using for loop

Python for loop is the best way to filter all the items of the list to find the positive numbers.

Example:


#list of numbers
list1 = [-12, 23, -46, -35, 22, 13, 67]
print('Original List is:- ', list1)

positive = []
for i in list1:
	if i > 0:
		positive.append(i)
        
print('Positive numbers are:- ', positive)

Output


Original List is:-  [-12, 23, -46, -35, 22, 13, 67]
Positive numbers are:-  [23, 22, 13, 67]

Using list comprehension

List comprehension is a short and effective way to find positive numbers from the list because you can put all the code within a single line of code.

Example


#list of numbers
list1 = [-12, 23, -46, -35, 22, 13, 67]
print('Original list:- ', list1)
even_numbers = [i for i in list1 if i > 0]
print('Positive numbers are:- ', even_numbers)

Output


Original list:-  [-12, 23, -46, -35, 22, 13, 67]
Positive numbers are:-  [23, 22, 13, 67]

Using while loop

While is also used to write a Python program to get all the positive numbers from the list.

Example:


#list of numbers
list1 = [-12, 23, -46, -35, 22, 13, 67]
print('List is:- ', list1)

#length of list1
x = len(list1)

num = 0
positive = []

while( num < x ):

    #checking condition
    if list1[num] > 0:
        positive.append(list1[num])
    num = num + 1
	
	
print('Positive numbers are:- ', positive)

Output


List is:-  [-12, 23, -46, -35, 22, 13, 67]
Positive numbers are:-  [23, 22, 13, 67]

Using lambda function

Lambda function is a single-line anonymous function that can be used to find the positive numbers from the list.

Example


list1 = [-12, 40, -46, -35, 30, 13, 100]
print('Original list:- ', list1)
positive = list(filter(lambda x: (x > 0), list1)) 
print('Positive numbers are:- ', positive)

Output


Original list:-  [-12, 40, -46, -35, 30, 13, 100]
Positive numbers are:-  [40, 30, 13, 100]

Conclusion

So here we have seen all the different ways to find positive numbers in a list in Python. This type of question is mostly asked by interviewers to test your logical skills for the Python developer position.

You use anyone from the above examples to find the positive numbers from the list.

If you like this article, please share and keep visit for further Python interesting examples.

~ Thanks for reading

Previous Article
Next Article

Was this post helpful?

Related Posts