How to Test for Positive Numbers in Python

In python programming, we can easily determine that the number is positive, negative, or zero by using different functions and statements. Firstly, get some knowledge on positive numbers and negative numbers along with python if-elif-else statement, nested if statement, for loop, while loop, list comprehensive topics for better understanding of how to check for positive numbers in python.

This tutorial of How to Test for Positive Numbers in Python contains the below modules:

How to Test for Positive Numbers in Python using if..elif..else statement?

Here, we have written a simple and straightforward code to work with any integer or number. In this section, we are testing the inputted number is positive or negative or zero in python using an if…elif…else statement. If the given number is greater than 0 (then it has to be positive), elif a number equals zero (then it’s neither positive nor negative…it’s simply zero), or else it’s less than zero (negative).

The following Python snippet defines how to test if an inputted number is positive or not. Check out the below program:

num = 4
if num > 0:
   print("Positive number")
elif num == 0:
   print("Zero")
else:
   print("Negative number")

In the illustration above, the number is equal to four, so the result would be a “Positive number” as it is greater than zero. If num was equal to -19, then the output would be “Negative number.”

Python program to print positive numbers in a list using For Loop

# Python program to print positive Numbers in a List
  
# list of numbers
list1 = [11, -21, 0, 45, 66, -93]
  
# iterating each number in list
for num in list1:
      
    # checking condition
    if num >= 0:
       print(num, end = " ")

Output: 

11 0 45 66

Check More:

Python Program to check if a Number is Positive, Negative or Zero using While Loop

# Python program to print positive Numbers in a List
  
# list of numbers
list1 = [-10, 21, -4, -45, -66, 93]
num = 0
  
# using while loop     
while(num < len(list1)):
      
    # checking condition
    if list1[num] >= 0:
        print(list1[num], end = " ")
      
    # increment num 
    num += 1

Result: 

21 93

Python Code to Test if a number is Positive using List Comprehension

# Python program to print Positive Numbers in a List
  
# list of numbers
list1 = [-10, -21, -4, 45, -66, 93]
  
# using list comprehension
pos_nos = [num for num in list1 if num >= 0]
  
print("Positive numbers in the list: ", *pos_nos)

Output: 

Positive numbers in the list: 45 93

Using Nested if Statement to find the Positive Number in Python

num = float(input("Enter a number: "))
if num >= 0:
   if num == 0:
       print("Zero")
   else:
       print("Positive number")
else:
   print("Negative number")

Output: 

Enter a number: 20
Positive number

Python program to print positive numbers in a list Using lambda expressions

# Python program to print positive Numbers in a List
  
# list of numbers 
list1 = [-10, 21, 4, -45, -66, 93, -11] 
  
  
# we can also print positive no's using lambda exp. 
pos_nos = list(filter(lambda x: (x >= 0), list1))
  
print("Positive numbers in the list: ", *pos_nos)

Result: 

Positive numbers in the list: 21, 4, 93

Leave a Reply

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