Write a Python program to filter the positive numbers from a list

example 1:-

nums = [-10, 10, 15, -56]
print("Original numbers in the list: ",nums)
Pos_nums = list(filter(lambda x: x >0, nums))
print("Positive numbers in the list: ",Pos_nums)

output:-

Original numbers in the list:  [-10, 10, 15, -56]
Positive numbers in the list:  [10, 15]


example 2:- using for loop

list1 = [20,11,1,-3]
  
# iterating each number in list
for num in list1:
      
    # checking condition
    if num >= 0:
       print(num, end = " ")

output:-
20 11 1 
>>> 
example 3:-

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post