Program to print positive numbers in the list using Python

by | Apr 3, 2021 | Python Programs

Home » Python » Python Programs » Program to print positive numbers in the list using Python

Introduction

In this section of python programming, we will understand the code to print the positive numbers in the list. The condition for a positive number is that it should be greater and equal to 0.

Program to print positive numbers in the list

Program

input_list, positive_list, positive_list1 = [] , [] , []
k = 0
n = int(input("Enter the length of list "))
for i in range(0, n):
    value = int(input())
    input_list.append(value)
# Uisng lambda expression   
result = list(filter(lambda x: (x >= 0), input_list))
print("Positive numbers in the list using lambda expression: ", *result)


# Using loop
for j in input_list:
    if j >= 0:
        positive_list.append(j)
print("Positive numbers in the given list {0} using for loop are {1}".format(input_list, positive_list))


# Using while loop
while(k < len(input_list)):
    if input_list[k] >= 0:
        positive_list1.append(input_list[k])
    k += 1
print("Positive numbers int he given {0} are {1}".format(input_list, positive_list))

Output

Program to print positive numbers in the list Output

Explanation

In the above python code, we have used lambda expression and loops (for and while) to find the positive numbers in the list. The condition for positive list is that the number should be greater than or equals to 0.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author