Python program to print positive numbers in a list
In this tutorial, we will write a Python program to find and print all the positive numbers in a list. The List is an ordered set of values enclosed in square brackets [ ]. List stores some values called elements in it, which can be accessed by their particular index.
Any number which is above zero is known as a positive number. Positive numbers can be written without any sign or a '+' sign in front of them. We will be following various approaches to find and print all the positive numbers in a list.
Input: [2, 6, -10, -3, 1, -9]
Output: [2, 6, 1]
Input: [-2, -4, 5, -3, 0, 6, -10]
Output: [5, 0, 6, ]
Approach to print positive numbers in a list
To execute this task we can follow various approaches, which will be discussed in detail below
- Traversing the list and checking each element if it is positive
- Using list comprehension
- Using lambda functions
Approach 1: traversing and checking
In this approach, we will use for loop to traverse the list, and with the help of if condition, we will check if the number in each iteration is greater than or equal to 0. The numbers which will satisfy the given condition will be printed.
Algorithm
Follow the algorithm to understand the approach better:
Step 1- Take input of list elements from the user
Step 2- Add the elements to the list
Step 3- To print the positive elements, run a loop for each element
Step 4- Check if the element is greater than or equal to 0
Step 5- Numbers that satisfy the condition will be printed
Python Program 1
Look at the program to understand the implementation of the above-mentioned approach. In this program, we have taken an input of elements of the list from the user and by using append() added the elements to the list. The append() is a built-in function in the Python library.
# print positive Numbers in a List
# input of list
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)
print("Positive numbers in",li,"are: ")
#traversing
for i in li:
# checking condition
if i >= 0:
print(i, end = " ")
Enter size of list 5
Enter element of list 3
Enter element of list -9
Enter element of list 0
Enter element of list -2
Enter element of list 1
Positive numbers in [3, -9, 0, -2, 1] are:
3 0 1
Approach 2: using list comprehension
In this approach, we will use the list comprehension method. List comprehension is a shorter syntax for creating a new list based on the values of an existing list. We will add the positive numbers from the list into the new list with the help of list comprehension.
Algorithm
Follow the algorithm to understand the approach better
Step 1- Take input of list elements from the user
Step 2- Add the elements to the list
Step 3- To print the positive numbers, use list comprehension
Step 4- Check if the element is greater than or equal to 0
Step 5- If the number satisfies the condition store it in a new list
Step 6- Print the new list
Python Program 2
In this program, we have taken an input of elements of the list from the user and using append() which is a built-in function in the Python library, added the elements to the list. To print the numbers with space we have used end=" ".
# print positive Numbers in a List
# input of list
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)
print("Positive numbers in",li,"are: ")
# using list comprehension
positive_num = [num for num in li if num >= 0]
print(positive_num)
Enter size of list 5
Enter element of list -2
Enter element of list 5
Enter element of list 2
Enter element of list -4
Enter element of list 9
Positive numbers in [-2, 5, 2, -4, 9] are:
[5, 2, 9]
Approach 3: using lambda function
In this approach, we will be using lambda function to display positive numbers in a list. A lambda function is a small anonymous function. It can take any number of arguments, but can only have one expression.
Algorithm
Follow the algorithm to understand the approach better
Step 1- Take input of list elements from the user
Step 2- Add the elements to the list
Step 3- To print the positive numbers, use the lambda function
Step 4- Give the condition as- number in the list is greater than or equal to 0
Step 5- Numbers that satisfy the condition will be stored in a new list
Step 6- Print the new list
Python Program 3
We have defined a lambda function that will store all the numbers in the given list which are greater than or equal to 0 to the new list.
# print positive Numbers in a List
# input of list
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)
print("Positive numbers in",li,"are: ")
# using lambda function
pos_num = list(filter(lambda x: (x>=0),li))
print(pos_num)
Enter size of list 6
Enter element of list 3
Enter element of list 7
Enter element of list 2
Enter element of list -3
Enter element of list -5
Enter element of list -1
Positive numbers in [3, 7, 2, -3, -5, -1] are:
[3, 7, 2]
Conclusion
In this tutorial, we have learned three approaches for finding and displaying all the positive numbers in a list. We have discussed the use of loops, conditional statements, list comprehension, and lambda functions in this tutorial.