Python judges positive and negative numbers

I won't say much nonsense, everyone should just look at the code!

a1 =raw_input("please input a number")
a =int(a1)if(a!=0):if(a   0):
print 'This is a positive number'if(a <0):
print 'This is negative'else:
print 'the number is equal to 0'

Supplementary knowledge: Determine whether a value is positive, negative, zero, or integer

When I was reading "Introduction to ES6 Standards" by Teacher Ruan Yifeng recently, I saw that ES6 has added two new methods.

Used to judge a value.

1. Judging the integer-Number.isInteger()

Number.isInteger() First judge whether the value is of type number, instead of returning false directly;

If it is a number type, it is judged whether it is an integer.

Number.isInteger(25);//true
Number.isInteger(25.222);//false
Number.isInteger('25');// false
Number.isInteger('25.222');//false
Number.isInteger('foo');// false

It is also very simple to use Es5 to judge whether it is a positive number. There are many ways to implement it. Here are two:

1、 Use Math.round, use rounding to determine whether the value is an integer.

functionnumberIsInteger(n){if(!Number.isInteger){returntypeof n ==='number'&& Math.round(n)=== n;}return n;}

2、 Use the remainder.

functionnumberIsInteger(n){if(!Number.isInteger){returntypeof n ==='number'&& n %1===0;}return Number.isInteger(n);}

Second, determine whether a number is positive, negative, or zero---Math.sign()

Return 5 values:

+1 A positive number

  • 1 negative number
    0   0
  • 0   -0
    NaN other values
console.log(Math.sign(-5));//-1
console.log(Math.sign(-5.222));// -1
console.log(Math.sign(555));// 1
console.log(Math.sign(0));// 0
console.log(Math.sign(-0));// -0
console.log(Math.sign('foo'));// NaN

Es5 implementation method:

Math.sign = Math.sign ||function(n){
 n =+n;if(n ===0||isNaN(n)){return n;}return x   0?1:-1;}

The above python method of judging positive and negative numbers is all the content shared by the editor, I hope to give you a reference.

Recommended Posts

Python judges positive and negative numbers
Python and Go
Python introspection and reflection
[python] python2 and python3 under ubuntu
Python deconstruction and packaging
Python3 configuration and entry.md
Python automated operation and maintenance 2
Leetcode 2 add two numbers Python
Python negative modulus operation example
ubuntu18.04 compile and install python3.8
Centos 6.10 reinstall python and yum
Python open read and write
CentOS7 install python3 and pip3
Python automated operation and maintenance 1
Python data structure and algorithm
Python multi-process and multi-thread basics
CentOS 6.9 compile and install python
CentOS 6 compile and install python 3
Generators and iterators in Python