Python if number is positive

Python Program to Check If a Number is Positive, Negative or Zero

In this post, we will learn how to check whether a number is positive, negative or zero using Python Programming language.

This program asks the user to enter a number, then it will check whether the entered number is positive, negative or zero using the following approaches:

Читайте также:  Php array delete null values

So, without further ado, let’s begin this tutorial.

Python Program to Check If a Number is Positive, Negative or Zero

# Python Program to Check If a Number is Positive, Negative or Zero num = int(input("Enter a number: ")) # Checking the number if num > 0: print("The entered number is positive.") elif num == 0: print("The entered number is zero.") else: print("The entered number is negative.")
Enter a number: 5 The entered number is positive. 
Enter a number: -7 The entered number is negative. 
Enter a number: 0 The entered number is zero. 

How Does This Program Work ?

num = int(input("Enter a number: "))

The user is asked to enter a number. The entered number gets stored in the num named variable.

if num > 0: print("The entered number is positive.") elif num == 0: print("The entered number is zero.") else: print("The entered number is negative")

Then, we check whether the entered number is greater or less than 0. If the entered number is greater than 0, then the entered number is a positive number.

If the entered number equals to 0, then the entered number is zero.

Similarly, if the entered number is less than 0, then the entered number is a negative number.

Python Program to Check If a Number is Positive, Negative or Zero Using Nested List

# Python Program to Check If a Number is Positive, Negative or Zero Using Nested If # Asking for input num = int(input("Enter a number: ")) # Checking the number if num >= 0: if num == 0: print("The entered number is zero.") else: print("The entered number is positive.") else: print("The entered number is negative.")
Enter a number: -18 The entered number is negative. 
Enter a number: 27 The entered number is positive. 
Enter a number: 0 The entered number is zero. 

Conclusion

I hope after going through this post, you understand how to check if a number is positive, negative or zero using Python Programming language.

Читайте также:  Как строить функции питон

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.

  • Python Program to Print Hello World
  • Python Program to Add Two Numbers
  • Python Program to Subtract Two Numbers
  • Python Program to Multiply Two Numbers
  • Python Program to Find the Square Root

Источник

Python Program to Check if A Number is Positive, Negative, or Zero

In this Python example, we will discuss how can we check if any given number is positive, negative, or zero with explanation and implementation. Let’s get started.

1. What is Number and When is it Positive or Negative?

A number is a mathematical object used to count, measure, and label.

– Wikipedia

In simple words, we can say that number can be an arithmetical value, which can be expressed using different ways like word, symbol, or figure, representing a particular quantity and used for counting and can make the computation possible.

When any given number is greater than zero (0) then the number is said to be a positive number and when any given number is lesser than zero(0) then the number is said to be a negative number.

Example: Input : 4 Output : The number is positive Input : -5 Output : The number is negative

Some of the topics which will be helpful for understanding the program implementation better are:

2. Program to Check if A Number is Positive, Negative, or Zero

As discussed above, when the number is greater than zero then it is positive, and negative when it is lesser than zero and when it is equal to zero then the number is zero.

Hence, for the program implementation, we will use the if…else… condition along with the relational operator.

Let’s implement the code and see how it works.

#Python Program to Check is Number is >0 or 0: print("The given number is Positive number") elif num == 0: print("The given number is Zero") else: print("The given number is Negative number")
Output Enter any Number: 56 The given number is Positive number

In the above program, the given input is greater than zero so the output for the positive number was displayed.

3. Check if Any Number is Positive, Negative, or Zero using Exception Handling

Now, in the above program, we have covered the most basic way of implementation.

We can use the exception handling method if the user provides any other input rather than float value. Let’s use the exception handling method for the same problem and find out the working.

# Python Program to Check is Number is >0 or 0: print("The given number is Positive number") elif num == 0: print("The given number is Zero") elif num < 0: print("The given number is Negative number") except ValueError: print("Sorry, Your Input is Invalid")
Output Enter any Number: 67er Sorry, Your Input is Invalid

In the program, we have taken the input inside the try block and if the input cannot be converted to float then ValueError would have been given as output, but we have raised the exception for ValueError and handled the exception successfully.

4. Conclusion

In this article, we have learned how can we check if any provided number is negative, positive, or zero and also implemented the exception handling in case the output is not a number.

Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Python Program to Check if A Number is Positive, Negative, or Zero was last modified: September 19th, 2021 by Garvit Parakh

Источник

Python: Check if a number is positive, negative or zero

Write a Python program to check if a number is positive, negative or zero.

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.
Negative Numbers: Any number below zero is known as a negative number. Negative numbers are always written with a '−' sign in front of them and they are counted down from zero, i.e. -1, -2, -3, -4 etc.
Always look at the sign in front of a number to check if it is positive or negative. Zero, 0, is neither positive nor negative.

Pictorial Presentation:

Python: Check if a number is positive, negative or zero.

Sample Solution-1:

Python Code:

num = float(input("Input a number: ")) if num > 0: print("It is positive number") elif num == 0: print("It is Zero") else: print("It is a negative number") 
Input a number: 150 It is positive number

Flowchart: Check if a number is positive, negative or zero.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Sample Solution-2:

Python Code:

n = float(input('Input a number: ')) print('Number is Positive.' if n > 0 else 'It is Zero!' if n == 0 else 'Number is Negative.') 
Input a number: 0 It is Zero!

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Sample Solution-3:

Python Code:

n = float(input("Input a number: ")) if n >= 0: if n == 0: print("It is Zero!") else: print("Number is Positive number.") else: print("Number is Negative number.") 
Input a number: -150 Number is Negative number.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

How to lowercase a string in Python?

The official 2.x documentation is here: str.lower()

The official 3.x documentation is here: str.lower()

  • Weekly Trends
  • Java Basic Programming Exercises
  • SQL Subqueries
  • Adventureworks Database Exercises
  • C# Sharp Basic Exercises
  • SQL COUNT() with distinct
  • JavaScript String Exercises
  • JavaScript HTML Form Validation
  • Java Collection Exercises
  • SQL COUNT() function
  • SQL Inner Join
  • JavaScript functions Exercises
  • Python Tutorial
  • Python Array Exercises
  • SQL Cross Join
  • C# Sharp Array Exercises

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook

Источник

How to Test for Positive Numbers in Python | Python Program to Check if Number is Positive

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 = " ")

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

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)
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")
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)
Positive numbers in the list: 21, 4, 93

Источник

Оцените статью