w3resource

Python: Filter the positive numbers from a list

Python Basic: Exercise-114 with Solution

Filter Positive Numbers

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

Positive Numbers: Any number above zero is known as a positive number. Positive numbers are written without any sign or a '+' sign in front of them and they are counted up from zero, i.e. 1, + 2, 3, +4 etc.

Pictorial Presentation:

Filter the positive numbers from a list

Sample Solution-1:

Python Code:

# Define a list named "nums" containing a series of integers.
nums = [34, 1, 0, -23, 12, -88]

# Print a message along with the original list of numbers.
print("Original numbers in the list: ", nums)

# Create a new list "new_nums" by using the "filter" function with a lambda function
# that filters out positive numbers from the "nums" list.
new_nums = list(filter(lambda x: x > 0, nums))

# Print a message along with the list of positive numbers from the "nums" list.
print("Positive numbers in the said list: ", new_nums)

Sample Output:

Original numbers in the list:  [34, 1, 0, -23, 12, -88]
Positive numbers in the said list:  [34, 1, 12]

Sample Solution-2:

Python Code:

# Define a list named "nums" containing a series of integers.
nums = [34, 1, 0, -23, 12, -88]

# Print a message along with the original list of numbers.
print("Original numbers in the list: ", nums)

# Print a message indicating that positive numbers in the list will be listed.
print("Positive numbers in the said list: ")

# Iterate through each number in the "nums" list.
for pos_nums in nums:
    # Check if the number is greater than 0 (positive).
    if pos_nums > 0:
        # Print positive numbers on the same line with a space separator.
        print(pos_nums, end=" ")

Sample Output:

Original numbers in the list:  [34, 1, 0, -23, 12, -88]
Positive numbers in the said list: 
34 1 12 

Flowchart:

Flowchart: Filter the positive numbers from a list.

Sample Solution-3:

Python Code:

# Define a list named "nums" containing a series of integers.
nums = [34, 1, 0, -23, 12, -88]

# Print a message along with the original list of numbers.
print("Original numbers in the list: ", nums)

# Create a new list "pos_nums" using a list comprehension to filter positive numbers from "nums."
# Positive numbers are those greater than 0.
pos_nums = [n for n in nums if n > 0]

# Print a message indicating that positive numbers in the list will be listed,
# followed by the positive numbers separated by a space.
print("Positive numbers in the said list: ", *pos_nums)

Sample Output:

Original numbers in the list:  [34, 1, 0, -23, 12, -88]
Positive numbers in the said list:  34 1 12

Python Code Editor:

 

Previous: Write a Python program to input a number, if it is not a number generates an error message.
Next: Write a Python program to compute the product of a list of integers (without using for loop).

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/python-basic-exercise-114.php